Skip to content
Snippets Groups Projects
Commit 6176cd96 authored by Lucas Gabriel Lima's avatar Lucas Gabriel Lima
Browse files

create route for population

parent d18c0a85
No related branches found
No related tags found
2 merge requests!116Release v1.0.0,!58Pib per capita e População
Pipeline #
......@@ -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;
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;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment