Production Level Knowledge & Tips

  1. 未分類
  2. 3 view

[node.js][express]RESTful API part2

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"}});
		}
	});
};

未分類 recent post

  1. Install sbt 1.0.0 and run sample template

  2. Visualization of Neural Network and its Train…

  3. [Machine Learning]Created docker image includ…

  4. [Node.js]How to write batch script with Node.…

  5. [Hive]Get top n items for each category

関連記事

PAGE TOP