1

Adding exports for category operations

This commit is contained in:
Alex Yatskov 2015-03-03 10:55:32 +09:00
parent b3b5f72a2e
commit 3d0fb315ac
4 changed files with 48 additions and 5 deletions

View File

@ -27,7 +27,7 @@
function onAdjust(name, value) {
_ctx.query.features[name] = value;
$.getJSON('/search', _ctx.query, function(results) {
$.getJSON('/query', _ctx.query, function(results) {
saveSnapshot(results);
outputSnapshot(results, true);
});
@ -61,7 +61,7 @@
};
}
$.getJSON('/search', _ctx.query, function(results) {
$.getJSON('/query', _ctx.query, function(results) {
if (!_.has(_ctx, 'grapher')) {
_ctx.grapher = new grapher.Grapher({
canvas: new Snap('#svg'),

View File

@ -13,6 +13,7 @@
"express": "~4.5.1",
"mysql": "^2.5.0",
"underscore": "^1.6.0",
"geolib": "~2.0.14"
"geolib": "~2.0.14",
"node-uuid": "~1.4.2"
}
}

View File

@ -26,6 +26,7 @@
var _ = require('underscore');
var geolib = require('geolib');
var mysql = require('mysql');
var uuid = require('node-uuid');
var pool = null;
@ -209,6 +210,33 @@ function sanitizeQuery(query) {
query.features = features;
}
function getCategories(callback) {
pool.query('SELECT * FROM categories', function(err, rows) {
if (err) {
throw err;
}
var categories = _.map(rows, function(row) {
return {id: row.id, description: row.description};
});
callback(categories);
});
}
function addCategory(query, callback) {
var description = query.description;
var id = uuid.v1();
pool.query('INSERT INTO categories(description, id) VALUES(?, ?)', [description, id], function(err, rows) {
if (err) {
throw err;
}
callback({id: id, description: description});
});
}
function execQuery(query, callback) {
sanitizeQuery(query);
@ -252,5 +280,7 @@ function execQuery(query, callback) {
module.exports = {
loadDb: loadDb,
execQuery: execQuery
execQuery: execQuery,
getCategories: getCategories,
addCategory: addCategory
};

View File

@ -39,12 +39,24 @@ function main(staticFiles, port) {
user: 'hscd'
});
app.use('/search', function(req, res) {
app.use('/query', function(req, res) {
search.execQuery(req.query, function(results) {
res.json(results);
});
});
app.use('/categories', function(req, res) {
search.getCategories(function(results) {
res.json(results);
});
});
app.use('/learn', function(req, res) {
search.addCategory(req.query, function(results) {
res.json(results);
});
});
app.use(express.static(path.join(__dirname, '..', staticFiles)));
app.listen(port);
}