Skip to content
Snippets Groups Projects
idhml.js 3.89 KiB
Newer Older
Gabriel Ruschel's avatar
Gabriel Ruschel committed
const express = require('express');

const idhmlApp = express.Router();

const libs = `${process.cwd()}/libs`;

const squel = require('squel');

const query = require(`${libs}/middlewares/query`);

const response = require(`${libs}/middlewares/response`);

const id2str = require(`${libs}/middlewares/id2str`);

const ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`);

let rqf = new ReqQueryFields();

idhmlApp.get('/year_range', (req, res, next) => {
    req.sql.from('adh_idh')
    .field('MIN(adh_idh.ano_censo)', 'start_year')
    .field('MAX(adh_idh.ano_censo)', 'end_year');
    next();
}, query, (req, res, next) => {
    req.sql.from('adh_idh_uf')
    .field('MIN(adh_idh_uf.ano_censo)', 'start_year')
    .field('MAX(adh_idh_uf.ano_censo)', 'end_year');
    req.old_result = req.result;
    next();
}, query, (req, res, next) => {
    // console.log(req.old_result[0].start_year);
    // console.log(req.result[0].start_year);
    if (req.old_result[0].start_year < req.result[0].start_year) {
        req.result[0].start_year = req.old_result[0].start_year;
    }
    if (req.old_result[0].end_year > req.result[0].end_year) {
        req.result[0].end_year = req.old_result[0].old_result;
    }
    next();
}, query, response('range'));

Vytor Calixto's avatar
Vytor Calixto committed
idhmlApp.get('/years', (req, res, next) => {
    req.sql.from('adh_idh')
    .field('DISTINCT adh_idh.ano_censo', 'year');
    next();
}, query, (req, res, next) => {
    req.oldResult = req.result;

    req.sql = squel.select();

    req.sql.from('adh_idh_uf')
    .field('DISTINCT adh_idh_uf.ano_censo', 'year');
    next();
}, query, (req, res, next) => {
    let result = Object.assign(req.oldResult, req.result);
    req.result = result;
    next();
}, response('years'));

Gabriel Ruschel's avatar
Gabriel Ruschel committed
rqf.addField({
    name: 'filter',
    field: false,
    where: true
Gabriel Ruschel's avatar
Gabriel Ruschel committed
}).addField({
    name: 'dims',
    field: true,
    where: false
Gabriel Ruschel's avatar
Gabriel Ruschel committed
}).addValue({
    name: 'city',
    table: 'municipio',
    tableField: 'nome',
    resultField: 'city_name',
    where: {
        relation: '=',
        type: 'integer',
        field: 'municipio_id',
        table: 'adh_idh'
    },
    join: {
        primary: 'id',
        foreign: 'municipio_id',
        foreignTable: 'adh_idh'
    }
}).addValue({
    name: 'min_year',
    table: '@',
    tableField: 'ano_censo',
    resultField: 'year',
    where: {
        relation: '>=',
        type: 'integer',
        table: '@',
        field: 'ano_censo'
    }
}).addValue({
    name: 'max_year',
    table: '@',
    tableField: 'ano_censo',
    resultField: 'year',
    where: {
        relation: '<=',
        type: 'integer',
        table: '@',
        field: 'ano_censo'
    }
}).addValue({
    name: 'state',
    table: 'estado',
    tableField: 'nome',
    resultField: 'state_name',
    where: {
        relation: '=',
        type: 'integer',
        field: 'estado_id',
Gabriel Ruschel's avatar
Gabriel Ruschel committed
        table: '@'
Gabriel Ruschel's avatar
Gabriel Ruschel committed
    },
    join: {
        primary: 'id',
        foreign: 'estado_id',
Gabriel Ruschel's avatar
Gabriel Ruschel committed
        foreignTable: '@'
Gabriel Ruschel's avatar
Gabriel Ruschel committed
    }
});

idhmlApp.get('/', rqf.parse(), (req, res, next) => {
Gabriel Ruschel's avatar
Gabriel Ruschel committed

    if (("city" in req.dims) || ("city" in req.filter)) {
Gabriel Ruschel's avatar
Gabriel Ruschel committed
        req.sql.from('adh_idh')
Gabriel Ruschel's avatar
Gabriel Ruschel committed
        .field('adh_idh.idhm_l', 'total')
Gabriel Ruschel's avatar
Gabriel Ruschel committed
        .field('adh_idh.ano_censo', 'year')
Gabriel Ruschel's avatar
Gabriel Ruschel committed
        .field('adh_idh.municipio_id', 'city_id')
        .group('adh_idh.idhm_l')
        .group('adh_idh.ano_censo')
        .group('adh_idh.municipio_id');
    } else if (("state" in req.filter) || ("state" in req.dims)) {
Gabriel Ruschel's avatar
Gabriel Ruschel committed
        req.sql.from('adh_idh_uf')
        .field('adh_idh_uf.idhm_l', 'total')
        .field('adh_idh_uf.ano_censo', 'year')
Gabriel Ruschel's avatar
Gabriel Ruschel committed
        .field('adh_idh_uf.estado_id', 'state_id')
        .group('adh_idh_uf.idhm_l')
        .group('adh_idh_uf.ano_censo')
        .group('adh_idh_uf.estado_id');
Gabriel Ruschel's avatar
Gabriel Ruschel committed
    } else {
        res.status(400);
        next({
            status: 400,
            message: 'Wrong/No filter specified'
        });
Gabriel Ruschel's avatar
Gabriel Ruschel committed
    }
    next();
Gabriel Ruschel's avatar
Gabriel Ruschel committed
}, rqf.build(), query, id2str.transform(), response('idhme'));
Gabriel Ruschel's avatar
Gabriel Ruschel committed

module.exports = idhmlApp;