From 3d0fb315acb3a2d55fb46d91922cd08d4b45407a Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Tue, 3 Mar 2015 10:55:32 +0900 Subject: [PATCH] Adding exports for category operations --- client/scripts/search.js | 4 ++-- server/package.json | 3 ++- server/search.js | 32 +++++++++++++++++++++++++++++++- server/server.js | 14 +++++++++++++- 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/client/scripts/search.js b/client/scripts/search.js index 4d4b99d..3849a2c 100644 --- a/client/scripts/search.js +++ b/client/scripts/search.js @@ -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'), diff --git a/server/package.json b/server/package.json index 386a39d..70ce419 100644 --- a/server/package.json +++ b/server/package.json @@ -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" } } diff --git a/server/search.js b/server/search.js index 9ada68c..6173cba 100644 --- a/server/search.js +++ b/server/search.js @@ -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 }; diff --git a/server/server.js b/server/server.js index b19ee2e..aa12757 100755 --- a/server/server.js +++ b/server/server.js @@ -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); }