diff --git a/src/libs/routes/cities.js b/src/libs/routes/cities.js deleted file mode 100644 index fd0302635c7ab4ded59bfb1bc41efe5150a5d929..0000000000000000000000000000000000000000 --- a/src/libs/routes/cities.js +++ /dev/null @@ -1,42 +0,0 @@ -var express = require('express') -var xml = require('js2xmlparser') -var router = express.Router() -var squel = require('squel') - -var libs = process.cwd() + '/libs/' - -var log = require(libs + 'log')(module) -var config = require(libs + 'config') - -var conn = require(libs + 'db/monet') -var query = require(libs + 'middlewares/query') - -function response(req, res) { - if (req.query.format === 'csv') { - res.csv(req.result.data) - } else if (req.query.format === 'xml') { - res.send(xml("result", JSON.stringify({city: req.result.data}))) - } - else { - res.json({ - result: req.result.data - }) - } -} - -router.get('/', function(req, res, next) { - req.query = squel.select().from('municipios').limit(2).toParam() - next() -}, query, response) - -router.get('/:id', function(req, res, next) { - req.query = squel.select().from('municipios').where('pk_municipio_id=?', parseInt(req.params.id, 10)).toParam() - next() -}, query, response) - -router.get('/state/:id', function(req, res, next) { - req.query = squel.select().from('municipios').where('fk_estado_id=?', parseInt(req.params.id, 10)).toParam() - next() -}, query, response) - -module.exports = router diff --git a/src/libs/routes/city.js b/src/libs/routes/city.js index 1b390498d9c1fce94c6d71af7d17d7854ba5fdd8..95423602a15a19984a86f48958e925f202a2ccb6 100644 --- a/src/libs/routes/city.js +++ b/src/libs/routes/city.js @@ -1,70 +1,35 @@ const express = require('express'); - -const xml = require('js2xmlparser'); - const cityApp = express(); - const libs = `${process.cwd()}/libs`; - const log = require(`${libs}/log`)(module); - +const squel = require('squel'); const dbQuery = require(`${libs}/db/query_exec`); +const query = require(`${libs}/middlewares/query`); +const response = require(`${libs}/middlewares/response`); -function response(req, res) { - if (req.query.format === 'csv') { - res.csv(req.result); - } else if (req.query.format === 'xml') { - res.send(xml('result', JSON.stringify({ city: req.result }))); - } else { - res.json({ result: req.result }); - } -} - -cityApp.get('/', (req, res) => { - const citySql = 'SELECT * FROM municipios LIMIT 5'; - dbQuery(yearSql).then((result) => { - req.result = result; - return response(req, res); - }, (error) => { - log.error(`[${req.originalUrl}] SQL query error: ${error}`); - next('Internal error, request could not be satisfied at this moment. Please, try again later'); - }); -}); +cityApp.get('/', (req, res, next) => { + req.query = squel.select().from('municipios').toParam(); + next(); +}, query, response); -cityApp.get('/:id', (req, res) => { - const citySql = 'SELECT * FROM municipios WHERE pk_municipio_id = ?'; - const cityId = parseInt(req.params.id, 10); - dbQuery(citySql, [cityId]).then((result) => { - req.result = result; - return response(req, res); - }, (error) => { - log.error(`[${req.originalUrl}] SQL query error: ${error}`); - next('Internal error, request could not be satisfied at this moment. Please, try again later'); - }); -}); +cityApp.get('/:id', (req, res, next) => { + req.query = squel.select().from('municipios').where('pk_municipio_id = ?', + parseInt(req.params.id, 10)).toParam(); + next(); +}, query, response); -cityApp.get('/ibge/:id', (req, res) => { +cityApp.get('/ibge/:id', (req, res, next) => { const citySql = 'SELECT * FROM municipios WHERE codigo_ibge = ?'; const cityIbgeCode = req.params.id; - dbQuery(citySql, [cityIbgeCode]).then((result) => { - req.result = result; - return response(req, res); - }, (error) => { - log.error(`[${req.originalUrl}] SQL query error: ${error}`); - next('Internal error, request could not be satisfied at this moment. Please, try again later'); - }); -}); - -cityApp.get('/state/:id', (req, res) => { - const citySql = 'SELECT * FROM municipios WHERE fk_estado_id = ?'; - const stateId = parseInt(req.params.id, 10); - dbQuery(citySql, [stateId]).then((result) => { - req.result = result; - return response(req, res); - }, (error) => { - log.error(`[${req.originalUrl}] SQL query error: ${error}`); - next('Internal error, request could not be satisfied at this moment. Please, try again later'); - }); -}); + req.query = squel.select().from('municipios').where('codigo_ibge = ?', + parseInt(req.params.id, 10)).toParam(); + next(); +}, query, response); + +cityApp.get('/state/:id', (req, res, next) => { + req.query = squel.select().from('municipios').where('fk_estado_id = ?', + parseInt(req.params.id, 10)); + next(); +}, query, response); module.exports = cityApp;