Skip to content
Snippets Groups Projects
Unverified Commit 7e4819e9 authored by João Victor Risso's avatar João Victor Risso
Browse files

Refactor enrollments route to use function chaining

parent 5ebad2f8
No related branches found
No related tags found
1 merge request!7Refactor enrollments route to include query building
Pipeline #
'use strict';
var express = require('express') var express = require('express')
var xml = require('js2xmlparser') var xml = require('js2xmlparser')
var router = express.Router() var router = express.Router()
...@@ -56,39 +58,34 @@ router.get('/data', function(req, res) { ...@@ -56,39 +58,34 @@ router.get('/data', function(req, res) {
}) })
}) })
router.get('/enrollments', function(req, res) { router.get('/enrollments', function(req, res, next) {
var params = req.query; const params = req.query;
var id = 0;
var location_id = 0;
var adm_dependency_id = 0;
var census_year = 0;
var enrollmentSql = "";
if (params.id) if (params.id) {
{ req.id = parseInt(params.id, 10);
id = parseInt(params.id, 10);
} }
if (params.location_id) if (params.location_id) {
{ req.location_id = parseInt(params.location_id, 10);
location_id = parseInt(params.location_id, 10);
} }
if (params.adm_dependency_id) if (params.adm_dependency_id) {
{ req.adm_dependency_id = parseInt(params.adm_dependency_id, 10);
adm_dependency_id = parseInt(params.adm_dependency_id, 10);
} }
if (params.census_year) if (params.census_year) {
{ req.census_year = parseInt(params.census_year, 10);
census_year = parseInt(params.census_year, 10);
} }
/** if (params.aggregate) {
* FIXME: parameter substitution in the queries is not safe (vulnerable to log.debug('aggregate parameter detected');
* SQL injection). Substitution from MonetDB module is not working for some next('route');
* reason. } else {
*/ log.debug('No aggregate parameter detected');
next();
}
/*
switch(params.aggregate) switch(params.aggregate)
{ {
case "city": case "city":
...@@ -132,7 +129,77 @@ router.get('/enrollments', function(req, res) { ...@@ -132,7 +129,77 @@ router.get('/enrollments', function(req, res) {
}); });
} }
log.debug("All resources were released"); log.debug("All resources were released");
}, function(error) {
}); });
}) */
}, function(req, res, next) {
/** When no +aggregate+ parameter value is specified on the request, then
* assign the query to compute the result for the whole country.
*/
log.debug('Using SQL query for the whole country');
req.sql_query = 'SELECT * FROM turmas LIMIT 1';
next('route');
});
router.get('/enrollments', function(req, res, next) {
const params = req.query;
if (!params.aggregate) {
next('route');
} else if (params.aggregate == 'region') {
log.debug('Using enrollments query for regions');
req.sql_query = 'SELECT * FROM turmas LIMIT 1';
}
next('route');
});
router.get('/enrollments', function(req, res, next) {
const params = req.query;
if (!params.aggregate) {
next('route');
} else if (params.aggregate == 'state') {
log.debug('Using enrollments query for states');
req.sql_query = 'SELECT * FROM turmas LIMIT 1';
}
next('route');
});
router.get('/enrollments', function(req, res, next) {
const params = req.query;
if (!params.aggregate) {
next('route');
} else if (params.aggregate == 'city') {
log.debug('Using enrollments query for cities');
req.sql_query = 'SELECT * FROM turmas LIMIT 1';
}
next('route');
});
router.get('/enrollments', function(req, res, next) {
log.debug('Request parameters: ${req}?');
if (!req.sql_query) {
/* Should only happen if there is a bug in the chaining of the
* '/enrollments' route, since when no +aggregate+ parameter is given,
* it defaults to use the query for the whole country.
*/
log.error('BUG -- No SQL query was found to be executed!');
res.status(501).end();
} else {
log.debug('SQL query: ${req.sql_query}?');
conn.query(req.sql_query, true).then(function(result) {
log.debug(result);
if (req.query.format === 'csv') {
res.csv(result.data);
} else if (req.query.format === 'xml') {
res.send(xml('result', JSON.stringify({enrollments: result.data})));
} else {
res.json({ result: result.data });
}
}, function(error) {
log.error('SQL query error: ${error}?');
res.status(501).end();
});
}
});
module.exports = router module.exports = router
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