Skip to content
Snippets Groups Projects
pibpercapita.js 3.36 KiB
Newer Older
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('/years', (req, res, next) => {
    req.sql.from('ibge_pib').
    field('DISTINCT ibge_pib.ano_censo', 'year');
    next();
}, query, response('years'));

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: 'region',
    table: 'regiao',
    tableField: 'nome',
    resultField: 'region_name',
    where: {
      relation: '=',
      type: 'integer',
      field: 'estado_id',
      table: 'ibge_pib'
    },
    join: {
        primary: 'id',
        foreign: 'regiao_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',
Lucas Gabriel Lima's avatar
Lucas Gabriel Lima committed
    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')
Lucas Gabriel Lima's avatar
Lucas Gabriel Lima committed
  .field('SUM(ibge_pib.pib)/SUM(ibge_pib.populacao)', 'total')
  .field('ibge_pib.ano_censo', 'year')
  .group('ibge_pib.ano_censo')
  .order('ibge_pib.ano_censo')

Lucas Gabriel Lima's avatar
Lucas Gabriel Lima committed
}, query, id2str.transform(false), response('pibpercapita'));

module.exports = pibpercapitaApp;