diff --git a/src/libs/middlewares/reqQueryFields.js b/src/libs/middlewares/reqQueryFields.js index 577b23598add747c5ee812bf2e68021ae2fb9dee..ca4e15e0e67213ec982c7b5ab51834de0f8023e2 100644 --- a/src/libs/middlewares/reqQueryFields.js +++ b/src/libs/middlewares/reqQueryFields.js @@ -223,6 +223,7 @@ class ReqQueryFields { if(value.where.type === 'integer') whereValue = parseInt(whereValue, 10); if(value.where.type === 'double') whereValue = parseFloat(whereValue); if(value.where.type === 'string') whereValue = '%'+whereValue+'%'; + if(value.where.type === 'boolean') whereValue = (whereValue.toLowerCase() === 'true' || parseInt(whereValue, 10) === 1); let tbl = value.where.table || value.table; let whereField = (value.where.type === 'string')? 'LOWER('+tbl+'.'+value.where.field+')' : tbl+'.'+value.where.field; let lower = (value.where.type === 'string') ? ' LOWER(?) ' : ' ? '; diff --git a/src/libs/routes/school.js b/src/libs/routes/school.js index d6e5f7d013f5b0bf11f14a3456273bf64ff0cfca..94b19a5c7fed02c8a4d6fee948d66de669b66b1b 100644 --- a/src/libs/routes/school.js +++ b/src/libs/routes/school.js @@ -13,6 +13,56 @@ const response = require(`${libs}/middlewares/response`); const ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`); let rqf = new ReqQueryFields(); +let rqfCount = new ReqQueryFields(); + +// Return location +schoolApp.get('/location', (req, res, next) => { + req.result = [ + {id: 1, name: 'Urbana'}, + {id: 2, name: 'Rural'} + ]; + next(); +}, response('location')); + +schoolApp.get('/adm_dependency', (req, res, next) => { + req.sql.from('dependencia_adm') + .field('id') + .field('nome', 'name') + .where('id <= 4'); + next(); +}, query, response('adm_dependency')); + +schoolApp.get('/adm_dependency_detailed', (req, res, next) => { + req.sql.from('dependencia_adm') + .field('id', 'id') + .field('nome', 'name'); + next(); +}, query, response('adm_dependency_detailed')); + +schoolApp.get('/agreement', (req, res, next) => { + req.result = [ + {id: 1, name: 'Estadual'}, + {id: 2, name: 'Municipal'}, + {id: 3, name: 'Estadual e Municipal'} + ]; + next(); +}, response('agreement')); + +schoolApp.get('/cook_room', (req, res, next) => { + req.result = [ + {id: 0, name: 'Não'}, + {id: 1, name: 'Sim'} + ]; + next(); +}, response('cook_room')); + +schoolApp.get('/government_agreement', (req, res, next) => { + req.result = [ + {id: 0, name: 'Não'}, + {id: 1, name: 'Sim'} + ]; + next(); +}, response('government_agreement')); rqf.addField({ name: 'filter', @@ -72,6 +122,373 @@ 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' + } +}).addValue({ + name: 'location', + table: 'escola', + tableField: 'cod_localizacao', + resultField: 'cod_localizacao', + where: { + relation: '=', + type: 'integer', + field: 'cod_localizacao' + } +}).addValue({ + name: 'cook_room', + table: 'escola', + tableField: 'cozinha', + resultField: 'cook_room_name', + where: { + relation: '=', + type: 'boolean', + field: 'cozinha' + } +}).addValue({ + name: 'government_agreement', + table: 'escola', + tableField: 'conveniada_pp', + resultField: 'government_agreement_name', + where: { + relation: '=', + type: 'boolean', + field: 'conveniada_pp' + } +}).addValue({ + name: 'informatics_lab', + table: 'escola', + tableField: 'lab_informatica', + resultField: 'informatics_lab_name', + where: { + relation: '=', + type: 'boolean', + field: 'lab_informatica' + } +}).addValue({ + name: 'science_lab', + table: 'escola', + tableField: 'lab_ciencias', + resultField: 'informatics_lab_name', + where: { + relation: '=', + type: 'boolean', + field: 'lab_ciencias' + } +}).addValue({ + name: 'special_attendence_room', + table: 'escola', + tableField: 'sala_atendimento_especial', + resultField: 'special_attendence_room_name', + where: { + relation: '=', + type: 'boolean', + field: 'sala_atendimento_especial' + } +}).addValue({ + name: 'indor_sports_court', + table: 'escola', + tableField: 'quadra_esportes_coberta', + resultField: 'indor_sports_court_name', + where: { + relation: '=', + type: 'boolean', + field: 'quadra_esportes_coberta' + } +}).addValue({ + name: 'education_eja', + table: 'escola', + tableField: 'ensino_eja', + resultField: 'education_eja_name', + where: { + relation: '=', + type: 'boolean', + field: 'ensino_eja' + } +}).addValue({ + name: 'education_professional', + table: 'escola', + tableField: 'educacao_profissional', + resultField: 'education_professional_name', + where: { + relation: '=', + type: 'boolean', + field: 'educacao_profissional' + } +}).addValue({ + name: 'education_middle_school', + table: 'escola', + tableField: 'reg_medio_medio', + resultField: 'education_middle_school_name', + where: { + relation: '=', + type: 'boolean', + field: 'reg_medio_medio' + } +}).addValue({ + name: 'education_end_elementary_school', + table: 'escola', + tableField: 'reg_fund_af', + resultField: 'education_end_elementary_school_name', + where: { + relation: '=', + type: 'boolean', + field: 'reg_fund_af' + } +}).addValue({ + name: 'education_begin_elementary_school', + table: 'escola', + tableField: 'reg_fund_ai', + resultField: 'education_begin_elementary_school_name', + where: { + relation: '=', + type: 'boolean', + field: 'reg_fund_ai' + } +}).addValue({ + name: 'education_preschool_child', + table: 'escola', + tableField: 'reg_infantil_preescola', + resultField: 'education_preschool_child_name', + where: { + relation: '=', + type: 'boolean', + field: 'reg_infantil_preescola' + } +}).addValue({ + name: 'directors_room', + table: 'escola', + tableField: 'sala_diretoria', + resultField: 'directors_room_name', + where: { + relation: '=', + type: 'boolean', + field: 'sala_diretoria' + } +}).addValue({ + name: 'teacher_room', + table: 'escola', + tableField: 'sala_professor', + resultField: 'teacher_room_name', + where: { + relation: '=', + type: 'boolean', + field: 'sala_professor' + } +}).addValue({ + name: 'playground', + table: 'escola', + tableField: 'parque_infantil', + resultField: 'playground_name', + where: { + relation: '=', + type: 'boolean', + field: 'parque_infantil' + } +}).addValue({ + name: 'nusery', + table: 'escola', + tableField: 'bercario', + resultField: 'nusery_name', + where: { + relation: '=', + type: 'boolean', + field: 'bercario' + } +}).addValue({ + name: 'toilet_inside_building', + table: 'escola', + tableField: 'sanitario_dentro_predio', + resultField: 'toilet_inside_building_name', + where: { + relation: '=', + type: 'boolean', + field: 'sanitario_dentro_predio' + } + }).addValue({ + name: 'wastepipe', + table: 'escola', + tableField: 'esgoto_sanitario', + resultField: 'wastepipe_name', + where: { + relation: '=', + type: 'boolean', + field: 'esgoto_sanitario' + } + }).addValue({ + name: 'water', + table: 'escola', + tableField: 'fornecimento_agua', + resultField: 'water_name', + where: { + relation: '=', + type: 'boolean', + field: 'fornecimento_agua ' + } +}).addValue({ + name: 'energy', + table: 'escola', + tableField: 'fornecimento_energia', + resultField: 'energy_name', + where: { + relation: '=', + type: 'boolean', + field: 'fornecimento_energia ' + } +}).addValue({ + name: 'broadband', + table: 'escola', + tableField: 'internet_banda_larga', + resultField: 'broadband_name', + where: { + relation: '=', + type: 'boolean', + field: 'internet_banda_larga ' + } +}).addValue({ + name: 'restroom_pne', + table: 'escola', + tableField: 'sanitario_pne', + resultField: 'restroom_pne_name', + where: { + relation: '=', + type: 'boolean', + field: 'sanitario_pne ' + } +}).addValue({ + name: 'denpendency_pne', + table: 'escola', + tableField: 'dependencias_pne', + resultField: 'denpendency_pne_name', + where: { + relation: '=', + type: 'boolean', + field: 'dependencias_pne ' + } +}).addValue({ + name: 'agreement', + table: 'escola', + tableField: 'tipo_convenio_pp', + resultField: 'agreement_name', + where: { + relation: '=', + type: 'integer', + field: 'tipo_convenio_pp' + } +}).addValue({ + name: 'building_school', + table: 'escola', + tableField: 'local_func_predio_escolar', + resultField: 'building_school_name', + where: { + relation: '=', + type: 'boolean', + field: 'local_func_predio_escolar' + } +}); + 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 +507,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;