diff --git a/src/libs/convert/stateName.js b/src/libs/convert/stateName.js new file mode 100644 index 0000000000000000000000000000000000000000..3fdfdba4cf8ad67cda3b922a4ffd5f43e1e96011 --- /dev/null +++ b/src/libs/convert/stateName.js @@ -0,0 +1,58 @@ +module.exports = function stateName(id) { + switch (id) { + case 11: + return 'Rondônia'; + case 12: + return 'Acre'; + case 13: + return 'Amazonas'; + case 14: + return 'Roraima'; + case 15: + return 'Pará'; + case 16: + return 'Amapá'; + case 17: + return 'Tocantins'; + case 21: + return 'Maranhão'; + case 22: + return'PiauÃ'; + case 23: + return 'Ceará'; + case 24: + return 'Rio Grande do Norte'; + case 25: + return 'ParaÃba'; + case 26: + return 'Pernambuco'; + case 27: + return 'Alagoas'; + case 28: + return 'Sergipe'; + case 29: + return 'Bahia'; + case 31: + return 'Minas Gerais'; + case 32: + return 'EspÃrito Santo'; + case 33: + return 'Rio de Janeiro '; + case 35: + return 'São Paulo'; + case 41: + return 'Paraná'; + case 42: + return 'Santa Catarina'; + case 43: + return 'Rio Grande do Sul'; + case 50: + return 'Mato Grosso do Sul'; + case 51: + return 'Mato Grosso'; + case 52: + return 'Goiás'; + case 53: + return 'Distrito Federal'; + } +}; diff --git a/src/libs/middlewares/id2str.js b/src/libs/middlewares/id2str.js index 8bde6b4a6b4865f482ee45a8c8cd78bd566caa99..99c03e2d5a75c4a52f6ededc93c3d2e6904719df 100644 --- a/src/libs/middlewares/id2str.js +++ b/src/libs/middlewares/id2str.js @@ -16,6 +16,7 @@ const educationType = require(`${libs}/convert/educationType`); const citySize = require(`${libs}/convert/citySize`); const incomeLevel = require(`${libs}/convert/incomeLevel`); const idhmLevel = require(`${libs}/convert/idhmLevel`); +const stateName = require(`${libs}/convert/stateName`); const ids = { gender_id: gender, @@ -63,7 +64,8 @@ const ids = { education_type_id: educationType, income_level_id: incomeLevel, city_size_id: citySize, - idhm_level_id: idhmLevel + idhm_level_id: idhmLevel, + state_id: stateName }; function transform(removeId=false) { diff --git a/src/libs/routes/idhm.js b/src/libs/routes/idhm.js index 16f225a7c9aa0d97d869d3e2e300f2dabc0356cd..51620254d30551f846fe735bd15a8d6f99a1a3fb 100644 --- a/src/libs/routes/idhm.js +++ b/src/libs/routes/idhm.js @@ -150,13 +150,6 @@ rqf.addField({ 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 && !("city" in req.filter)) { req.sql.from('adh_idh_uf') .field('adh_idh_uf.idhm', 'total') .field('adh_idh_uf.ano_censo', 'year') @@ -164,21 +157,32 @@ idhmApp.get('/', rqf.parse(), (req, res, next) => { .group('adh_idh_uf.idhm') .group('adh_idh_uf.ano_censo') .group('adh_idh_uf.estado_id') - } else if ("city" in req.filter) { + } else if ("state" in req.filter && !("city" in req.filter) && !("city" in req.dims)) { + 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 ("city" in req.filter || "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') - .field('adh_idh.estado_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 { - next({ - status: 400, - message: 'Wrong/No filter specified' - }); + 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') } next(); }, rqf.build(), query, id2str.transform(), response('idhm')); diff --git a/src/libs/routes/idhmr.js b/src/libs/routes/idhmr.js index c8a0cba078bfc420973438f230209534d495f715..df8847bcd1d6e588044e5920d9796ebe3b783d39 100644 --- a/src/libs/routes/idhmr.js +++ b/src/libs/routes/idhmr.js @@ -126,31 +126,42 @@ rqf.addField({ 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 && !("city" 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) { + .field('adh_idh_uf.estado_id', 'state_id') + .group('adh_idh_uf.idhm_r') + .group('adh_idh_uf.ano_censo') + .group('adh_idh_uf.estado_id') + } else if ("state" in req.filter && !("city" in req.filter) && !("city" in req.dims)) { + 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') + .group('adh_idh_uf.idhm_r') + .group('adh_idh_uf.ano_censo') + .group('adh_idh_uf.estado_id') + } else if ("city" in req.filter || "city" in req.dims) { req.sql.from('adh_idh') .field('adh_idh.idhm_r', 'total') .field('adh_idh.ano_censo', 'year') .field('adh_idh.municipio_id', 'city_id') - .field('adh_idh.estado_id', 'estado_id'); + .field('adh_idh.estado_id', 'state_id') + .group('adh_idh.idhm_r') + .group('adh_idh.ano_censo') + .group('adh_idh.municipio_id') + .group('adh_idh.estado_id') } else { - next({ - status: 400, - message: 'Wrong/No filter specified' - }); + 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') + .group('adh_idh_uf.idhm_r') + .group('adh_idh_uf.ano_censo') + .group('adh_idh_uf.estado_id') } next(); -}, rqf.build(),query, response('idhmr')); +}, rqf.build(),query, id2str.transform(), response('idhmr')); module.exports = idhmrApp; diff --git a/src/test/idhm.js b/src/test/idhm.js index 210d58187ebd4d3a5860a92ec2ea081a7f6f1e33..3b6eae6b5d655115f7faca5d0fe27898327d4d44 100644 --- a/src/test/idhm.js +++ b/src/test/idhm.js @@ -49,30 +49,43 @@ describe('request idhm', () => { 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'); + res.body.result[0].should.have.property('state_name'); done(); }); }); - it('should list idhm with invalid filters', (done) => { + it('should list idhm with valid dims', (done) => { chai.request(server) - .get('/api/v1/idhm?filter=foo:2010,bar:41') + .get('/api/v1/idhm?dims=city') .end((err, res) => { - res.should.have.status(400); + res.should.have.status(200); res.should.be.json; - res.body.should.have.property('error'); - res.body.error.should.be.equal('Wrong/No filter specified'); + 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('city_id'); + res.body.result[0].should.have.property('state_id'); + res.body.result[0].should.have.property('city_name'); + res.body.result[0].should.have.property('state_name'); done(); }); }); - it('should return 400 with no filters', (done) => { + it('should list idhm with valid filtes and dims', (done) => { chai.request(server) - .get('/api/v1/idhm') + .get('/api/v1/idhm?filter=state:41&dims=idhm_level') .end((err, res) => { - res.should.have.status(400); + res.should.have.status(200); res.should.be.json; - res.body.should.have.property('error'); - res.body.error.should.be.equal('Wrong/No filter specified'); + 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'); + res.body.result[0].should.have.property('idhm_level_id'); + res.body.result[0].should.have.property('state_name'); + res.body.result[0].should.have.property('idhm_level_name'); done(); }) }); diff --git a/src/test/idhmr.js b/src/test/idhmr.js index 64ac892f21e51aa6c24e8f069d486b2afbb8d51a..3a7370658adc8d9313739dc60f5f776a7c7def46 100644 --- a/src/test/idhmr.js +++ b/src/test/idhmr.js @@ -24,57 +24,68 @@ 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(); - }) - }); + 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'); + res.body.result[0].should.have.property('state_name'); + done(); + }); + }); + + it('should list idhmr with valid dims', (done) => { + chai.request(server) + .get('/api/v1/idhmr?dims=state') + .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'); + res.body.result[0].should.have.property('state_name'); + done(); + }); + }); + + it('should list idhmr with valid filtes and dims', (done) => { + chai.request(server) + .get('/api/v1/idhm?filter=state:41&dims=city') + .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('city_id'); + res.body.result[0].should.have.property('state_id'); + res.body.result[0].should.have.property('city_name'); + res.body.result[0].should.have.property('state_name'); + done(); + }) + }); });