базовый поиск

This commit is contained in:
Alexey Kasyanchuk
2017-01-01 06:25:04 +03:00
parent 46708c8f7e
commit 1b6a15bcdd
4 changed files with 64 additions and 17 deletions

View File

@ -12,7 +12,7 @@ server.listen(8099);
var connection = mysql.createConnection({ var connection = mysql.createConnection({
host : 'localhost', host : 'localhost',
user : 'root', user : 'root',
password : '', password : 'degitisi',
database : 'btsearch' database : 'btsearch'
}); });
@ -26,18 +26,23 @@ app.use(express.static('build'));
io.on('connection', function(socket) io.on('connection', function(socket)
{ {
function baseRowData(row)
{
return {
hash: row.hash,
name: row.name,
size: row.size,
files: row.files,
piecelength: row.piecelength
}
}
socket.on('recentTorrents', function(callback) socket.on('recentTorrents', function(callback)
{ {
connection.query('SELECT * FROM `torrents` ORDER BY added DESC LIMIT 0,10', function (error, rows, fields) { connection.query('SELECT * FROM `torrents` ORDER BY added DESC LIMIT 0,10', function (error, rows, fields) {
let torrents = []; let torrents = [];
rows.forEach((row) => { rows.forEach((row) => {
torrents.push({ torrents.push(baseRowData(row));
hash: row.hash,
name: row.name,
size: row.size,
files: row.files,
piecelength: row.piecelength,
});
}); });
callback(torrents) callback(torrents)
@ -52,13 +57,34 @@ io.on('connection', function(socket)
return; return;
} }
callback({ callback(baseRowData(rows[0]))
hash: rows[0].hash, });
name: rows[0].name, });
size: rows[0].size,
files: rows[0].files, socket.on('search', function(text, callback)
piecelength: rows[0].piecelength, {
}) let search = {};
console.log(text);
let q = 2;
connection.query('SELECT * FROM `torrents` WHERE MATCH(`name`) AGAINST(?)', text, function (error, rows, fields) {
rows.forEach((row) => {
search[row.hash] = baseRowData(row);
});
if(--q == 0)
callback(Object.keys(search).map(function(key) {
return search[key];
}));
});
connection.query('SELECT * FROM `files` INNER JOIN torrents ON(torrents.hash = files.hash) WHERE MATCH(`path`) AGAINST(?)', text, function (error, rows, fields) {
rows.forEach((row) => {
search[row.hash] = baseRowData(row);
search[row.hash].path = row.path;
});
if(--q == 0)
callback(Object.keys(search).map(function(key) {
return search[key];
}));
}); });
}); });
}); });
@ -126,5 +152,5 @@ connection.connect(function(err) {
// spider.on('nodes', (nodes)=>console.log('foundNodes')) // spider.on('nodes', (nodes)=>console.log('foundNodes'))
spider.listen(4445) //spider.listen(4445)
}); });

View File

@ -5,7 +5,6 @@
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico"> <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<title>BT Search</title> <title>BT Search</title>
<script src="http://localhost:8099/socket.io/socket.io.js"></script>
</head> </head>
<body> <body>
<div id="root"></div> <div id="root"></div>

View File

@ -5,12 +5,14 @@ var io = require("socket.io-client");
window.torrentSocket = io('http://localhost:8099/'); window.torrentSocket = io('http://localhost:8099/');
import RecentTorrents from './recent-torrents' import RecentTorrents from './recent-torrents'
import Search from './search'
class App extends Component { class App extends Component {
render() { render() {
return ( return (
<div className="App"> <div className="App">
<RecentTorrents /> <RecentTorrents />
<Search />
</div> </div>
); );
} }

20
src/search.js Normal file
View File

@ -0,0 +1,20 @@
import React, { Component } from 'react';
export default class Search extends Component {
componentDidMount() {
}
render() {
return (
<div className="row">
<input type='text' ref='searchInput' onKeyPress={(e) => {
if (e.key === 'Enter') {
window.torrentSocket.emit('search', e.target.value, (torrents) => {
console.log(torrents);
});
}
}} />
</div>
);
}
}