From e7908d70dc27f10cc4742e25caf8d11558837cc2 Mon Sep 17 00:00:00 2001 From: Gabriel Ruschel <grc15@inf.ufpr.br> Date: Wed, 29 Mar 2017 11:51:14 -0300 Subject: [PATCH] Count schools by state, region, city and other filters --- src/libs/routes/school.js | 131 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) diff --git a/src/libs/routes/school.js b/src/libs/routes/school.js index d6e5f7d0..a1135ebb 100644 --- a/src/libs/routes/school.js +++ b/src/libs/routes/school.js @@ -13,6 +13,7 @@ const response = require(`${libs}/middlewares/response`); const ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`); let rqf = new ReqQueryFields(); +let rqfCount = new ReqQueryFields(); rqf.addField({ name: 'filter', @@ -72,6 +73,124 @@ rqf.addField({ } }); +rqfCount.addField({ + name: 'filter', + field: false, + where: true +}).addField({ + name: 'dims', + field: true, + where: false +}).addValue({ + name: 'id', + table: 'escola', + tableField: 'id', + where: { + relation: '=', + type: 'integer', + field: 'id' + } +}).addValue({ + name: 'city', + table: 'municipio', + tableField: 'nome', + resultField: 'city_name', + where: { + relation: '=', + type: 'integer', + field: 'municipio_id', + table: 'escola' + }, + join: { + primary: 'id', + foreign: 'municipio_id', + foreignTable: 'escola' + } +}).addValue({ + name: 'state', + table: 'estado', + tableField: 'nome', + resultField: 'state_name', + where: { + relation: '=', + type: 'integer', + field: 'estado_id', + table: 'escola' + }, + join: { + primary: 'id', + foreign: 'estado_id', + foreignTable: 'escola' + } +}).addValue({ + name: 'region', + table: 'regiao', + tableField: 'nome', + resultField: 'region_name', + where: { + relation: '=', + type: 'integer', + field: 'id' + }, + join: { + primary: 'id', + foreign: 'regiao_id', + foreignTable: 'escola' + } +}).addValue({ + name: 'year', + table: 'escola', + tableField: 'ano_censo', + resultField: 'year', + where: { + relation: '=', + type: 'integer', + field: 'ano_censo', + table: 'escola' + } +}).addValue({ + name: 'adm_dependency', + table: 'dependencia_adm', + tableField: 'nome', + resultField: 'adm_dependency_name', + where: { + relation: '=', + type: 'integer', + field: 'id' + }, + join: { + primary: 'id', + foreign: 'dependencia_adm_id', + foreignTable: 'escola' + } +}).addValue({ + name: 'adm_dependency_detailed', + table: 'dependencia_adm', + tableField: 'nome', + resultField: 'adm_dependency_detailed_name', + where: { + relation: '=', + type: 'integer', + field: 'id' + }, + join: { + primary: 'id', + foreign: 'dependencia_adm_priv', + foreignTable: 'escola' + } +// tem problemas +}).addValue({ + name: 'government_agreement', + table: 'escola', + tableField: 'conveniada_pp', + resultField: 'conveniada_poder_publico', + where: { + relation: '=', + type: 'integer', + field: 'conveniada_pp' + } +}); + schoolApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { console.log(req.filter); if(typeof req.filter === 'undefined' || Object.keys(req.filter).length === 0) { @@ -90,4 +209,16 @@ schoolApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { next(); }, query, response('school')); +schoolApp.get('/count', rqfCount.parse(), rqfCount.build(), (req, res, next) => { + + req.sql.from('escola') + .field('COUNT(escola.id)', 'total') + .field("'Brasil'", 'name') + .field('escola.ano_censo', 'year') + .group('escola.ano_censo') + .order('escola.ano_censo') + .where('escola.situacao_de_funcionamento = 1 AND escola.ensino_regular = 1'); + next(); +}, query, response('school')); + module.exports = schoolApp; -- GitLab