diff --git a/src/libs/routes/api.js b/src/libs/routes/api.js index 2e7c9a3efd4267a6e75f5e53ed187eebde464e2a..00ead593b5721caaef808bad7f2b478c1420eb26 100644 --- a/src/libs/routes/api.js +++ b/src/libs/routes/api.js @@ -30,6 +30,8 @@ const classroom = require('./classroom'); const teacher = require('./teacher'); +const idhme = require('./idhme'); + api.get('/', (req, res) => { res.json({ msg: 'SimCAQ API is running' }); }); @@ -46,5 +48,6 @@ api.use('/school', cache('15 day'), school); api.use('/spatial', cache('1 day'), spatial); api.use('/classroom', cache('15 day'), classroom); api.use('/teacher', cache('1 day'), teacher); +api.use('/idhme', cache('15 day'), idhme); module.exports = api; diff --git a/src/libs/routes/idhme.js b/src/libs/routes/idhme.js new file mode 100644 index 0000000000000000000000000000000000000000..a33e360b5bd9076f63f5f3692a0424aa702cb8b4 --- /dev/null +++ b/src/libs/routes/idhme.js @@ -0,0 +1,73 @@ +const express = require('express'); + +const idhmeApp = express.Router(); + +const libs = `${process.cwd()}/libs`; + +const squel = require('squel'); + +const query = require(`${libs}/middlewares/query`); + +const response = require(`${libs}/middlewares/response`); + +const id2str = require(`${libs}/middlewares/id2str`); + +const ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`); + +let rqf = new ReqQueryFields(); + +rqf.addField({ + name: 'filter', + field: false, + where: true +}).addValue({ + name: 'city', + table: 'municipio', + tableField: 'nome', + resultField: 'city_name', + where: { + relation: '=', + type: 'integer', + field: 'municipio_id', + table: 'adh_idh' + }, + join: { + primary: 'id', + foreign: 'municipio_id', + foreignTable: 'adh_idh' + } +}).addValue({ + name: 'state', + table: 'estado', + tableField: 'nome', + resultField: 'state_name', + where: { + relation: '=', + type: 'integer', + field: 'estado_id', + table: 'adh_idh' + }, + join: { + primary: 'id', + foreign: 'estado_id', + foreignTable: 'adh_idh' + } +}); + +idhmeApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { + if(typeof req.filter === 'undefined' || Object.keys(req.filter).length === 0) { + res.status(400); + next({ + status: 400, + message: 'Wrong/No filter specified' + }); + } + req.sql.from('adh_idh') + .field('adh_idh.idhm_e', 'IDHME') + .field('adh_idh.ano_censo', 'year') + .field('adh_idh.estado_id', 'state_id') + .field('adh_idh.municipio_id', 'city_id'); + next(); +}, query, response('idhme')); + +module.exports = idhmeApp;