/* Copyright (C) 2016 Centro de Computacao Cientifica e Software Livre Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR This file is part of simcaq-node. simcaq-node is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. simcaq-node is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with simcaq-node. If not, see <https://www.gnu.org/licenses/>. */ const express = require('express'); const courseCountApp = 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 ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`); const id2str = require(`${libs}/middlewares/id2str`); const config = require(`${libs}/config`); const addMissing = require(`${libs}/middlewares/addMissing`); const cache = require('apicache').options({ debug: config.debug, statusCodes: {include: [200]} }).middleware; let rqf = new ReqQueryFields(); courseCountApp.use(cache('15 day')); courseCountApp.get('/upper_adm_dependency', (req, res, next) => { req.result = []; for(let i = 1; i <= 7; ++i) { req.result.push({ id: i, name: id2str.upperAdmDependency(i) }); }; next(); }, response('upper_adm_dependency')); courseCountApp.get('/years', (req, res, next) => { req.sql.from('curso_ens_superior') .field('DISTINCT curso_ens_superior.ano_censo', 'year'); next(); }, query, response('years')); courseCountApp.get('/year_range', (req, res, next) => { req.sql.from('curso_ens_superior') .field('MIN(curso_ens_superior.ano_censo)', 'start_year') .field('MAX(curso_ens_superior.ano_censo)', 'end_year'); next(); }, query, response('range')); courseCountApp.get('/academic_organization', (req, res, next) => { req.result = []; for(let i = 1; i <= 5; ++i) { req.result.push({ id: i, name: id2str.academicOrganization(i) }); }; next(); }, response('academic_organization')); courseCountApp.get('/capital', (req, res, next) => { req.result = [ {id: null, name: 'Não Classificado'}, {id: 0, name: 'Não'}, {id: 1, name: 'Sim'} ]; next(); }, response('capital')); courseCountApp.get('/ocde_geral', (req, res, next) => { req.result = []; for(let i = 1; i <= 8; ++i) { req.result.push({ id: i, name: id2str.ocdeGeral(i) }); }; next(); }, response('ocde_geral')); courseCountApp.get('/ocde_specific', (req, res, next) => { req.result = []; const defaultCase = null; for(let i = 1; i <= 86; ++i) { let obj = { id: i, name: id2str.ocdeSpecific(i) }; if (obj.name !== id2str.ocdeSpecific(defaultCase)){ req.result.push(obj); } }; req.result.push({ id: defaultCase, name: id2str.ocdeSpecific(defaultCase) }); next(); }, response('ocde_specific')); courseCountApp.get('/ocde_detailed', (req, res, next) => { req.result = []; const defaultCase = null; for(let i = 142; i <= 863; ++i) { let obj = { id: i, name: id2str.ocdeDetailed(i) }; if (obj.name !== id2str.ocdeDetailed(defaultCase)){ req.result.push(obj); } }; req.result.push({ id: defaultCase, name: id2str.ocdeDetailed(defaultCase) }); next(); }, response('ocde_detailed')); courseCountApp.get('/academic_level', (req, res, next) => { req.result = []; for(let i = 1; i <= 4; ++i) { req.result.push({ id: i, name: id2str.academicLevel(i) }); }; next(); }, response('academic_level')); courseCountApp.get('/upper_education_mod', (req, res, next) => { req.result = []; for(let i = 1; i <= 3; ++i) { req.result.push({ id: i, name: id2str.upperEducationMod(i) }); }; next(); }, response('upper_education_mod')); courseCountApp.get('/is_free', (req, res, next) => { req.result = [ {id: null, name: 'Não Classificado'}, {id: 0, name: 'Não'}, {id: 1, name: 'Sim'} ]; next(); }, response('is_free')); courseCountApp.get('/night_time', (req, res, next) => { req.result = [ {id: null, name: 'Não Classificado'}, {id: 0, name: 'Não'}, {id: 1, name: 'Sim'} ]; next(); }, response('night_time')); courseCountApp.get('/university', (req, res, next) => { req.sql.from('curso_ens_superior') .field('DISTINCT curso_ens_superior.nome_ies', 'nome') .field('curso_ens_superior.cod_ies', 'cod') next(); }, query, response('university')); rqf.addField({ name: 'filter', field: false, where: true }).addField({ name: 'dims', field: true, where: false }).addValue({ name: 'region', table: 'regiao', tableField: ['nome', 'id'], resultField: ['region_name', 'region_id'], where: { relation: '=', type: 'integer', field: 'id' }, join: { primary: 'nome', foreign: 'nome_regiao_curso', foreignTable: 'curso_ens_superior' } }).addValue({ name: 'state', table: 'estado', tableField: ['nome', 'id'], resultField: ['state_name', 'state_id'], where: { relation: '=', type: 'integer', field: 'id' }, join: { primary: 'id', foreign: 'cod_uf_curso', foreignTable: 'curso_ens_superior' } }).addValue({ name: 'city', table: 'curso_ens_superior', tableField: ['municipio_curso', 'cod_municipio_curso'], resultField: ['city_name', 'city_id'], where: { relation: '=', type: 'integer', field: 'cod_municipio_curso' } }).addValue({ name: 'university', table: 'curso_ens_superior', tableField: ['cod_ies', 'nome_ies'], resultField: ['university_id', 'university_name'], where: { relation: '=', type: 'integer', field: 'cod_ies' } }).addValue({ name:'upper_adm_dependency', table: 'curso_ens_superior', tableField: 'par_categoria_administrativa', resultField: 'upper_adm_dependency_id', where: { relation: '=', type: 'integer', field: 'par_categoria_administrativa' } }).addValue({ name:'academic_organization', table: 'curso_ens_superior', tableField: 'cod_organizacao_academica', resultField: 'academic_organization_id', where: { relation: '=', type: 'integer', field: 'cod_organizacao_academica' } }).addValue({ name:'capital', table: 'curso_ens_superior', tableField: 'tfd_capital_curso', resultField: 'capital_id', where: { relation: '=', type: 'integer', field: 'tfd_capital_curso' } }).addValue({ name:'course', table: 'curso_ens_superior', tableField: 'nome_curso', resultField: 'course_name', where: { relation: 'LIKE', type: 'string', field: 'nome_curso' } }).addValue({ name:'ocde_specific', table: 'curso_ens_superior', tableField: ['cod_ocde_area_especifica', 'nome_ocde_area_especifica'], resultField: ['ocde_specific_id', 'ocde_specific_name'], where: { relation: '=', type: 'integer', field: 'cod_ocde_area_especifica' } }).addValue({ name:'ocde_geral', table: 'curso_ens_superior', tableField: ['cod_ocde_area_geral', 'nome_ocde_area_geral'], resultField: ['ocde_geral_id', 'ocde_geral_name'], where: { relation: '=', type: 'integer', field: 'cod_ocde_area_geral' } }).addValue({ name:'ocde_detailed', table: 'curso_ens_superior', tableField: ['cod_ocde_area_detalhada', 'nome_ocde_area_detalhada'], resultField: ['ocde_detailed_id', 'ocde_datailed_name'], where: { relation: '=', type: 'integer', field: 'cod_ocde_area_detalhada' } }).addValue({ name:'academic_level', table: 'curso_ens_superior', tableField: 'cod_grau_academico', resultField: 'academic_level_id', where: { relation: '=', type: 'integer', field: 'cod_grau_academico' } }).addValue({ name:'upper_education_mod', table: 'curso_ens_superior', tableField: 'cod_modalidade_ensino', resultField: 'upper_education_mod_id', where: { relation: '=', type: 'integer', field: 'cod_modalidade_ensino' } }).addValue({ name:'is_free', table: 'curso_ens_superior', tableField: 'gratuito', resultField: 'is_free_id', where: { relation: '=', type: 'boolean', field: 'gratuito' } }).addValue({ name:'night_time', table: 'curso_ens_superior', tableField: 'noturno_curso', resultField: 'night_time_id', where: { relation: '=', type: 'boolean', field: 'noturno_curso' } }).addValue({ name:'situation', table: 'curso_ens_superior', tableField: 'cod_situacao_curso', resultField: 'situacao_curso_id', where: { relation: '=', type: 'integer', field: 'cod_situacao_curso' } }).addValue({ name:'year', table: 'curso_ens_superior', tableField: 'ano_censo', resultField: 'year', where: { relation: '=', type: 'integer', field: 'ano_censo' } }).addValue({ name: 'min_year', table: 'curso_ens_superior', tableField: 'ano_censo', resultField: 'year', where: { relation: '>=', type: 'integer', table: 'curso_ens_superior', field: 'ano_censo' } }).addValue({ name: 'max_year', table: 'curso_ens_superior', tableField: 'ano_censo', resultField: 'year', where: { relation: '<=', type: 'integer', table: 'curso_ens_superior', field: 'ano_censo' } }); courseCountApp.get('/', rqf.parse(), (req, res, next) => { req.sql.from('curso_ens_superior') .field('COUNT(curso_ens_superior.cod_curso)', 'total') .field("'Brasil'", 'name') .field('curso_ens_superior.ano_censo', 'year') .group('curso_ens_superior.ano_censo') .order('curso_ens_superior.ano_censo') .where('curso_ens_superior.tipo_atributo_ingresso <> 1 OR curso_ens_superior.tipo_atributo_ingresso is NULL') .where('curso_ens_superior.cod_nivel_academico = 1'); next(); }, rqf.build(), query, id2str.transform(), addMissing(rqf), response('course_count')); module.exports = courseCountApp;