diff --git a/src/libs/routes/api.js b/src/libs/routes/api.js index 0e9d294a0d7026e8f3b9dea4b51a7daabf549f3a..b4648cc44b8f43a3b0254b518f6e092335303171 100644 --- a/src/libs/routes/api.js +++ b/src/libs/routes/api.js @@ -36,6 +36,10 @@ const pibpercapita = require('./pibpercapita') const population = require('./population') +const idhm = require('./idhm'); + +const idhmr = require('./idhmr'); + api.get('/', (req, res) => { res.json({ msg: 'SimCAQ API is running' }); }); @@ -52,6 +56,8 @@ 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('/idhmr', cache('1 day'), idhmr); +api.use('/idhm', cache('1 day'), idhm); api.use('/idhme', cache('15 day'), idhme); api.use('/pibpercapita', cache('1 day'), pibpercapita); api.use('/population', cache('1 day'), population); diff --git a/src/libs/routes/idhm.js b/src/libs/routes/idhm.js new file mode 100644 index 0000000000000000000000000000000000000000..3181e67667e7e4ded45c0561b4795e6ce3bc5998 --- /dev/null +++ b/src/libs/routes/idhm.js @@ -0,0 +1,187 @@ +const express = require('express'); + +const idhmApp = 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(); + +idhmApp.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')); + +idhmApp.get('/IDHM_level', (req, res, next) => { + req.result = [ + {id: null, name: 'Não Classificado'}, + {id: 1, name: 'Muito Baixa'}, + {id: 2, name: 'Baixo'}, + {id: 3, name: 'Médio'}, + {id: 4, name: 'Alto'}, + {id: 5, name: 'Muito Alto'} + ]; + next(); +}, response('IDHM_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: '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: '@' + }, + join: { + primary: 'id', + foreign: 'estado_id', + foreignTable: '@' + } +}).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: 'IDHM_level', + table: '@', + tableField: 'idhm_nivel', + resultField: 'idhm_nivel_id', + where: { + relation: '=', + type: 'integer', + table: '@', + field: 'idhm_nivel' + } +}); + + +idhmApp.get('/', rqf.parse(), (req, res, next) => { + log.debug(req.sql.toParam()); + if((Object.keys(req.filter).length === 0) && (Object.keys(req.dims).length === 0)) { + res.status(400); + next({ + status: 400, + message: 'Wrong/No filter specified' + }); + } + if ("state" in req.filter) { + console.log("BBBBB"); + req.sql.from('adh_idh_uf') + .field('adh_idh_uf.idhm', 'total') + .field('adh_idh_uf.ano_censo', 'year') + .field('adh_idh_uf.estado_id', 'state_id') + .group('adh_idh_uf.idhm') + .group('adh_idh_uf.ano_censo') + .group('adh_idh_uf.estado_id') + } else if ("state" in req.dims) { + console.log("AAAAAAAAA"); + req.sql.from('adh_idh') + .field('adh_idh.idhm', 'total') + .field('adh_idh.ano_censo', 'year') + .field('adh_idh.municipio_id', 'city_id') + .field('adh_idh.estado_id', 'state_id') + .group('adh_idh.idhm') + .group('adh_idh.ano_censo') + .group('adh_idh.municipio_id') + .group('adh_idh.estado_id') + } else if ("city" in req.filter) { + req.sql.from('adh_idh') + .field('adh_idh.idhm', 'total') + .field('adh_idh.ano_censo', 'year') + .field('adh_idh.municipio_id', 'city_id') + .group('adh_idh.idhm') + .group('adh_idh.ano_censo') + .group('adh_idh.municipio_id') + } else if ("city" in req.dims) { + req.sql.from('adh_idh') + .field('adh_idh.idhm', 'total') + .field('adh_idh.ano_censo', 'year') + .field('adh_idh.municipio_id', 'city_id') + .group('adh_idh.idhm') + .group('adh_idh.ano_censo') + .group('adh_idh.municipio_id') + .group('adh_idh.estado_id') + } else { + next({ + status: 400, + message: 'Wrong/No filter specified' + }); + } + next(); +}, rqf.build(),query, response('idhm')); + +module.exports = idhmApp; diff --git a/src/libs/routes/idhmr.js b/src/libs/routes/idhmr.js new file mode 100644 index 0000000000000000000000000000000000000000..48973ec6c32cae821528dae3f79f19d504a316d8 --- /dev/null +++ b/src/libs/routes/idhmr.js @@ -0,0 +1,137 @@ +const express = require('express'); + +const idhmrApp = 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(); + +idhmrApp.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')); + +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: '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: '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' + } +}); + +idhmrApp.get('/', rqf.parse(), (req, res, next) => { + log.debug(req.sql.toParam()); + 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) { + req.sql.from('adh_idh_uf') + .field('adh_idh_uf.idhm_r', 'total') + .field('adh_idh_uf.ano_censo', 'year') + .field('adh_idh_uf.estado_id', 'state_id'); + } else if ("city" in req.filter) { + req.sql.from('adh_idh') + .field('adh_idh.idhm_r', 'total') + .field('adh_idh.ano_censo', 'year') + .field('adh_idh.municipio_id', 'city_id'); + } else { + next({ + status: 400, + message: 'Wrong/No filter specified' + }); + } + + next(); +}, rqf.build(),query, response('idhmr')); + +module.exports = idhmrApp; diff --git a/src/test/idhm.js b/src/test/idhm.js new file mode 100644 index 0000000000000000000000000000000000000000..210d58187ebd4d3a5860a92ec2ea081a7f6f1e33 --- /dev/null +++ b/src/test/idhm.js @@ -0,0 +1,80 @@ +process.env.NODE_ENV = 'test'; + +const chai = require('chai'); + +const dirtyChai = require('dirty-chai'); + +chai.use(dirtyChai); + +const chaiXml = require('chai-xml'); + +chai.use(chaiXml); + +const chaiHttp = require('chai-http'); + +const assert = chai.assert; + +const expect = chai.expect; + +const should = chai.should(); // actually call the function + +const libs = `${process.cwd()}/libs`; + +const server = require(`${libs}/app`); + +chai.use(chaiHttp); +describe('request idhm', () => { + it('should list the year range', (done) => { + chai.request(server) + .get('/api/v1/idhm/year_range') + .end((err, res) => { + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property('result'); + res.body.result.should.be.a('array'); + res.body.result[0].should.have.property('start_year'); + res.body.result[0].should.have.property('end_year'); + done(); + }); + }); + + it('should list idhm with valid filters', (done) => { + chai.request(server) + .get('/api/v1/idhm?filter=min_year:2000,state:41') + .end((err, res) => { + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property('result'); + res.body.result.should.be.a('array'); + res.body.result[0].should.have.property('total'); + res.body.result[0].should.have.property('year'); + res.body.result[0].should.have.property('state_id'); + done(); + }); + }); + + it('should list idhm with invalid filters', (done) => { + chai.request(server) + .get('/api/v1/idhm?filter=foo:2010,bar:41') + .end((err, res) => { + res.should.have.status(400); + res.should.be.json; + res.body.should.have.property('error'); + res.body.error.should.be.equal('Wrong/No filter specified'); + done(); + }); + }); + + it('should return 400 with no filters', (done) => { + chai.request(server) + .get('/api/v1/idhm') + .end((err, res) => { + res.should.have.status(400); + res.should.be.json; + res.body.should.have.property('error'); + res.body.error.should.be.equal('Wrong/No filter specified'); + done(); + }) + }); + +}); diff --git a/src/test/idhmr.js b/src/test/idhmr.js new file mode 100644 index 0000000000000000000000000000000000000000..64ac892f21e51aa6c24e8f069d486b2afbb8d51a --- /dev/null +++ b/src/test/idhmr.js @@ -0,0 +1,80 @@ +process.env.NODE_ENV = 'test'; + +const chai = require('chai'); + +const dirtyChai = require('dirty-chai'); + +chai.use(dirtyChai); + +const chaiXml = require('chai-xml'); + +chai.use(chaiXml); + +const chaiHttp = require('chai-http'); + +const assert = chai.assert; + +const expect = chai.expect; + +const should = chai.should(); // actually call the function + +const libs = `${process.cwd()}/libs`; + +const server = require(`${libs}/app`); + +chai.use(chaiHttp); +describe('request idhmr', () => { + it('should list the year range', (done) => { + chai.request(server) + .get('/api/v1/idhmr/year_range') + .end((err, res) => { + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property('result'); + res.body.result.should.be.a('array'); + res.body.result[0].should.have.property('start_year'); + res.body.result[0].should.have.property('end_year'); + done(); + }); + }); + + it('should list idhmr with valid filters', (done) => { + chai.request(server) + .get('/api/v1/idhmr?filter=min_year:2000,state:41') + .end((err, res) => { + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property('result'); + res.body.result.should.be.a('array'); + res.body.result[0].should.have.property('total'); + res.body.result[0].should.have.property('year'); + res.body.result[0].should.have.property('state_id'); + done(); + }); + }); + + it('should list idhmr with invalid filters', (done) => { + chai.request(server) + .get('/api/v1/idhmr?filter=foo:2010,bar:41') + .end((err, res) => { + res.should.have.status(400); + res.should.be.json; + res.body.should.have.property('error'); + res.body.error.should.be.equal('Wrong/No filter specified'); + done(); + }); + }); + + it('should return 400 with no filters', (done) => { + chai.request(server) + .get('/api/v1/idhmr') + .end((err, res) => { + res.should.have.status(400); + res.should.be.json; + res.body.should.have.property('error'); + res.body.error.should.be.equal('Wrong/No filter specified'); + done(); + }) + }); + +});