From 6325779cb61613e4ba9285fd20519d5bb93c39fd Mon Sep 17 00:00:00 2001 From: Gabriel Ruschel <grc15@inf.ufpr.br> Date: Tue, 27 Jun 2017 11:47:07 -0300 Subject: [PATCH] Add idhm_e route --- src/libs/routes/api.js | 3 ++ src/libs/routes/idhme.js | 73 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 src/libs/routes/idhme.js diff --git a/src/libs/routes/api.js b/src/libs/routes/api.js index 2e7c9a3e..00ead593 100644 --- a/src/libs/routes/api.js +++ b/src/libs/routes/api.js @@ -30,6 +30,8 @@ const classroom = require('./classroom'); const teacher = require('./teacher'); +const idhme = require('./idhme'); + api.get('/', (req, res) => { res.json({ msg: 'SimCAQ API is running' }); }); @@ -46,5 +48,6 @@ api.use('/school', cache('15 day'), school); api.use('/spatial', cache('1 day'), spatial); api.use('/classroom', cache('15 day'), classroom); api.use('/teacher', cache('1 day'), teacher); +api.use('/idhme', cache('15 day'), idhme); module.exports = api; diff --git a/src/libs/routes/idhme.js b/src/libs/routes/idhme.js new file mode 100644 index 00000000..a33e360b --- /dev/null +++ b/src/libs/routes/idhme.js @@ -0,0 +1,73 @@ +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' + }, + join: { + primary: 'id', + foreign: 'estado_id', + foreignTable: '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' + }); + } + req.sql.from('adh_idh') + .field('adh_idh.idhm_e', 'IDHME') + .field('adh_idh.ano_censo', 'year') + .field('adh_idh.estado_id', 'state_id') + .field('adh_idh.municipio_id', 'city_id'); + next(); +}, query, response('idhme')); + +module.exports = idhmeApp; -- GitLab