diff --git a/src/libs/routes/api.js b/src/libs/routes/api.js index 45bdea870e8ec629ba3b8e877a62f0fed29bdeba..760ef5501bde072fd1a2ddfe733af452b5c0a9e8 100644 --- a/src/libs/routes/api.js +++ b/src/libs/routes/api.js @@ -122,6 +122,8 @@ const mesoregion = require(`${libs}/routes/mesoregion`); const microregion = require(`${libs}/routes/microregion`); +const location = require(`${libs}/routes/location`); + api.get('/', (req, res) => { res.json({ msg: 'SimCAQ API is running' }); }); @@ -174,4 +176,5 @@ api.use('/course_count', courseCount); api.use('/school_location', schoolLocation); api.use('/mesoregion', mesoregion); api.use('/microregion', microregion); +api.use('/location', location); module.exports = api; diff --git a/src/libs/routes/location.js b/src/libs/routes/location.js new file mode 100644 index 0000000000000000000000000000000000000000..d493c452f8dfa1c16a5731d5088fbdf700ff5b08 --- /dev/null +++ b/src/libs/routes/location.js @@ -0,0 +1,135 @@ +const express = require('express'); + +const locationApp = express.Router(); + +const libs = `${process.cwd()}/libs`; + +const log = require(`${libs}/log`)(module); + +const squel = require('squel'); + +const query = require(`${libs}/middlewares/query`).query; + +const response = require(`${libs}/middlewares/response`); + +const id2str = require(`${libs}/middlewares/id2str`); + +const ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`); + +const request = require(`request`); + +const config = require(`${libs}/config`); + +const passport = require('passport'); + +const addMissing = require(`${libs}/middlewares/addMissing`); + +const cache = require('apicache').options({ debug: config.debug, statusCodes: {include: [200]} }).middleware; + +locationApp.use(cache('15 day')) + +let rqf = new ReqQueryFields(); + +rqf.addField({ + name: 'filter', + field: false, + where: true +}).addField({ + name: 'dims', + field: true, + where: false +}).addValue({ + name: 'id', + table: 'localizacao_escolas', + tableField: 'id', + where: { + relation: '=', + type: 'integer', + field: 'id' + } +}).addValue({ + name: 'mesoregion', + table: 'municipio', + tableField: ['nome_mesorregiao', 'mesorregiao_id'], + resultField: ['mesoregion_name', 'mesoregion_id'], + where: { + relation: '=', + type: 'integer', + field: 'mesorregiao_id', + table: 'municipio' + }, + join: { + primary: 'id', + foreign: 'municipio_id', + foreignTable: '@' + } +}).addValue({ + name: 'microregion', + table: 'municipio', + tableField: ['nome_microrregiao', 'microrregiao_id'], + resultField: ['microregion_name', 'microregion_id'], + where: { + relation: '=', + type: 'integer', + field: 'microrregiao_id', + table: 'municipio' + }, + join: { + primary: 'id', + foreign: 'municipio_id', + foreignTable: '@' + } +}).addValueToField({ + name: 'city', + table: 'municipio', + tableField: ['nome', 'id'], + resultField: ['city_name', 'city_id'], + where: { + relation: '=', + type: 'integer', + field: 'municipio_id', + }, + join: { + primary: 'id', + foreign: 'municipio_id', + foreignTable: '@' + } +}, 'dims'); + +locationApp.get('/school', rqf.parse(), (req, res, next) => { + req.dims.city=true; + req.dims.mesoregion=true; + req.dims.microregion=true; + + req.sql.from('localizacao_escolas') + .field('localizacao_escolas.nome', 'name') + .field('localizacao_escolas.id', 'id') + .field('localizacao_escolas.latitude', 'latitude') + .field('localizacao_escolas.longitude', 'longitude') + .group('localizacao_escolas.nome') + .group('localizacao_escolas.id') + .group('localizacao_escolas.latitude') + .group('localizacao_escolas.longitude'); + next(); +}, rqf.build(), query, id2str.transform(), response('location')); + +locationApp.get('/campi', rqf.parse(), (req, res, next) => { + req.dims.city=true; + req.dims.mesoregion=true; + req.dims.microregion=true; + + req.sql.from('localizacao_campi') + .field('localizacao_campi.nome', 'name') + .field('localizacao_campi.id', 'id') + .field('localizacao_campi.ies_id', 'ies_id') + .field('localizacao_campi.latitude', 'latitude') + .field('localizacao_campi.longitude', 'longitude') + .group('localizacao_campi.nome') + .group('localizacao_campi.id') + .group('localizacao_campi.ies_id') + .group('localizacao_campi.latitude') + .group('localizacao_campi.longitude'); + next(); +}, rqf.build(), query, id2str.transform(), response('location')); + +module.exports = locationApp; \ No newline at end of file