Tech Tips

  1. Uncategorized
  2. 168 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"}});
		}
	});
};

Uncategorized recent post

  1. Run Amazon FreeRTOS on M5Stack Core2 for AWS …

  2. Udacity Self-Driving Car Engineer Nanodegree …

  3. Install sbt 1.0.0 and run sample template

  4. Visualization of Neural Network and its Train…

  5. [Machine Learning]Created docker image includ…

関連記事

PAGE TOP