From 6176cd96ce0884c011bf0be3c1127ed38f9f2174 Mon Sep 17 00:00:00 2001 From: Lucas Gabriel Lima <lgl15@inf.ufpr.br> Date: Thu, 29 Jun 2017 11:34:56 -0300 Subject: [PATCH] create route for population --- src/libs/routes/api.js | 3 + src/libs/routes/population.js | 105 ++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 src/libs/routes/population.js diff --git a/src/libs/routes/api.js b/src/libs/routes/api.js index 117bdf3e..07fb93e7 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 00000000..890f8c7e --- /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; -- GitLab