diff --git a/src/libs/app.js b/src/libs/app.js index c16c805f3ff2ac360d62fcdf3d771bcd5c5e366c..4e3e51c6bf3d216a519ce5a33e16b45941c75040 100644 --- a/src/libs/app.js +++ b/src/libs/app.js @@ -4,17 +4,20 @@ const bodyParser = require('body-parser'); const methodOverride = require('method-override'); const cors = require('cors'); const compression = require('compression'); +const squel = require('squel'); -const log = require('./log')(module); +const libs = `${process.cwd()}/libs`; -const config = require('./config'); +const log = require(`${libs}/log`)(module); + +const config = require(`${libs}/config`); const cache = require('apicache').options({ debug: config.get('debug') }).middleware; const app = express(); -const api = require('./routes/api'); +const api = require(`${libs}/routes/api`); -const mongoose = require('./db/mongoose'); +const mongoose = require(`${libs}/db/mongoose`); const db = mongoose(); @@ -32,7 +35,12 @@ app.use(methodOverride()); app.use(cache('1 day')); // Enable maximum compression app.use(compression(9)); -app.use(api); +// Middleware tha adds the squel object to req +app.use((req, res, next) => { + req.sql = squel.select(); + next(); +}); +app.use('/api/v1', api); // Catch 404 and forward to error handler app.use((req, res, next) => { diff --git a/src/libs/log.js b/src/libs/log.js index e71edf141bd434ae749725ef5298e200c1105e7e..ee975b8dd3d75720cf5937642f98b01af4673e66 100644 --- a/src/libs/log.js +++ b/src/libs/log.js @@ -1,4 +1,5 @@ -const config = require('./config'); +const libs = `${process.cwd()}/libs`; +const config = require(`${libs}/config`); const winston = require('winston'); @@ -46,9 +47,7 @@ function logger(module) { ], exitOnError: false, }); - const debugMode = (typeof config.get('debug') === 'undefined') ? - config.get('debug') : false; - if (!debugMode) { + if (!config.get('debug')) { log.remove('debug-log'); } return log; diff --git a/src/libs/middlewares/dimensions.js b/src/libs/middlewares/dimensions.js deleted file mode 100644 index 6d66e901fa835845e304b79ae54a398afc0103da..0000000000000000000000000000000000000000 --- a/src/libs/middlewares/dimensions.js +++ /dev/null @@ -1,52 +0,0 @@ - -// This function returns the intersection of two arrays -function intersect(a, b) { - let t; - if (b.length > a.length) { - t = b; b = a; a = t; - } - return a.filter((e) => b.indexOf(e) !== -1); -} - -// Dimensions middleware -// ``` -// EXAMPLE: -// Use it with no parameters to get all the dimensions specified -// app.get('/', dimensions(), function(req, res, next){}) -// -// Use it with an array of accepted values -// app.get('/', dimensions(['year', 'location']), function(req, res, next){}) -// -// Use it globally -// app.use(dimensions()) -// ``` -function dimensions(dims) { - return (req, res, next) => { - req.dims = {}; - if (req.query.dims) { - // Split the string and get all dimensions. - // Example string: 'state:41,year:2013,urban' - const params = req.query.dims.split(','); - const dimObj = {}; - // For each dimension in the array see if there is a value associated - for (const param of params) { - const kv = param.split(':'); - dimObj[kv[0]] = (typeof kv[1] === 'undefined') ? null : kv[1]; - } - - // If the dims array exists and is not empty - if (typeof dims !== 'undefined' && dims.length > 0) { - // Intersect the two arrays - const intersection = intersect(dims, Object.keys(dimObj)); - for (let i = 0; i < intersection.length; ++i) { - req.dims[intersection[i]] = dimObj[intersection[i]]; - } - } else { - req.dims = dimObj; - } - } - next(); - }; -} - -module.exports = dimensions; diff --git a/src/libs/middlewares/parseParams.js b/src/libs/middlewares/parseParams.js new file mode 100644 index 0000000000000000000000000000000000000000..c74b6b491bc445b22cd480c85d9f232494a139de --- /dev/null +++ b/src/libs/middlewares/parseParams.js @@ -0,0 +1,66 @@ +/** +* ParseParams middleware +* +* EXAMPLE: +* Use it with no parameters to get all the params specified +* app.get('/', parseParams('dims'), function(req, res, next){}) +* +* Use it with an array of accepted values +* app.get('/', parseParams('filter', ['year', 'location']), function(req, res, next){}) +* +* Use it globally +* app.use(parseParams('dims')) +*/ + +const libs = `${process.cwd()}/libs`; + +const log = require(`${libs}/log`)(module); + + // This function returns the intersection of two arrays +function intersect(a, b) { + let t; + if (b.length > a.length) { + t = b; b = a; a = t; + } + return a.filter((e) => b.indexOf(e) !== -1); +} + +function parseParams(queryParam, arr) { + return (req, res, next) => { + req[queryParam] = {}; + if (req.query[queryParam]) { + const params = req.query[queryParam].split(','); + // Temporary object to hold the params and it's values + const obj = {}; + for (const param of params) { + // Get the key and the value - state:41 is key 'state' whith value 41 + const kv = param.split(':'); + // Check if there is a value. If there isn't, assign null + obj[kv[0]] = (typeof kv[1] === 'undefined') ? null : kv[1]; + } + + // If the array exists and is not empty we intersect + if (typeof arr !== 'undefined' && arr.length > 0) { + // Intersect the keys of the obj with the array arr. + // The intersection array is assigned with the keys + const intersection = intersect(arr, Object.keys(obj)); + // This is a bit tricky... + // For each key in the intersection array we get it's value in the obj + // and assign it to the custom attribute in the req obj. + // For example: instersection => ["state"] so + // obj[intersection[i]] (with i=0) is obj["state"], that is 41 + // and req[queryParam]["state"] = 41 + for (let i = 0; i < intersection.length; ++i) { + req[queryParam][intersection[i]] = obj[intersection[i]]; + } + req[queryParam].size = intersection.length; + } else { + req[queryParam] = obj; + req[queryParam].size = Object.keys(obj).length; + } + } + next(); + }; +} + +module.exports = parseParams; diff --git a/src/libs/middlewares/query.js b/src/libs/middlewares/query.js index a3435dec4c2d2f37a167b6749e1c6aea0b9d1368..a4f20e3ba5bdd31cf7dab85a44121b8e8e456fc0 100644 --- a/src/libs/middlewares/query.js +++ b/src/libs/middlewares/query.js @@ -4,8 +4,9 @@ const execQuery = require(`${libs}/db/query_exec`); // Middleware that executes a query defined by a squel object in req.sql function query(req, res, next) { - log.debug(req.sql); - execQuery(req.sql.text, req.sql.values).then((result) => { + let sql = req.sql.toParam(); + log.debug(sql); + execQuery(sql.text, sql.values).then((result) => { log.debug(result); req.result = result; next(); diff --git a/src/libs/routes/api.js b/src/libs/routes/api.js index 914abaa77d6c4acaca36fb07a7f1c9eeeeddfbc6..3faaa926c7055bc9849d4457e183e8ea6fd48cf8 100644 --- a/src/libs/routes/api.js +++ b/src/libs/routes/api.js @@ -12,15 +12,15 @@ const city = require('./city'); const school = require('./school'); -// API status route -api.get('/api/v1', (req, res) => { +api.get('/', (req, res) => { res.json({ msg: 'SimCAQ API is running' }); }); -api.use('/api/v1/enrollment', enrollment); -api.use('/api/v1/state', state); -api.use('/api/v1/region', region); -api.use('/api/v1/city', city); -api.use('/api/v1/school', school); +// mount API routes +api.use('/enrollment', enrollment); +api.use('/state', state); +api.use('/region', region); +api.use('/city', city); +api.use('/school', school); module.exports = api; diff --git a/src/libs/routes/city.js b/src/libs/routes/city.js index cb55e2209d795ac359a287a392a6df1a00f6438f..733da6f341cc37a7b6b792b8f5383076eae75067 100644 --- a/src/libs/routes/city.js +++ b/src/libs/routes/city.js @@ -1,6 +1,6 @@ const express = require('express'); -const cityApp = express(); +const cityApp = express.Router(); const libs = `${process.cwd()}/libs`; @@ -12,28 +12,28 @@ const response = require(`${libs}/middlewares/response`); // Return all cities cityApp.get('/', (req, res, next) => { - req.sql = squel.select().from('municipios').toParam(); + req.sql.from('municipios'); next(); }, query, response('city')); // Return a specific city by it's id cityApp.get('/:id', (req, res, next) => { - req.sql = squel.select().from('municipios').where('pk_municipio_id = ?', - parseInt(req.params.id, 10)).toParam(); + req.sql.from('municipios') + .where('pk_municipio_id = ?', parseInt(req.params.id, 10)); next(); }, query, response('city')); // Return a specific city by it's IBGE code cityApp.get('/ibge/:id', (req, res, next) => { - req.sql = squel.select().from('municipios').where('codigo_ibge = ?', - req.params.id).toParam(); + req.sql.from('municipios') + .where('codigo_ibge = ?', req.params.id); next(); }, query, response('city')); // Return all the cities from a specific state cityApp.get('/state/:id', (req, res, next) => { - req.sql = squel.select().from('municipios').where('fk_estado_id = ?', - parseInt(req.params.id, 10)).toParam(); + req.sql.from('municipios') + .where('fk_estado_id = ?', parseInt(req.params.id, 10)); next(); }, query, response('city')); diff --git a/src/libs/routes/enrollment.js b/src/libs/routes/enrollment.js index 6b37b35c4af4983329221759e98ba43989ca3956..a73f203fe0cdbf8c5cbd4300541f3d2383cfec9d 100644 --- a/src/libs/routes/enrollment.js +++ b/src/libs/routes/enrollment.js @@ -1,6 +1,6 @@ const express = require('express'); -const enrollmentApp = express(); +const enrollmentApp = express.Router(); const libs = `${process.cwd()}/libs`; @@ -12,37 +12,18 @@ const query = require(`${libs}/middlewares/query`); const response = require(`${libs}/middlewares/response`); -// **Temporary** solution to add where clauses that are common to all requests -function filter(req, q) { - if (typeof req.min_year !== 'undefined') { - q.where('ano_censo>=?', req.min_year); - } +const parseParams = require(`${libs}/middlewares/parseParams`); - if (typeof req.max_year !== 'undefined') { - q.where('ano_censo<=?', req.max_year); - } - - if (typeof req.adm_dependency_id !== 'undefined') { - q.where('fk_dependencia_adm_id=?', req.adm_dependency_id); - } - - if (typeof req.location_id !== 'undefined') { - q.where('id_localizacao=?', req.location_id); - } +// **Temporary** solution to add where clauses that are common to all requests - if (typeof req.education_level_id !== 'undefined') { - q.where('fk_etapa_ensino_id=?', req.education_level_id); - } -} // Complete range of the enrollments dataset. // Returns a tuple of start and ending years of the complete enrollments dataset. enrollmentApp.get('/year_range', (req, res, next) => { req.sql = squel.select() - .from('turmas') - .field('MIN(turmas.ano_censo)', 'start_year') - .field('MAX(turmas.ano_censo)', 'end_year') - .toParam(); + .from('turmas') + .field('MIN(turmas.ano_censo)', 'start_year') + .field('MAX(turmas.ano_censo)', 'end_year'); next(); }, query, response('range')); @@ -50,10 +31,9 @@ enrollmentApp.get('/year_range', (req, res, next) => { // Returns all educational levels avaible enrollmentApp.get('/education_level', (req, res, next) => { req.sql = squel.select() - .from('etapa_ensino') - .field('pk_etapa_ensino_id', 'id') - .field('desc_etapa', 'name') - .toParam(); + .from('etapa_ensino') + .field('pk_etapa_ensino_id', 'id') + .field('desc_etapa', 'name'); next(); }, query, response('education_level')); @@ -61,150 +41,178 @@ enrollmentApp.get('/education_level', (req, res, next) => { // Returns all adm dependencies enrollmentApp.get('/adm_dependency', (req, res, next) => { req.sql = squel.select() - .from('dependencia_adms') - .field('pk_dependencia_adm_id', 'id') - .field('nome', 'name') - .toParam(); + .from('dependencia_adms') + .field('pk_dependencia_adm_id', 'id') + .field('nome', 'name'); next(); }, query, response('adm_dependency')); enrollmentApp.get('/data', (req, res, next) => { - req.sql = squel.select().from('turmas').toParam(); + req.sql = squel.select().from('turmas'); next(); }, query, response('data')); -enrollmentApp.use('/', (req, res, next) => { - const params = req.query; - req.paramCnt = 0; +// Parse the filters and dimensions parameter in the query +enrollmentApp.use('/', parseParams('filter', [ + 'min_year', + 'max_year', + 'adm_dependency_id', + 'location_id', + 'education_level_id', + 'region', + 'state', + 'city', + 'school' +]), parseParams('dims', [ + 'adm_dependency_id', + 'location_id', + 'education_level_id', + 'region', + 'state', + 'city', + 'school' +]), (req, res, next) => { + log.debug(req.filter); + log.debug(req.dims); - if (typeof params.id !== 'undefined') { - req.id = parseInt(params.id, 10); - req.paramCnt += 1; + // Do the joins + if(typeof req.filter.adm_dependency_id !== 'undefined' + || typeof req.dims.adm_dependency_id !== 'undefined') { + req.sql.join('dependencia_adms', null, 'fk_dependencia_adm_id=dependencia_adms.pk_dependencia_adm_id'); } - if (typeof params.location_id !== 'undefined') { - req.location_id = parseInt(params.location_id, 10); - req.paramCnt += 1; + if(typeof req.filter.education_level_id !== 'undefined' + || typeof req.dims.education_level_id !== 'undefined') { + req.sql.join('etapa_ensino', null, 'fk_etapa_ensino_id=etapa_ensino.pk_etapa_ensino_id'); } - if (typeof params.adm_dependency_id !== 'undefined') { - req.adm_dependency_id = parseInt(params.adm_dependency_id, 10); - req.paramCnt += 1; + if(typeof req.filter.region !== 'undefined' + || typeof req.dims.region !== 'undefined') { + req.sql.join('municipios', null, 'fk_municipio_id=municipios.pk_municipio_id') + .join('estados', null, 'municipios.fk_estado_id=estados.pk_estado_id') + .join('regioes', null, 'estados.fk_regiao_id=regioes.pk_regiao_id'); } - if (typeof params.min_year !== 'undefined') { - req.min_year = parseInt(params.min_year, 10); - req.paramCnt += 1; + if((typeof req.filter.state !== 'undefined' + || typeof req.dims.state !== 'undefined') + && (typeof req.filter.region === 'undefined' + && typeof req.dims.region === 'undefined')) { + req.sql.join('municipios', null, 'fk_municipio_id=municipios.pk_municipio_id') + .join('estados', null, 'municipios.fk_estado_id=estados.pk_estado_id'); } - if (typeof params.max_year !== 'undefined') { - req.max_year = parseInt(params.max_year, 10); - req.paramCnt += 1; + if((typeof req.filter.city !== 'undefined' + || typeof req.dims.city !== 'undefined') + && (typeof req.filter.state === 'undefined' + && typeof req.dims.state === 'undefined') + && (typeof req.filter.region === 'undefined' + && typeof req.dims.region === 'undefined')) { + req.sql.join('municipios', null, 'fk_municipio_id=municipios.pk_municipio_id'); } - if (typeof params.education_level_id !== 'undefined') { - req.education_level_id = parseInt(params.education_level_id, 10); - req.paramCnt += 1; + if(typeof req.dims.school !== 'undefined') { + req.sql.join('escolas', null, 'fk_escola_id=escolas.pk_escola_id'); } - next(); -}); + // Dimensions (add fields) + + if(typeof req.dims.education_level_id !== 'undefined') { + req.sql.field('desc_etapa', 'education_level') + .group('desc_etapa') + .order('desc_etapa'); + } -enrollmentApp.use('/', (req, res, next) => { - const params = req.query; - if (typeof params.aggregate !== 'undefined' && params.aggregate === 'region') { - log.debug('Using enrollments query for regions'); - const q = squel.select().from('mat_regioes') - .field('name') - .field('SUM(total)', 'total') - .field('ano_censo', 'year'); + if(typeof req.dims.region !== 'undefined') { + req.sql.field('regioes.nome', 'region_name') + .group('regioes.nome') + .order('regioes.nome'); + } - filter(req, q); + if(typeof req.dims.state !== 'undefined') { + req.sql.field('estados.nome', 'state_name') + .group('estados.nome') + .order('estados.nome'); + } - if (typeof req.id !== 'undefined') { - q.where('pk_regiao_id=?', req.id); - } - req.sql = q.group('name').group('ano_censo').order('ano_censo').toParam(); + if(typeof req.dims.city !== 'undefined') { + req.sql.field('municipios.nome', 'city_name') + .group('municipios.nome') + .order('municipios.nome'); } - next(); -}); -enrollmentApp.use('/', (req, res, next) => { - const params = req.query; - if (typeof params.aggregate !== 'undefined' && params.aggregate === 'state') { - log.debug('Using enrollments query for states'); - const q = squel.select().from('mat_estados') - .field('name') - .field('SUM(total)', 'total') - .field('ano_censo', 'year'); + if(typeof req.dims.school !== 'undefined') { + req.sql.field('escolas.nome_entidade', 'school_name') + .group('escolas.nome_entidade') + .order('escolas.nome_entidade'); + } - filter(req, q); + if(typeof req.dims.adm_dependency_id !== 'undefined') { + req.sql.field('dependencia_adms.nome', 'adm_dependency_name') + .group('dependencia_adms.nome') + .order('dependencia_adms.nome'); + } - if (typeof req.id !== 'undefined') { - q.where('pk_estado_id=?', req.id); - } - req.sql = q.group('name').group('ano_censo').order('ano_censo').toParam(); + if(typeof req.dims.location_id !== 'undefined') { + req.sql.field('turmas.id_localizacao', 'location') + .group('turmas.id_localizacao') + .order('turmas.id_localizacao'); } - next(); -}); -enrollmentApp.use('/', (req, res, next) => { - const params = req.query; - if (typeof params.aggregate !== 'undefined' && params.aggregate === 'city') { - log.debug('Using enrollments query for cities'); - const q = squel.select().from('mat_municipios') - .field('name') - .field('SUM(total)', 'total') - .field('ano_censo', 'year'); + if(typeof req.dims.region === 'undefined' + && typeof req.dims.state === 'undefined' + && typeof req.dims.city === 'undefined') { + req.sql.field("'Brasil'", 'name'); + } - filter(req, q); + // Filter (add where) - if (typeof req.id !== 'undefined') { - q.where('pk_municipio_id=?', req.id); - } - req.sql = q.group('name').group('ano_censo').order('ano_censo').toParam(); + if (typeof req.filter.min_year !== 'undefined') { + req.sql.where('turmas.ano_censo>=?', parseInt(req.filter.min_year, 10)); } - next(); -}); -enrollmentApp.use('/', (req, res, next) => { - const params = req.query; - if (typeof params.aggregate !== 'undefined' && params.aggregate === 'school') { - log.debug('Using enrollments query for schools'); - const q = squel.select().from('mat_escolas') - .field('name') - .field('SUM(total)', 'total') - .field('ano_censo', 'year'); + if (typeof req.filter.max_year !== 'undefined') { + req.sql.where('turmas.ano_censo<=?', parseInt(req.filter.max_year, 10)); + } - filter(req, q); + if (typeof req.filter.adm_dependency_id !== 'undefined') { + req.sql.where('pk_dependencia_adm_id=?', parseInt(req.filter.adm_dependency_id, 10)); + } - if (typeof req.id !== 'undefined') { - q.where('pk_escola_id=?', req.id); - } - req.sql = q.group('name').group('ano_censo').order('ano_censo').toParam(); + if (typeof req.filter.location_id !== 'undefined') { + req.sql.where('turmas.id_localizacao=?', parseInt(req.filter.location_id, 10)); + } + + if (typeof req.filter.education_level_id !== 'undefined') { + req.sql.where('pk_etapa_ensino_id=?', parseInt(req.filter.education_level_id, 10)); } - next(); -}); -enrollmentApp.use('/', (req, res, next) => { - const params = req.query; - if (typeof params.aggregate === 'undefined') { - log.debug('Using enrollments query for the whole country'); - const q = squel.select().from('turmas').field("'Brasil'", 'name') - .field('COALESCE(SUM(num_matriculas),0)', 'total') - .field('ano_censo', 'year'); + if (typeof req.filter.region !== 'undefined') { + req.sql.where('pk_regiao_id=?', parseInt(req.filter.region, 10)); + } + + if (typeof req.filter.state !== 'undefined') { + req.sql.where('pk_estado_id=?', parseInt(req.filter.state, 10)); + } - filter(req, q); + if (typeof req.filter.city !== 'undefined') { + req.sql.where('turmas.fk_municipio_id=?', parseInt(req.filter.city, 10)); + } - req.sql = q.group('ano_censo').order('ano_censo').toParam(); + if (typeof req.filter.school !== 'undefined') { + req.sql.where('turmas.fk_escola_id=?', parseInt(req.filter.school, 10)); } + log.debug(req.sql.toParam()); next(); }); enrollmentApp.get('/', (req, res, next) => { - log.debug(`Request parameters: ${req}`); + req.sql.field('COALESCE(SUM(num_matriculas), 0)', 'total') + .field('turmas.ano_censo', 'year') + .from('turmas') + .group('turmas.ano_censo') + .order('turmas.ano_censo'); next(); -}, query, response('enrollments')); +}, query, response('test')); module.exports = enrollmentApp; diff --git a/src/libs/routes/region.js b/src/libs/routes/region.js index 6efb36c48d1c803aaea8a3b20bb24b4ddd62eb41..773ad347959049dd2137d96abb255a3bc809a2d4 100644 --- a/src/libs/routes/region.js +++ b/src/libs/routes/region.js @@ -1,6 +1,6 @@ const express = require('express'); -const regionApp = express(); +const regionApp = express.Router(); const libs = `${process.cwd()}/libs`; @@ -12,14 +12,14 @@ const response = require(`${libs}/middlewares/response`); // Get all regions regionApp.get('/', (req, res, next) => { - req.sql = squel.select().from('regioes').toParam(); + req.sql.from('regioes'); next(); }, query, response('region')); // Get a region by it's id regionApp.get('/:id', (req, res, next) => { - req.sql = squel.select().from('regioes').where('pk_regiao_id = ?', - parseInt(req.params.id, 10)).toParam(); + req.sql.from('regioes') + .where('pk_regiao_id = ?', parseInt(req.params.id, 10)); next(); }, query, response('region')); diff --git a/src/libs/routes/school.js b/src/libs/routes/school.js index 255e595b31b59b61479f45b9f2c155bd1d9c8870..8662d945f4f39989bc318709e7beab9802a3bfb8 100644 --- a/src/libs/routes/school.js +++ b/src/libs/routes/school.js @@ -1,6 +1,6 @@ const express = require('express'); -const schoolApp = express(); +const schoolApp = express.Router(); const libs = `${process.cwd()}/libs`; @@ -16,50 +16,44 @@ const response = require(`${libs}/middlewares/response`); * A api fica sobrecarregada * Pense na cena do elevador de driver mas o elevador é uma bomba de fusão e demora mais que uma luta do DBz */ -/* schoolApp.get('/', (req, res, next) => { -* req.sql = squel.select().from('escolas') -* .field('pk_escola_id') -* .field('nome_entidade', 'name') -* .field('ano_censo', 'year') -* .field('fk_cod_estado') -* .field('fk_cod_municipio') -* .toParam(); -* next(); -* }, query, response('school')); -*/ +// schoolApp.get('/', (req, res, next) => { +// req.sql = squel.select().from('escolas') +// .field('pk_escola_id') +// .field('nome_entidade', 'name') +// .field('ano_censo', 'year') +// .field('fk_cod_estado') +// .field('fk_cod_municipio'); +// next(); +// }, query, response('school')); // Get a school by it's id schoolApp.get('/:id', (req, res, next) => { - req.sql = squel.select().from('escolas').where('pk_escola_id = ?', - parseInt(req.params.id, 10)).toParam(); + req.sql.from('escolas') + .where('pk_escola_id = ?', parseInt(req.params.id, 10)); next(); }, query, response('school')); // Get all schools from a state schoolApp.get('/state/:id', (req, res, next) => { - req.sql = squel.select().from('escolas') + req.sql.from('escolas') .field('pk_escola_id') .field('nome_entidade', 'name') .field('ano_censo', 'year') .field('fk_cod_estado') .field('fk_cod_municipio') - .where('fk_cod_estado = ?', - parseInt(req.params.id, 10)) - .toParam(); + .where('fk_cod_estado = ?', parseInt(req.params.id, 10)); next(); }, query, response('school')); // Get all schools from a city schoolApp.get('/city/:id', (req, res, next) => { - req.sql = squel.select().from('escolas') + req.sql.from('escolas') .field('pk_escola_id') .field('nome_entidade', 'name') .field('ano_censo', 'year') .field('fk_cod_estado') .field('fk_cod_municipio') - .where('fk_cod_municipio = ?', - parseInt(req.params.id, 10)) - .toParam(); + .where('fk_cod_municipio = ?', parseInt(req.params.id, 10)); next(); }, query, response('school')); diff --git a/src/libs/routes/state.js b/src/libs/routes/state.js index 7ee58c575d706d57b02bf51f2b4136bab0d8b57f..75e1c0b7835901804ea78e459b1fe0050ec03631 100644 --- a/src/libs/routes/state.js +++ b/src/libs/routes/state.js @@ -1,6 +1,6 @@ const express = require('express'); -const stateApp = express(); +const stateApp = express.Router(); const libs = `${process.cwd()}/libs`; @@ -12,21 +12,21 @@ const response = require(`${libs}/middlewares/response`); // Get all states stateApp.get('/', (req, res, next) => { - req.sql = squel.select().from('estados').toParam(); + req.sql.from('estados'); next(); }, query, response('state')); // Get a state stateApp.get('/:id', (req, res, next) => { - req.sql = squel.select().from('estados').where('pk_estado_id = ?', - parseInt(req.params.id, 10)).toParam(); + req.sql.from('estados') + .where('pk_estado_id = ?', parseInt(req.params.id, 10)); next(); }, query, response('state')); // Get all states from a region stateApp.get('/region/:id', (req, res, next) => { - req.sql = squel.select().from('estados').where('fk_regiao_id = ?', - parseInt(req.params.id, 10)).toParam(); + req.sql.from('estados') + .where('fk_regiao_id = ?', parseInt(req.params.id, 10)); next(); }, query, response('state')); diff --git a/src/test/test.js b/src/test/test.js index dde277d0dec15186290a5a0aa992dd60d8e5c7a4..e6bdd1fd0edf6ce272f73a68d6749401705859b6 100644 --- a/src/test/test.js +++ b/src/test/test.js @@ -19,7 +19,7 @@ chai.use(chaiHttp); describe('request enrollments', () => { it('should list enrollments', (done) => { chai.request(server) - .get('/v1/enrollment') + .get('/api/v1/enrollment') .end((err, res) => { res.should.have.status(200); res.should.be.json; @@ -35,7 +35,7 @@ describe('request enrollments', () => { describe('request regions', () => { it('should list all regions', (done) => { chai.request(server) - .get('/v1/region') + .get('/api/v1/region') .end((err, res) => { res.should.have.status(200); res.should.be.json; @@ -49,7 +49,7 @@ describe('request regions', () => { it('should list region by id', (done) => { chai.request(server) - .get('/v1/region/1') + .get('/api/v1/region/1') .end((err, res) => { res.should.have.status(200); res.should.be.json; @@ -66,7 +66,7 @@ describe('request regions', () => { describe('request states', () => { it('should list all states', (done) => { chai.request(server) - .get('/v1/state') + .get('/api/v1/state') .end((err, res) => { res.should.have.status(200); res.should.be.json; @@ -81,7 +81,7 @@ describe('request states', () => { it('should list a state by id', (done) => { chai.request(server) - .get('/v1/state/11') + .get('/api/v1/state/11') .end((err, res) => { res.should.have.status(200); res.should.be.json; @@ -97,7 +97,7 @@ describe('request states', () => { it('should list states by region id', (done) => { chai.request(server) - .get('/v1/state/region/1') + .get('/api/v1/state/region/1') .end((err, res) => { res.should.have.status(200); res.should.be.json; @@ -114,7 +114,7 @@ describe('request states', () => { describe('request cities', () => { it('should list all cities', (done) => { chai.request(server) - .get('/v1/city') + .get('/api/v1/city') .end((err, res) => { res.should.have.status(200); res.should.be.json; @@ -130,7 +130,7 @@ describe('request cities', () => { it('should list a city by id', (done) => { chai.request(server) - .get('/v1/city/1') + .get('/api/v1/city/1') .end((err, res) => { res.should.have.status(200); res.should.be.json; @@ -146,7 +146,7 @@ describe('request cities', () => { it('should list a city by codigo_ibge', (done) => { chai.request(server) - .get('/v1/city/ibge/1200013') + .get('/api/v1/city/ibge/1200013') .end((err, res) => { res.should.have.status(200); res.should.be.json;