Refactor more, fix and design 404

This commit is contained in:
Jonathan Cremin 2015-08-21 18:33:50 +01:00
parent 7de374e00b
commit 8521baa6d9
11 changed files with 223 additions and 138 deletions

View file

@ -3,6 +3,9 @@ import co from 'co';
import lookup from '../lib/lookup';
import services from '../lib/services';
import debuglog from 'debug';
const debug = debuglog('match.audio:search');
module.exports = function* () {
const url = parse(this.request.body.url);
this.assert(url.host, 400, {error: {message: 'You need to submit a url.'}});
@ -26,17 +29,21 @@ module.exports = function* () {
yield this.db.matches.save({_id: item.service + '$$' + item.id, 'created_at': new Date(), services: matches});
this.body = item;
process.nextTick(co.wrap(function* (){
process.nextTick(() => {
for (let service of services) {
if (service.id === item.service) {
continue;
}
matches[service.id] = {service: service.id};
const match = yield service.search(item);
match.matched_at = new Date(); // eslint-disable-line camelcase
const update = {};
update['services.' + match.service] = match;
yield this.db.matches.updateOne({_id: item.service + '$$' + item.id}, {'$set': update});
co(function* (){
const match = yield service.search(item);
match.matched_at = new Date(); // eslint-disable-line camelcase
const update = {};
update['services.' + match.service] = match;
yield this.db.matches.updateOne({_id: item.service + '$$' + item.id}, {'$set': update});
}.bind(this)).catch((err) => {
debug(err);
});
}
}.bind(this)));
});
};

View file

@ -31,15 +31,13 @@ export default function* (serviceId, type, itemId, format, next) {
const shares = formatAndSort(doc.services, serviceId);
if (format === 'json') {
this.body = {shares: shares};
} else {
const Handler = yield createHandler(routes, this.request.url);
const App = React.createFactory(Handler);
let content = React.renderToString(new App({shares: shares}));
content = content.replace('</body></html>', '<script>var shares = ' + JSON.stringify(shares) + '</script></body></html>');
this.body = '<!doctype html>\n' + content;
return this.body = {shares: shares};
}
const Handler = yield createHandler(routes, this.request.url);
const App = React.createFactory(Handler);
let content = React.renderToString(new App({shares: shares}));
content = content.replace('</body></html>', '<script>var shares = ' + JSON.stringify(shares) + '</script></body></html>');
this.body = '<!doctype html>\n' + content;
};