diff --git a/src/libs/routes/idhme.js b/src/libs/routes/idhme.js index c28a6ddf2efc29a3b006248b9beacf6401c246fc..1a3c581ca4de0dc2a202ec44953549e391a6fb73 100644 --- a/src/libs/routes/idhme.js +++ b/src/libs/routes/idhme.js @@ -16,12 +16,28 @@ const ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`); let rqf = new ReqQueryFields(); -// idhmeApp.get('/year_range', (req, res, next) => { -// req.sql.from('turma') -// .field('MIN(turma.ano_censo)', 'start_year') -// .field('MAX(turma.ano_censo)', 'end_year'); -// next(); -// }, query, response('range')); +idhmeApp.get('/year_range', (req, res, next) => { + req.sql.from('adh_idh') + .field('MIN(adh_idh.ano_censo)', 'start_year') + .field('MAX(adh_idh.ano_censo)', 'end_year'); + next(); +}, query, (req, res, next) => { + req.sql.from('adh_idh_uf') + .field('MIN(adh_idh_uf.ano_censo)', 'start_year') + .field('MAX(adh_idh_uf.ano_censo)', 'end_year'); + req.old_result = req.result; + next(); +}, query, (req, res, next) => { + // console.log(req.old_result[0].start_year); + // console.log(req.result[0].start_year); + if (req.old_result[0].start_year < req.result[0].start_year) { + req.result[0].start_year = req.old_result[0].start_year; + } + if (req.old_result[0].end_year > req.result[0].end_year) { + req.result[0].end_year = req.old_result[0].old_result; + } + next(); +}, query, response('range')); rqf.addField({ name: 'filter', @@ -43,6 +59,28 @@ rqf.addField({ foreign: 'municipio_id', foreignTable: 'adh_idh' } +}).addValue({ + name: 'min_year', + table: '@', + tableField: 'ano_censo', + resultField: 'year', + where: { + relation: '>=', + type: 'integer', + table: '@', + field: 'ano_censo' + } +}).addValue({ + name: 'max_year', + table: '@', + tableField: 'ano_censo', + resultField: 'year', + where: { + relation: '<=', + type: 'integer', + table: '@', + field: 'ano_censo' + } }).addValue({ name: 'state', table: 'estado', @@ -59,20 +97,9 @@ rqf.addField({ foreign: 'estado_id', foreignTable: 'adh_idh_uf' } -}).addValue({ - name: 'year', - table: 'adh_idh', - tableField: 'ano_censo', - resultField: 'year', - where: { - relation: '=', - type: 'integer', - field: 'ano_censo', - table: 'adh_idh' - } }); -idhmeApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { +idhmeApp.get('/', rqf.parse(), (req, res, next) => { if(typeof req.filter === 'undefined' || Object.keys(req.filter).length === 0) { res.status(400); next({ @@ -83,7 +110,7 @@ idhmeApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { if ("state" in req.filter) { // console.log("sim"); req.sql.from('adh_idh_uf') - .field('adh_idh_uf.idhm_e', 'IDHME') + .field('adh_idh_uf.idhm_e', 'total') .field('adh_idh_uf.ano_censo', 'year') .field('adh_idh_uf.estado_id', 'state_id'); } else if ("city" in req.filter) { @@ -91,8 +118,13 @@ idhmeApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { .field('adh_idh.idhm_e', 'IDHME') .field('adh_idh.ano_censo', 'year') .field('adh_idh.municipio_id', 'city_id'); + } else { + next({ + status: 400, + message: 'Wrong/No filter specified' + }); } next(); -}, query, response('idhme')); +}, rqf.build(), query, response('idhme')); module.exports = idhmeApp;