diff --git a/src/libs/routes/api.js b/src/libs/routes/api.js index b7840c774ef61437cc15eb8658d35b231a262d35..53bf96bbf4b4f14a7853e272c747839523e54d14 100644 --- a/src/libs/routes/api.js +++ b/src/libs/routes/api.js @@ -32,6 +32,8 @@ const teacher = require('./teacher'); const idhmr = require('./idhmr'); +const idhm = require('./idhm'); + api.get('/', (req, res) => { res.json({ msg: 'SimCAQ API is running' }); }); @@ -49,5 +51,6 @@ api.use('/spatial', cache('1 day'), spatial); api.use('/classroom', cache('15 day'), classroom); api.use('/teacher', cache('1 day'), teacher); api.use('/idhmr', cache('1 day'), idhmr); +api.use('/idhm', cache('1 day'), idhm); module.exports = api; diff --git a/src/libs/routes/idhm.js b/src/libs/routes/idhm.js new file mode 100644 index 0000000000000000000000000000000000000000..cfc8044e0af2376025fc98beca8868aca4105d43 --- /dev/null +++ b/src/libs/routes/idhm.js @@ -0,0 +1,100 @@ +const express = require('express'); + +const idhmApp = express.Router(); + +const libs = `${process.cwd()}/libs`; + +const log = require(`${libs}/log`)(module); + +const squel = require('squel'); + +const query = require(`${libs}/middlewares/query`); + +const response = require(`${libs}/middlewares/response`); + +const ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`); + +const id2str = require(`${libs}/middlewares/id2str`); + +let rqf = new ReqQueryFields(); + +idhmApp.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, response('range')); + +rqf.addField({ + name: 'filter', + field: false, + where: true +}).addField({ + name: 'dims', + field: true, + where: false +}).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' + } + }).addValue({ + name: 'min_year', + table: 'adh_idh', + tableField: 'ano_censo', + resultField: 'year', + where: { + relation: '>=', + type: 'integer', + field: 'ano_censo' + } + }).addValue({ + name: 'max_year', + table: 'adh_idh', + tableField: 'ano_censo', + resultField: 'year', + where: { + relation: '<=', + type: 'integer', + field: 'ano_censo' + } +}); +idhmApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { + log.debug(req.sql.toParam()); + req.sql.field('(adh_idh.idhm)', 'IDHM') + .field("'Brasil'", 'name') + .field('adh_idh.municipio_id', 'municipio_id') + .field('adh_idh.estado_id', 'estado_id') + .field('adh_idh.ano_censo', 'year') + .from('adh_idh') + next(); +}, query, id2str.transform(true), response('idhm')); + +module.exports = idhmApp; diff --git a/src/libs/routes/idhmr.js b/src/libs/routes/idhmr.js index 0c52c1c10b4892c28c8c1cd83e161f778b06a82e..6a8aa706d0d60f3e54d3d4924026c5eb5e614924 100644 --- a/src/libs/routes/idhmr.js +++ b/src/libs/routes/idhmr.js @@ -91,6 +91,7 @@ idhmrApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { req.sql.field('(adh_idh.idhm_r)', 'IDHMR') .field("'Brasil'", 'name') .field('adh_idh.municipio_id', 'municipio_id') + .field('adh_idh.estado_id', 'estado_id') .field('adh_idh.ano_censo', 'year') .from('adh_idh') next();