Skip to content
Snippets Groups Projects
Commit e3960564 authored by Vytor Calixto's avatar Vytor Calixto :space_invader:
Browse files

Update census_year to min_year and max_year

parent 4c4a49a9
No related branches found
No related tags found
No related merge requests found
Pipeline #
......@@ -14,8 +14,14 @@ 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.census_year !== 'undefined') {
q.where('ano_censo=?', req.census_year);
if (typeof req.min_year !== 'undefined') {
if (typeof req.max_year === 'undefined') q.where('ano_censo=?', req.min_year);
else q.where('ano_censo>=?', req.min_year);
}
if (typeof req.max_year !== 'undefined') {
if (typeof req.min_year === 'undefined') q.where('ano_censo=?', req.max_year);
else q.where('ano_censo<=?', req.max_year);
}
if (typeof req.adm_dependency_id !== 'undefined') {
......@@ -84,8 +90,13 @@ enrollmentApp.use('/', (req, res, next) => {
req.paramCnt += 1;
}
if (typeof params.census_year !== 'undefined') {
req.census_year = parseInt(params.census_year, 10);
if (typeof params.min_year !== 'undefined') {
req.min_year = parseInt(params.min_year, 10);
req.paramCnt += 1;
}
if (typeof params.max_year !== 'undefined') {
req.max_year = parseInt(params.max_year, 10);
req.paramCnt += 1;
}
......@@ -101,14 +112,17 @@ 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');
const q = squel.select().from('mat_regioes')
.field('name')
.field('SUM(total)', 'total')
.field('ano_censo', 'year');
filter(q);
if (typeof req.id !== 'undefined') {
q.where('pk_regiao_id=?', req.id);
}
req.sql = q.group('name').toParam();
req.sql = q.group('name').group('ano_censo').order('ano_censo').toParam();
}
next();
});
......@@ -117,14 +131,17 @@ 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');
const q = squel.select().from('mat_estados')
.field('name')
.field('SUM(total)', 'total')
.field('ano_censo', 'year');
filter(req, q);
if (typeof req.id !== 'undefined') {
q.where('pk_estado_id=?', req.id);
}
req.sql = q.group('name').toParam();
req.sql = q.group('name').group('ano_censo').order('ano_censo').toParam();
}
next();
});
......@@ -133,14 +150,17 @@ 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_estados').field('name').field('SUM(total)', 'total');
const q = squel.select().from('mat_municipios')
.field('name')
.field('SUM(total)', 'total')
.field('ano_censo', 'year');
filter(req, q);
if (typeof req.id !== 'undefined') {
q.where('pk_municipio_id=?', req.id);
}
req.sql = q.group('name').toParam();
req.sql = q.group('name').group('ano_censo').order('ano_censo').toParam();
}
next();
});
......@@ -150,11 +170,12 @@ enrollmentApp.use('/', (req, res, next) => {
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('COALESCE(SUM(num_matriculas),0)', 'total')
.field('ano_censo', 'year');
filter(req, q);
req.sql = q.toParam();
req.sql = q.group('ano_censo').order('ano_censo').toParam();
}
next();
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment