diff --git a/src/libs/routes/idhme.js b/src/libs/routes/idhme.js index 2a68bc9aedf20edaa01f7974dc6c8fd57e1517d7..9667d5b2151061f9badce0dc7ce194fdd1f2eba2 100644 --- a/src/libs/routes/idhme.js +++ b/src/libs/routes/idhme.js @@ -61,6 +61,10 @@ rqf.addField({ name: 'filter', field: false, where: true +}).addField({ + name: 'dims', + field: true, + where: false }).addValue({ name: 'city', table: 'municipio', @@ -118,33 +122,25 @@ rqf.addField({ }); idhmeApp.get('/', rqf.parse(), (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) && !("city" in req.filter)) { - next({ - status: 400, - message: 'Wrong/No filter specified' - }); - } - if ("city" in req.filter) { + if (("city" in req.dims) || ("city" in req.filter)) { req.sql.from('adh_idh') .field('adh_idh.idhm_e', 'total') .field('adh_idh.ano_censo', 'year') - .field('adh_idh.municipio_id', 'city_id'); + .field('adh_idh.municipio_id', 'city_id') + .group('adh_idh.idhm_e') + .group('adh_idh.ano_censo') + .group('adh_idh.municipio_id'); } else { - // console.log("sim"); req.sql.from('adh_idh_uf') .field('adh_idh_uf.idhm_e', 'total') .field('adh_idh_uf.ano_censo', 'year') - .field('adh_idh_uf.estado_id', 'state_id'); + .field('adh_idh_uf.estado_id', 'state_id') + .group('adh_idh_uf.idhm_e') + .group('adh_idh_uf.ano_censo') + .group('adh_idh_uf.estado_id'); } next(); -}, rqf.build(), query, response('idhme')); +}, rqf.build(), query, id2str.transform(), response('idhme')); module.exports = idhmeApp; diff --git a/src/libs/routes/idhml.js b/src/libs/routes/idhml.js index 5ba3e12b704a0eb3ac3ae7c5119dcdf0093db21d..be1ee7a3b7a0b00b7d914d0a9fe1d480034beae7 100644 --- a/src/libs/routes/idhml.js +++ b/src/libs/routes/idhml.js @@ -61,6 +61,10 @@ rqf.addField({ name: 'filter', field: false, where: true +}).addField({ + name: 'dims', + field: true, + where: false }).addValue({ name: 'city', table: 'municipio', @@ -118,32 +122,25 @@ rqf.addField({ }); idhmlApp.get('/', rqf.parse(), (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) && !("city" in req.filter)) { - next({ - status: 400, - message: 'Wrong/No filter specified' - }); - } - if ("city" in req.filter) { + + if (("city" in req.dims) || ("city" in req.filter)) { req.sql.from('adh_idh') .field('adh_idh.idhm_l', 'total') .field('adh_idh.ano_censo', 'year') - .field('adh_idh.municipio_id', 'city_id'); + .field('adh_idh.municipio_id', 'city_id') + .group('adh_idh.idhm_l') + .group('adh_idh.ano_censo') + .group('adh_idh.municipio_id'); } else { - // console.log("sim"); req.sql.from('adh_idh_uf') .field('adh_idh_uf.idhm_l', 'total') .field('adh_idh_uf.ano_censo', 'year') - .field('adh_idh_uf.estado_id', 'state_id'); + .field('adh_idh_uf.estado_id', 'state_id') + .group('adh_idh_uf.idhm_l') + .group('adh_idh_uf.ano_censo') + .group('adh_idh_uf.estado_id'); } next(); -}, rqf.build(), query, response('idhml')); +}, rqf.build(), query, id2str.transform(), response('idhme')); module.exports = idhmlApp; diff --git a/src/libs/routes/school.js b/src/libs/routes/school.js index fce424982290c9fa7b5847d6dae8c0937f9c7e2d..40526cffc5947dda85b65c1c0fa567376c42c706 100644 --- a/src/libs/routes/school.js +++ b/src/libs/routes/school.js @@ -479,33 +479,23 @@ rqfCount.addField({ } }).addValue({ name: 'adm_dependency', - table: 'dependencia_adm', - tableField: 'nome', - resultField: 'adm_dependency_name', + table: 'escola', + tableField: 'dependencia_adm_id', + resultField: 'adm_dependency_id', where: { relation: '=', type: 'integer', - field: 'id' - }, - join: { - primary: 'id', - foreign: 'dependencia_adm_id', - foreignTable: 'escola' + field: 'dependencia_adm_id' } }).addValue({ name: 'adm_dependency_detailed', - table: 'dependencia_adm_priv', - tableField: 'nome', - resultField: 'adm_dependency_detailed_name', + table: 'escola', + tableField: 'dependencia_adm_priv', + resultField: 'adm_dependency_detailed_id', where: { relation: '=', type: 'integer', - field: 'id' - }, - join: { - primary: 'id', - foreign: 'dependencia_adm_priv', - foreignTable: 'escola' + field: 'dependencia_adm_priv' } }).addValue({ name: 'location', diff --git a/src/test/idhme.js b/src/test/idhme.js index 285160819ada0b77cde946c97ba56ffa0bcc48fd..ce3a83bb99ed06d73caf6f02873adcee40afb3be 100644 --- a/src/test/idhme.js +++ b/src/test/idhme.js @@ -38,6 +38,19 @@ describe('request idhme', () => { }); }); + it('should list the available years', (done) => { + chai.request(server) + .get('/api/v1/idhme/years') + .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('year'); + done(); + }); + }); + it('should list idhme with valid filters', (done) => { chai.request(server) .get('/api/v1/idhme?filter=min_year:2000,state:41') @@ -57,24 +70,61 @@ describe('request idhme', () => { chai.request(server) .get('/api/v1/idhme?filter=foo:2010,bar:41') .end((err, res) => { - res.should.have.status(400); + res.should.have.status(200); + res.should.be.json; + 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 idhme with valid dimensions', (done) => { + chai.request(server) + .get('/api/v1/idhme?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 idhme with valid filters and dimensions', (done) => { + chai.request(server) + .get('/api/v1/idhme?filter=state:41,min_year:2010&dims=city') + .end((err, res) => { + 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('city_name'); done(); }); }); - it('should return 400 with no filters', (done) => { + it('should list idhme from all states with all years available with no argument dimensions and filters', (done) => { chai.request(server) .get('/api/v1/idhme') .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'); done(); - }) + }); }); }); diff --git a/src/test/idhml.js b/src/test/idhml.js index c3409bb45bb7fb97c95567847b837a9d153cb9ae..b4ece764162c2caf34ff1aaa6ec4f0b9c11fd66f 100644 --- a/src/test/idhml.js +++ b/src/test/idhml.js @@ -38,6 +38,19 @@ describe('request idhml', () => { }); }); + it('should list the available years', (done) => { + chai.request(server) + .get('/api/v1/idhml/years') + .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('year'); + done(); + }); + }); + it('should list idhml with valid filters', (done) => { chai.request(server) .get('/api/v1/idhml?filter=min_year:2000,state:41') @@ -57,24 +70,61 @@ describe('request idhml', () => { chai.request(server) .get('/api/v1/idhml?filter=foo:2010,bar:41') .end((err, res) => { - res.should.have.status(400); + res.should.have.status(200); + res.should.be.json; + 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 idhml with valid dimensions', (done) => { + chai.request(server) + .get('/api/v1/idhml?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 idhml with valid filters and dimensions', (done) => { + chai.request(server) + .get('/api/v1/idhml?filter=state:41,min_year:2010&dims=city') + .end((err, res) => { + 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('city_name'); done(); }); }); - it('should return 400 with no filters', (done) => { + it('should list idhml from all states with all years available with no argument dimensions and filters', (done) => { chai.request(server) .get('/api/v1/idhml') .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'); done(); - }) + }); }); });