Target
Output data dynamically with mongoDB and GET parameters.
Source
var requestline = require('../libs/requestline');
var mongo = require('../libs/recommend_data');
var EventRelation = mongo.EventRelation;
function sort_items(items){
var result = new Array();
// copy
for (var key in items) {
var element = {};
element["event_id"] = key;
element["genre_id"] = items[key]["genre_id"];
element["similarity"] = items[key]["similarity"];
result.push(element);
}
// sort
for (var i = 0; i < items.length-1; i++) {
for (var j = i+1; j < items.length; j++) {
if (result[i]["similarity"] < result[j]["similarity"]) {
var tmp = result[i];
result[i] = result[j];
result[j] = tmp;
}
}
}
return result;
}
exports.index = function(req, res){
var reqline = new requestline['Requestline'](req.query);
console.log("Query Parameters");
console.log(reqline.getParams());
var items_tmp = {};
EventRelation.find({timeslot:reqline.getParams()['timeslot']}, function(err, items) {
for (var i = 0; i < items.length; i++) {
if (parseInt(items[i]["target_event_id"]) in items_tmp) {
items_tmp[parseInt(items[i]["target_id"])]["similarity"] += parseFloat(items[i]["distance"]);
} else {
items_tmp[parseInt(items[i]["target_id"])] = {};
items_tmp[parseInt(items[i]["target_id"])]["genre_id"] = parseInt(items[i]["target_genre_id"]);
items_tmp[parseInt(items[i]["target_id"])]["similarity"] = parseFloat(items[i]["distance"]);
}
}
var result = sort_items(items_tmp);
if (reqline.validate()) {
res.json(result);
} else {
res.json({ error : { type : "Qeury Syntax Error", message : "Query Syntax Error"}});
}
});
};