Skip to content
Snippets Groups Projects
Commit b8a37045 authored by Eduardo L. Buratti's avatar Eduardo L. Buratti
Browse files

web: Add route to list points (for search)

parent 615c7017
No related branches found
No related tags found
No related merge requests found
exports.list = function(req, res) {
var c = 1, filters, sort, offset, conditions = [], parameters = [], where = "";
filters = req.body.filters || {};
switch (req.body.sorting) {
case "name": sort = "name ASC"; break;
case "-name": sort = "name DESC"; break;
case "project": sort = "project ASC"; break;
case "-project": sort = "project DESC"; break;
case "location": sort = "location ASC"; break;
case "-location": sort = "location DESC"; break;
default: sort = "name ASC"; break;
}
offset = (req.body.page || 0) * 50;
for (var key in filters) {
switch (key) {
case "project":
for (var i = 0; i < filters[key].length; i++) {
conditions.push("project = $"+(c++));
parameters.push(filters[key][i]);
}
break;
}
}
if (conditions.length > 0)
where = " AND (" + conditions.join(' OR ') + ")";
var query = "\
SELECT \
pt.id, \
pt.name, \
pt.project, \
pt.id_city, \
initcap(ct.name) || ', ' || upper(ct.state) AS location \
FROM \
(SELECT \
t.id_point AS id, \
t.name, \
p.id_city, \
CASE WHEN p.is_gesac THEN 'TLBR/GESAC' ELSE 'TLBR' END AS project \
FROM telecenter t, point p \
WHERE t.id_point = p.id \
UNION \
SELECT \
id_point AS id, \
establishment AS name, \
p.id_city, \
'GESAC' AS project \
FROM convention c, point p \
WHERE c.id_point = p.id) pt, city ct \
WHERE pt.id_city = ct.id " + where + " \
ORDER BY " + sort + " \
LIMIT 50 \
OFFSET " + offset;
req.db.query(query, parameters, function(result) {
req.db.done();
res.json(result.rows);
});
};
\ No newline at end of file
...@@ -4,14 +4,17 @@ var express = require('express'); ...@@ -4,14 +4,17 @@ var express = require('express');
var config = require('./config.js'); var config = require('./config.js');
var db = require('./middleware/db.js'); var db = require('./middleware/db.js');
var points = require('./routes/points.js');
var port = parseInt(process.argv.splice(2)[0]) || 3000; var port = parseInt(process.argv.splice(2)[0]) || 3000;
var app = express(); var app = express();
app.use(express.logger('dev')); app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.static(__dirname + '/app')); app.use(express.static(__dirname + '/app'));
db.config(config.db_config); db.config(config.db_config);
app.get('/api/test/:from/:to', db.connect, db.query('queries/test.sql', ['from', 'to'])); app.all('/api/points', db.connect, points.list);
app.listen(port); app.listen(port);
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment