Commit 48a861fd authored by Vytor Calixto's avatar Vytor Calixto 👾

Start centralization of routes in api.js

Routes done:
* region
* state
* city
* school
* enrollments

Related: SCRUM#198
parent 6da004e7
Pipeline #7811 failed with stage
in 2 minutes and 11 seconds
......@@ -24,18 +24,63 @@ const simulation = require('./simulation');
const user = require('./user');
const query = require(`${libs}/middlewares/query`);
const response = require(`${libs}/middlewares/response`);
api.get('/', (req, res) => {
res.json({ msg: 'SimCAQ API is running' });
});
// mount API routes
api.use('/user', user);
api.use('/simulation', simulation);
api.use('/enrollment', cache('1 day'), enrollment);
api.use('/state', cache('15 day'), state);
api.use('/region', cache('15 day'), region);
api.use('/city', cache('15 day'), city);
api.use('/school', cache('15 day'), school);
// Rotas de matrículas
api.get('/enrollment/year_range', enrollment.getYearRange, query, response('year_range'));
api.get('/enrollment/location', enrollment.getLocation, query, response('location'));
api.get('/enrollment/education_level', enrollment.getEducationLevel, query, response('education_level'));
api.get('/enrollment/adm_dependency', enrollment.getAdmDependency, query, response('adm_dependency'));
api.get('/enrollment', parseParams('filter', [
'min_year',
'max_year',
'adm_dependency',
'location',
'education_level',
'region',
'state',
'city',
'school'
]), parseParams('dims', [
'adm_dependency',
'location',
'education_level',
'region',
'state',
'city',
'school'
]), enrollment.parseDimensionsAndFilters, enrollment.getEnrollments, query, response('enrollment'));
// Rotas das regiões
api.get('/region', region.getAll, query, response('region'));
api.get('/region/:id', region.getById, query, response('region'));
// Rotas dos estados
api.get('/state', state.getAll, query, response('state'));
api.get('/state/:id', state.getById, query, response('state'));
api.get('/state/region/:id', state.getAllByRegion, query, response('state'));
// Rotas das cidades
api.get('/city', city.getAll, query, response('city'));
api.get('/city/:id', city.getById, query, response('city'));
api.get('/city/ibge/:id', city.getById, query, response('city'));
api.get('/city/state/:id', city.getByState, query, response('city'));
// Rotas das escolas
api.get('/school/:id', school.getById, query, response('school'));
api.get('/school/state/:id', school.getByState, query, response('school'));
api.get('/school/city/:id', school.getByCity, query, response('school'));
api.use('/location', cache('1 day'), location);
module.exports = api;
......@@ -10,18 +10,20 @@ const query = require(`${libs}/middlewares/query`);
const response = require(`${libs}/middlewares/response`);
// Return all cities
cityApp.get('/', (req, res, next) => {
// Retorna todas as cidades
function getAll(req, res, next) {
// SELECT pk_cod_ibge AS pk_municipio_id, nome, pk_cod_ibge AS codigo_ibge, fk_estado_id FROM municipio;
req.sql.from('municipio')
.field('pk_cod_ibge', 'pk_municipio_id')
.field('nome')
.field('pk_cod_ibge', 'codigo_ibge')
.field('fk_estado_id');
next();
}, query, response('city'));
}
// Return a specific city by it's id
cityApp.get('/:id', (req, res, next) => {
// Retorna uma cidade por seu código
function getById(req, res, next) {
// SELECT pk_cod_ibge AS pk_municipio_id, nome, pk_cod_ibge AS codigo_ibge, fk_estado_id FROM municipio WHERE pk_cod_ibge = :id;
req.sql.from('municipio')
.field('pk_cod_ibge', 'pk_municipio_id')
.field('nome', 'nome')
......@@ -29,22 +31,11 @@ cityApp.get('/:id', (req, res, next) => {
.field('fk_estado_id')
.where('pk_cod_ibge = ?', parseInt(req.params.id, 10));
next();
}, query, response('city'));
}
// Return a specific city by it's IBGE code
// TODO: this route becomes obsolete in the new schema, deprecate it
cityApp.get('/ibge/:id', (req, res, next) => {
req.sql.from('municipio')
.field('pk_cod_ibge', 'pk_municipio_id')
.field('nome')
.field('fk_estado_id')
.field('pk_cod_ibge', 'codigo_ibge')
.where('pk_cod_ibge = ?', parseInt(req.params.id, 10));
next();
}, query, response('city'));
// Return all the cities from a specific state
cityApp.get('/state/:id', (req, res, next) => {
// Retorna todas as cidades de um estado
function getByState(req, res, next) {
// SELECT pk_cod_ibge AS pk_municipio_id, nome, pk_cod_ibge AS codigo_ibge, fk_estado_id FROM municipio WHERE fk_estado_id = :id;
req.sql.from('municipio')
.field('pk_cod_ibge', 'pk_municipio_id')
.field('nome')
......@@ -52,6 +43,10 @@ cityApp.get('/state/:id', (req, res, next) => {
.field('fk_estado_id')
.where('fk_estado_id = ?', parseInt(req.params.id, 10));
next();
}, query, response('city'));
}
module.exports = cityApp;
module.exports = {
getAll: getAll,
getById: getById,
getByState: getByState
};
......@@ -18,134 +18,109 @@ const parseParams = require(`${libs}/middlewares/parseParams`);
// 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) => {
function getYearRange(req, res, next) {
req.sql = squel.select()
.from('turma')
.field('MIN(turma.ano_censo)', 'start_year')
.field('MAX(turma.ano_censo)', 'end_year');
next();
}, query, response('range'));
}
enrollmentApp.get('/location', (req, res, next) => {
function getLocation(req, res, next) {
req.sql = squel.select()
.field('pk_localizacao_id', 'location_id')
.field('descricao', 'description')
.from('localizacao');
next();
}, query, response('location'));
}
// Returns all educational levels avaible
enrollmentApp.get('/education_level', (req, res, next) => {
// Returns all educational levels available
function getEducationLevel(req, res, next) {
req.sql = squel.select()
.from('etapa_ensino')
.field('pk_etapa_ensino_id', 'id')
.field('desc_etapa', 'name');
next();
}, query, response('education_level'));
}
// Returns all adm dependencies
enrollmentApp.get('/adm_dependency', (req, res, next) => {
function getAdmDependency(req, res, next) {
req.sql = squel.select()
.from('dependencia_adm')
.field('pk_dependencia_adm_id', 'id')
.field('nome', 'name');
next();
}, query, response('adm_dependency'));
// Parse the filters and dimensions parameter in the query
enrollmentApp.use('/', parseParams('filter', [
'min_year',
'max_year',
'adm_dependency',
'location',
'education_level',
'region',
'state',
'city',
'school'
]), parseParams('dims', [
'adm_dependency',
'location',
'education_level',
'region',
'state',
'city',
'school'
]), (req, res, next) => {
log.debug(req.filter);
log.debug(req.dims);
}
function parseDimensionsAndFilters(req, res, next) {
//applyJoins(req.sql, req.filter, req.dims);
// Do the joins
if(typeof req.filter.adm_dependency !== 'undefined'
|| typeof req.dims.adm_dependency !== 'undefined') {
if (typeof req.filter.adm_dependency !== 'undefined' ||
typeof req.dims.adm_dependency !== 'undefined') {
req.sql.join('dependencia_adm', null,
'fk_dependencia_adm_id = dependencia_adm.pk_dependencia_adm_id');
}
if(typeof req.filter.education_level !== 'undefined'
|| typeof req.dims.education_level !== 'undefined') {
if (typeof req.filter.education_level !== 'undefined' ||
typeof req.dims.education_level !== 'undefined') {
req.sql.join('etapa_ensino', null,
'turma.fk_etapa_ensino_id = etapa_ensino.pk_etapa_ensino_id');
}
if(typeof req.filter.region !== 'undefined'
|| typeof req.dims.region !== 'undefined') {
req.sql.join('municipio', null, 'fk_municipio_id = municipio.pk_cod_ibge')
.join('estado', null, 'municipio.fk_estado_id = estado.pk_estado_id')
.join('regiao', null, 'estado.fk_regiao_id = regiao.pk_regiao_id');
if (typeof req.filter.region !== 'undefined' ||
typeof req.dims.region !== 'undefined') {
req.sql.join('municipio', null, 'fk_municipio_id = municipio.pk_cod_ibge')
.join('estado', null, 'municipio.fk_estado_id = estado.pk_estado_id')
.join('regiao', null, 'estado.fk_regiao_id = regiao.pk_regiao_id');
}
if((typeof req.filter.state !== 'undefined'
|| typeof req.dims.state !== 'undefined')
&& (typeof req.filter.region === 'undefined'
&& typeof req.dims.region === 'undefined')) {
req.sql.join('municipio', null, 'fk_municipio_id = municipio.pk_cod_ibge')
.join('estado', null, 'municipio.fk_estado_id = estado.pk_estado_id');
if ((typeof req.filter.state !== 'undefined' ||
typeof req.dims.state !== 'undefined') &&
(typeof req.filter.region === 'undefined' &&
typeof req.dims.region === 'undefined')) {
req.sql.join('municipio', null, 'fk_municipio_id = municipio.pk_cod_ibge')
.join('estado', null, 'municipio.fk_estado_id = estado.pk_estado_id');
}
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')) {
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('municipio', null, 'fk_municipio_id = municipio.pk_cod_ibge');
}
if(typeof req.filter.location !== 'undefined' || typeof req.dims.location !== 'undefined') {
if (typeof req.filter.location !== 'undefined' || typeof req.dims.location !== 'undefined') {
req.sql.join('localizacao', null, 'fk_localizacao_id = localizacao.pk_localizacao_id')
}
if(typeof req.dims.school !== 'undefined') {
if (typeof req.dims.school !== 'undefined') {
req.sql.join('escola', null, 'turma.cod_entidade = escola.cod_entidade');
}
// Dimensions (add fields)
if(typeof req.dims.education_level !== 'undefined') {
if (typeof req.dims.education_level !== 'undefined') {
req.sql.field('desc_etapa', 'education_level')
.group('desc_etapa')
.order('desc_etapa');
}
if(typeof req.dims.region !== 'undefined') {
if (typeof req.dims.region !== 'undefined') {
req.sql.field('regiao.nome', 'region_name')
.group('regiao.nome')
.order('regiao.nome');
}
if(typeof req.dims.state !== 'undefined') {
if (typeof req.dims.state !== 'undefined') {
req.sql.field('estado.nome', 'state_name')
.group('estado.nome')
.order('estado.nome');
}
if(typeof req.dims.city !== 'undefined') {
if (typeof req.dims.city !== 'undefined') {
req.sql.field('municipio.nome', 'city_name')
.group('municipio.nome')
.order('municipio.nome');
......@@ -160,28 +135,28 @@ enrollmentApp.use('/', parseParams('filter', [
}
*/
if(typeof req.dims.school !== 'undefined') {
if (typeof req.dims.school !== 'undefined') {
req.sql.field('escola.cod_entidade', 'school_code')
.group('escola.cod_entidade')
.order('escola.cod_entidade');
}
if(typeof req.dims.adm_dependency !== 'undefined') {
if (typeof req.dims.adm_dependency !== 'undefined') {
req.sql.field('dependencia_adm.nome', 'adm_dependency_name')
.group('dependencia_adm.nome')
.order('dependencia_adm.nome');
}
if(typeof req.dims.location !== 'undefined') {
if (typeof req.dims.location !== 'undefined') {
req.sql.field('localizacao.descricao', 'location_name')
.group('localizacao.descricao')
.order('localizacao.descricao');
}
if(typeof req.dims.region === 'undefined'
&& typeof req.dims.state === 'undefined'
&& typeof req.dims.city === 'undefined'
&& typeof req.dims.school === 'undefined') {
if (typeof req.dims.region === 'undefined' &&
typeof req.dims.state === 'undefined' &&
typeof req.dims.city === 'undefined' &&
typeof req.dims.school === 'undefined') {
req.sql.field("'Brasil'", 'name');
}
......@@ -224,15 +199,21 @@ enrollmentApp.use('/', parseParams('filter', [
}
log.debug(req.sql.toParam());
next();
});
}
enrollmentApp.get('/', (req, res, next) => {
function getEnrollments(req, res, next) {
req.sql.field('COALESCE(SUM(num_matriculas), 0)', 'total')
.field('turma.ano_censo', 'year')
.from('turma')
.group('turma.ano_censo')
.order('turma.ano_censo');
next();
}, query, response('enrollment'));
module.exports = enrollmentApp;
}
module.exports = {
getYearRange: getYearRange,
getLocation: getLocation,
getEducationLevel: getEducationLevel,
getAdmDependency: getAdmDependency,
getEnrollments: getEnrollments
};
......@@ -10,17 +10,22 @@ const query = require(`${libs}/middlewares/query`);
const response = require(`${libs}/middlewares/response`);
// Get all regions
regionApp.get('/', (req, res, next) => {
// Retorna todas as regiões
function getAll(req, res, next) {
// SELECT * FROM regiao;
req.sql.from('regiao');
next();
}, query, response('region'));
}
// Get a region by it's id
regionApp.get('/:id', (req, res, next) => {
// Retorna uma região pelo seu ID
function getById(req, res, next) {
// SELECT * FROM regiao WHERE pk_regiao_id = :id;
req.sql.from('regiao')
.where('pk_regiao_id = ?', parseInt(req.params.id, 10));
next();
}, query, response('region'));
}
module.exports = regionApp;
module.exports = {
getAll: getAll,
getById: getById
};
......@@ -10,24 +10,7 @@ const query = require(`${libs}/middlewares/query`);
const response = require(`${libs}/middlewares/response`);
/**
* YOU SHALL NOT PASS
* Esta rota foi desabilitada pois é mais violenta que clube da luta batendo em laranja mecânica
* 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');
// next();
// }, query, response('school'));
// Get a school by it's id
schoolApp.get('/:id', (req, res, next) => {
function getById(req, res, next) {
req.sql.from('escola')
.field('cod_entidade', 'pk_escola_id')
.field('cod_entidade')
......@@ -36,10 +19,9 @@ schoolApp.get('/:id', (req, res, next) => {
.field('fk_estado_id')
.where('cod_entidade = ?', parseInt(req.params.id, 10));
next();
}, query, response('school'));
}
// Get all schools from a state
schoolApp.get('/state/:id', (req, res, next) => {
function getByState(req, res, next) {
req.sql.from('escola')
.field('cod_entidade', 'pk_escola_id')
.field('cod_entidade')
......@@ -47,10 +29,9 @@ schoolApp.get('/state/:id', (req, res, next) => {
.field('fk_municipio_id')
.where('fk_estado_id = ?', parseInt(req.params.id, 10));
next();
}, query, response('school'));
}
// Get all schools from a city
schoolApp.get('/city/:id', (req, res, next) => {
function getByCity(req, res, next) {
req.sql.from('escola')
.field('cod_entidade', 'pk_escola_id')
.field('cod_entidade')
......@@ -58,6 +39,10 @@ schoolApp.get('/city/:id', (req, res, next) => {
.field('fk_estado_id')
.where('fk_municipio_id = ?', parseInt(req.params.id, 10));
next();
}, query, response('school'));
}
module.exports = schoolApp;
module.exports = {
getById: getById,
getByState: getByState,
getByCity: getByCity
};
......@@ -10,24 +10,31 @@ const query = require(`${libs}/middlewares/query`);
const response = require(`${libs}/middlewares/response`);
// Get all states
stateApp.get('/', (req, res, next) => {
// Retorna todos os estados
function getAll(req, res, next) {
// SELECT * FROM estado;
req.sql.from('estado');
next();
}, query, response('state'));
}
// Get a state
stateApp.get('/:id', (req, res, next) => {
// Retorna um estado por seu ID
function getById(req, res, next) {
// SELECT * FROM estado WHERE pk_estado_id = :id;
req.sql.from('estado')
.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) => {
// Retorna todos os estados de uma região
function getAllByRegion(req, res, next) {
// SELECT * FROM estado WHERE fk_regiao_id = :id;
req.sql.from('estado')
.where('fk_regiao_id = ?', parseInt(req.params.id, 10));
next();
}, query, response('state'));
}
module.exports = stateApp;
module.exports = {
getAll: getAll,
getById: getById,
getAllByRegion: getAllByRegion
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment