diff --git a/src/libs/routes/api.js b/src/libs/routes/api.js index 00ead593b5721caaef808bad7f2b478c1420eb26..0e9d294a0d7026e8f3b9dea4b51a7daabf549f3a 100644 --- a/src/libs/routes/api.js +++ b/src/libs/routes/api.js @@ -32,6 +32,10 @@ const teacher = require('./teacher'); const idhme = require('./idhme'); +const pibpercapita = require('./pibpercapita') + +const population = require('./population') + api.get('/', (req, res) => { res.json({ msg: 'SimCAQ API is running' }); }); @@ -49,5 +53,7 @@ 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); +api.use('/pibpercapita', cache('1 day'), pibpercapita); +api.use('/population', cache('1 day'), population); module.exports = api; diff --git a/src/libs/routes/pibpercapita.js b/src/libs/routes/pibpercapita.js new file mode 100644 index 0000000000000000000000000000000000000000..a504fad4cce570c31783652c2f7955cecd1b2fa6 --- /dev/null +++ b/src/libs/routes/pibpercapita.js @@ -0,0 +1,122 @@ +const express = require('express'); + +const pibpercapitaApp = 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(); + +pibpercapitaApp.get('/year_range', (req, res, next) => { + req.sql.from('ibge_pib') + .field('MIN(ibge_pib.ano_censo)', 'start_year') + .field('MAX(ibge_pib.ano_censo)', 'end_year'); + next(); +}, query, response('range')); + +pibpercapitaApp.get('/income_level', (req, res, next) => { + req.result = [ + {id: 1, name: "1º quintil – 20% menores"}, + {id: 2, name: "2º quintil"}, + {id: 3, name: "3º quintil"}, + {id: 4, name: "4º quintil"}, + {id: 5, name: "5º quintil – 20% maiores"}, + ]; + next(); +}, response('income_level')); + +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_pib' + }, + join: { + primary: 'id', + foreign: 'municipio_id', + foreignTable: 'ibge_pib' + } +}).addValue({ + name: 'state', + table: 'estado', + tableField: 'nome', + resultField: 'state_name', + where: { + relation: '=', + type: 'integer', + field: 'estado_id', + table: 'ibge_pib' + }, + join: { + primary: 'id', + foreign: 'estado_id', + foreignTable: 'ibge_pib' + } +}).addValue({ + name: 'min_year', + table: 'ibge_pib', + tableField: 'ano_censo', + resultField: 'year', + where: { + relation: '>=', + type: 'integer', + field: 'ano_censo' + } +}).addValue({ + name: 'max_year', + table: 'ibge_pib', + tableField: 'ano_censo', + resultField: 'year', + where: { + relation: '<=', + type: 'integer', + field: 'ano_censo' + } +}).addValue({ + name: 'income_level', + table: 'ibge_pib', + tableField: 'nivel_renda_per_capita', + resultField: 'income_level_id', + where: { + relation: '=', + type: 'integer', + field: 'nivel_renda_per_capita' + } +}); + +pibpercapitaApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { + log.debug(req.sql.toParam()); + req.sql.from('ibge_pib') + .field('SUM(ibge_pib.pib)/SUM(ibge_pib.populacao)', 'pibpercapita') + .field('ibge_pib.ano_censo', 'year') + .group('ibge_pib.ano_censo') + .order('ibge_pib.ano_censo') + + next(); +}, query, id2str.transform(true), response('pibpercapita')); + +module.exports = pibpercapitaApp; diff --git a/src/libs/routes/population.js b/src/libs/routes/population.js new file mode 100644 index 0000000000000000000000000000000000000000..f2a89783ddad50859cdffeeb38a8d7b76f89a507 --- /dev/null +++ b/src/libs/routes/population.js @@ -0,0 +1,141 @@ +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')); + +populationApp.get('/city_size', (req, res, next) => { + req.result = [ + {id: 1, name: "até 5000"}, + {id: 2, name: "5001 - 10000"}, + {id: 3, name: "10001 - 20000"}, + {id: 4, name: "20001 - 50000"}, + {id: 5, name: "50001 - 100000"}, + {id: 6, name: "100001 - 500000"}, + {id: 7, name: "mais que 500000"} + ]; + next(); +}, response('city_size')); + +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: 'region', + table: 'regiao', + tableField: 'nome', + resultField: 'region_name', + where: { + relation: '=', + type: 'integer', + field: 'regiao_id', + table: 'ibge_populacao' + }, + join: { + primary: 'id', + foreign: 'regiao_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' + } +}).addValue({ + name: 'city_size', + table: 'ibge_populacao', + tableField: 'porte', + resultField: 'city_size_id', + where: { + relation: '=', + type: 'integer', + field: 'porte' + } +}); + +populationApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { + log.debug(req.sql.toParam()); + log.debug(req.dims); + req.sql.from('ibge_populacao') + .field('SUM(ibge_populacao.populacao)', 'total') + .field('ibge_populacao.ano_censo', 'year') + .group('ibge_populacao.ano_censo') + .order('ibge_populacao.ano_censo') + + next(); +}, query, id2str.transform(true), response('population')); + +module.exports = populationApp;