Switch to Koa, more es6

This commit is contained in:
Jonathan Cremin 2015-06-03 21:45:54 -07:00
parent 1090affc9c
commit b3abff99ae
36 changed files with 25573 additions and 928 deletions

View file

@ -1,44 +1,42 @@
"use strict";
var parse = require("url").parse;
var lookup = require("../lib/lookup");
var services = require("../lib/services");
import {parse} from 'url';
import co from 'co';
import lookup from '../lib/lookup';
import services from '../lib/services';
module.exports = function(req, res) {
var url = parse(req.body.url);
if (!url.host) {
return res.json({error: {message: "You need to submit a url."}});
}
module.exports = function* () {
let url = parse(this.request.body.url);
this.assert(url.host, 400, {error: {message: 'You need to submit a url.'}});
var promise = lookup(req.body.url);
let item = yield lookup(this.request.body.url);
if (!promise) {
return res.json({error: {message: "No supported music found at that link :("}});
}
this.assert(item, 400, {error: {message: 'No supported music found at that link :('}});
promise.then(function(item) {
if (!item) {
return res.json({error: {message: "No supported music found at that link :("}});
item.matched_at = new Date(); // eslint-disable-line camelcase
let matches = {};
matches[item.service] = item;
for (let service of services) {
if (service.id === item.service) {
continue;
}
item.matched_at = new Date(); // eslint-disable-line camelcase
var matches = {};
matches[item.service] = item;
services.forEach(function(service) {
matches[service.id] = {service: service.id};
}
yield this.db.matches.save({_id: item.service + '$$' + item.id, 'created_at': new Date(), services: matches});
this.body = item;
process.nextTick(co.wrap(function* (){
for (let service of services) {
if (service.id === item.service) {
return;
continue;
}
matches[service.id] = {service: service.id};
service.search(item).then(function(match) {
match.matched_at = new Date(); // eslint-disable-line camelcase
var update = {};
update["services." + match.service] = match;
req.db.matches.update({_id: item.service + "$$" + item.id}, {"$set": update});
});
});
return req.db.matches.save({_id: item.service + "$$" + item.id, "created_at": new Date(), services: matches}).then(function() {
res.json(item);
});
}, function(error) {
console.log(error.stack);
res.json({error: {message: "No matches found for this link, sorry :("}});
});
let match = yield service.search(item);
match.matched_at = new Date(); // eslint-disable-line camelcase
let update = {};
update['services.' + match.service] = match;
yield this.db.matches.updateOne({_id: item.service + '$$' + item.id}, {'$set': update});
}
}.bind(this)));
};