diff --git a/src/libs/routes/api.js b/src/libs/routes/api.js index 117bdf3eed0faad6665571e5ce213575cbc110b2..07fb93e7ee2b42e1ff9becc51fb9d436cb9bf5c5 100644 --- a/src/libs/routes/api.js +++ b/src/libs/routes/api.js @@ -34,6 +34,8 @@ const idhmr = require('./idhmr'); const pibpercapita = require('./pibpercapita') +const population = require('./population') + api.get('/', (req, res) => { res.json({ msg: 'SimCAQ API is running' }); }); @@ -52,5 +54,6 @@ api.use('/classroom', cache('15 day'), classroom); api.use('/teacher', cache('1 day'), teacher); api.use('/idhmr', cache('1 day'), idhmr); api.use('/pibpercapita', cache('1 day'), pibpercapita); +api.use('/population', cache('1 day'), population); module.exports = api; diff --git a/src/libs/routes/population.js b/src/libs/routes/population.js new file mode 100644 index 0000000000000000000000000000000000000000..890f8c7e66c707930736ea0aac4eea6f5311cc6c --- /dev/null +++ b/src/libs/routes/population.js @@ -0,0 +1,105 @@ +const express = require('express'); + +const populationApp = 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(); + +populationApp.get('/year_range', (req, res, next) => { + req.sql.from('ibge_populacao') + .field('MIN(ibge_populacao.ano_censo)', 'start_year') + .field('MAX(ibge_populacao.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: 'ibge_populacao' + }, + join: { + primary: 'id', + foreign: 'municipio_id', + foreignTable: 'ibge_populacao' + } +}).addValue({ + name: 'state', + table: 'estado', + tableField: 'nome', + resultField: 'state_name', + where: { + relation: '=', + type: 'integer', + field: 'estado_id', + table: 'ibge_populacao' + }, + join: { + primary: 'id', + foreign: 'estado_id', + foreignTable: 'ibge_populacao' + } +}).addValue({ + name: 'min_year', + table: 'ibge_populacao', + tableField: 'ano_censo', + resultField: 'year', + where: { + relation: '>=', + type: 'integer', + field: 'ano_censo' + } +}).addValue({ + name: 'max_year', + table: 'ibge_populacao', + tableField: 'ano_censo', + resultField: 'year', + where: { + relation: '<=', + type: 'integer', + field: 'ano_censo' + } +}); + +populationApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { + log.debug(req.sql.toParam()); + req.sql.field('(ibge_populacao.populacao)', 'populacao') + .field('ibge_populacao.municipio_id', 'municipio_id') + .field('ibge_populacao.ano_censo', 'year') + .from('ibge_populacao') + next(); +}, query, (req, res, next) => { + let somapib = 0; + for (var i = 0; i < req.result.length; i++) { + req.result[i]; + } + next() +}, id2str.transform(true), response('population')); + +module.exports = populationApp;