const express = require('express'); const idhmeApp = 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(); rqf.addField({ name: 'filter', field: false, where: true }).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: 'state', table: 'estado', tableField: 'nome', resultField: 'state_name', where: { relation: '=', type: 'integer', field: 'estado_id', table: 'adh_idh_uf' }, join: { primary: 'id', foreign: 'estado_id', foreignTable: 'adh_idh_uf' } }).addValue({ name: 'year', table: 'adh_idh', tableField: 'ano_censo', resultField: 'year', where: { relation: '=', type: 'integer', field: 'ano_censo', table: 'adh_idh' } }); idhmeApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { if(typeof req.filter === 'undefined' || Object.keys(req.filter).length === 0) { res.status(400); next({ status: 400, message: 'Wrong/No filter specified' }); } if ("state" in req.filter) { // console.log("sim"); req.sql.from('adh_idh_uf') .field('adh_idh_uf.idhm_e', 'IDHME') .field('adh_idh_uf.ano_censo', 'year') .field('adh_idh_uf.estado_id', 'state_id'); next(); } else if ("city" in req.filter) { req.sql.from('adh_idh') .field('adh_idh.idhm_e', 'IDHME') .field('adh_idh.ano_censo', 'year') .field('adh_idh.municipio_id', 'city_id'); next(); } }, query, response('idhme')); module.exports = idhmeApp;