/* Copyright (C) 2016 Centro de Computacao Cientifica e Software Livre Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR This file is part of simcaq-node. simcaq-node is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. simcaq-node is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with simcaq-node. If not, see <https://www.gnu.org/licenses/>. */ const express = require('express'); const PnadNovoApp = express.Router(); const libs = `${process.cwd()}/libs`; const squel = require('squel'); const query = require(`${libs}/middlewares/query`).query; const response = require(`${libs}/middlewares/response`); const ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`); const config = require(`${libs}/config`); const cache = require('apicache').options({ debug: config.debug, statusCodes: {include: [200]} }).middleware; let rqf = new ReqQueryFields(); PnadNovoApp.use(cache('15 day')); rqf.addField({ name: 'filter', field: false, where: true }).addField({ name: 'dims', field: true, where: false }).addValue({ name: 'id', table: 'pnad_novo', tableField: 'id', where: { relation: '=', type: 'integer', field: 'id' } }).addValueToField({ name: 'state', table: 'estado', tableField: ['nome', 'id'], resultField: ['state_name', 'state_id'], where: { relation: '=', type: 'integer', field: 'id', }, join: { primary: 'id', foreign: 'cod_uf', foreignTable: 'pnad_novo' } }, 'filter').addValue({ name: 'state_not', table: 'estado', tableField: ['nome', 'id'], resultField: ['state_name', 'state_id'], where: { relation: '<>', type: 'integer', field: 'cod_uf', table: 'pnad_novo' }, join: { primary: 'id', foreign: 'cod_uf', foreignTable: 'pnad_novo' } }).addValue({ name: 'illiteracy', table: 'pnad_novo', tableField: 'analfabetismo', resultField: 'illiteracy', where: { relation: '=', type: 'integer', field: 'analfabetismo' } }).addValue({ name: 'years_of_study', table: 'pnad_novo', tableField: 'anos_de_estudo', resultField: 'years_of_study', where: { relation: '=', type: 'integer', field: 'anos_de_estudo' } }).addValue({ name: 'instruction_level', table: 'pnad_novo', tableField: 'nivel_de_instrucao', resultField: 'instruction_level', where: { relation: '=', type: 'integer', field: 'nivel_de_instrucao' } }).addValue({ name: 'adm_dependency', table: 'pnad_novo', tableField: 'dependencia_adm', resultField: 'adm_dependency', where: { relation: '=', type: 'integer', field: 'dependencia_adm' } }).addValue({ name: 'attends_school', table: 'pnad_novo', tableField: 'frequenta_escola', resultField: 'attends_school', where: { relation: '=', type: 'integer', field: 'frequenta_escola' } }).addValue({ name: 'modality', table: 'pnad_novo', tableField: 'modalidade', resultField: 'modality', where: { relation: '=', type: 'integer', field: 'modalidade' } }).addValue({ name: 'attended_modality', table: 'pnad_novo', tableField: 'nivel_etapa_modalidade_freq', resultField: 'attended_modality', where: { relation: '=', type: 'integer', field: 'nivel_etapa_modalidade_freq' } }).addValue({ name: 'bolsa_familia', table: 'pnad_novo', tableField: 'recebeu_rendimentos_de_programa_bolsa_familia', resultField: 'bolsa_familia', where: { relation: '=', type: 'integer', field: 'recebeu_rendimentos_de_programa_bolsa_familia' } }).addValue({ name: 'race', table: 'pnad_novo', tableField: 'cor_raca', resultField: 'race', where: { relation: '=', type: 'integer', field: 'cor_raca' } }).addValue({ name: 'income_range', table: 'pnad_novo', tableField: 'faixa_rendimento', resultField: 'income_range', where: { relation: '=', type: 'integer', field: 'faixa_rendimento' } }).addValue({ name: 'gender', table: 'pnad_novo', tableField: 'sexo', resultField: 'gender', where: { relation: '=', type: 'integer', field: 'sexo' } }).addValue({ name: 'cap_code', table: 'pnad_novo', tableField: 'cod_cap', resultField: 'cap_code', where: { relation: '=', type: 'integer', field: 'cod_cap' } }).addValue({ name: 'reg_code', table: 'pnad_novo', tableField: 'cod_regiao', resultField: 'reg_code', where: { relation: '=', type: 'integer', field: 'cod_regiao' } }).addValue({ name: 'metro_code', table: 'pnad_novo', tableField: 'cod_rm_ride', resultField: 'metro_code', where: { relation: '=', type: 'integer', field: 'cod_rm_ride' } }).addValue({ name: 'min_year', table: 'pnad_novo', tableField: 'ano_ref', resultField: 'year', where: { relation: '>=', type: 'integer', field: 'ano_ref' } }).addValue({ name: 'max_year', table: 'pnad_novo', tableField: '', resultField: 'year', where: { relation: '<=', type: 'integer', field: 'ano_ref' } }).addField({ name: 'search', field: false, where: true }).addValueToField({ name: 'name', table: 'pnad_novo', tableField: 'nome', where: { relation: 'LIKE', type: 'string', field: 'nome' } }, 'search').addValue({ name: 'mesoregion', table: 'pnad_novo', tableField: 'mesorregiao_id', where: { relation: '=', type: 'integer', field: 'mesorregiao_id' } }).addValue({ name: 'microregion', table: 'pnad_novo', tableField: 'microrregiao_id', where: { relation: '=', type: 'integer', field: 'microrregiao_id' } }).addValueToField({ name: 'region', table: 'estado', tableField: 'regiao_id', resultField: 'region_id', where: { relation: '=', type: 'integer', field: 'regiao_id', table: 'estado' }, join: { primary: 'id', foreign: 'estado_id', foreignTable: 'municipio' } }, 'filter'); PnadNovoApp.get('/', (req, res, next) => { req.sql.from('pnad_novo') .field('round(sum(pnad_novo.peso_domicilio_pessoas_com_cal), 0)', 'total') .field('pnad_novo.ano_ref', 'year') .group('pnad_novo.ano_ref') .order('pnad_novo.ano_ref') console.log(req.sql.toString()) next(); }, query, response('pnad_novo')); PnadNovoApp.get('/years', (req, res, next) => { req.sql.from('pnad_novo') .field('DISTINCT pnad_novo.ano_ref', 'year') next(); }, query, response('years')); module.exports = PnadNovoApp;