From 93cbea3c8430ded698f80922bcb10e08fccf3789 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Leon=20A=2E=20Okida=20Gon=C3=A7alves?= <laog19@inf.ufpr.br>
Date: Thu, 6 Apr 2023 11:05:59 -0300
Subject: [PATCH] Add new classes route

---
 src/libs/routes_v2/api.js                     |   3 +
 src/libs/routes_v2/simcaqNewClasses.js        | 131 ++++++++++++++++++
 src/libs/routes_v2/simcaqNumberOfEmployees.js |   2 +-
 3 files changed, 135 insertions(+), 1 deletion(-)
 create mode 100644 src/libs/routes_v2/simcaqNewClasses.js

diff --git a/src/libs/routes_v2/api.js b/src/libs/routes_v2/api.js
index fd345d05..195a23e8 100644
--- a/src/libs/routes_v2/api.js
+++ b/src/libs/routes_v2/api.js
@@ -148,6 +148,8 @@ const simcaqWorkload = require(`${libs}/routes_v2/simcaqWorkload`);
 
 const simcaqNumberOfEmployees = require(`${libs}/routes_v2/simcaqNumberOfEmployees`);
 
+const simcaqNewClasses = require(`${libs}/routes_v2/simcaqNewClasses`);
+
 api.get('/', (req, res) => {
     res.json({ msg: 'SimCAQ API v2 is running' });
 });
@@ -213,5 +215,6 @@ api.use('/simcaq_division_of_responsibility', simcaqDivisionOfResponsibility);
 api.use('/simcaq_classroom_size', simcaqClassroomSize);
 api.use('/simcaq_workload', simcaqWorkload);
 api.use('/simcaq_number_of_employees', simcaqNumberOfEmployees);
+api.use('/simcaq_new_classes', simcaqNewClasses);
 
 module.exports = api;
diff --git a/src/libs/routes_v2/simcaqNewClasses.js b/src/libs/routes_v2/simcaqNewClasses.js
new file mode 100644
index 00000000..4922270e
--- /dev/null
+++ b/src/libs/routes_v2/simcaqNewClasses.js
@@ -0,0 +1,131 @@
+/*
+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 simcaqNewClassesApp = express.Router();
+
+const libs = `${process.cwd()}/libs`;
+
+const squel = require('squel');
+
+const query = require(`${libs}/middlewares/query`).query;
+
+const response = require(`${libs}/middlewares/response`);
+
+const ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`);
+
+const reqBody = require(`${libs}/middlewares/reqBody`);
+
+const config = require(`${libs}/config`); 
+
+const cache = require('apicache').options({ debug: config.debug, statusCodes: {include: [200]} }).middleware;
+
+let rqf = new ReqQueryFields();
+
+const id2str = require(`${libs}/middlewares/id2str`);
+
+simcaqNewClassesApp.use(cache('15 day'));
+
+rqf.addField({
+    name: 'filter',
+    field: false,
+    where: true
+}).addField({
+    name: 'dims',
+    field: true,
+    where: false
+}).addValue({
+    name: 'year',
+    table: 'simcaq_novas_salas',
+    tableField: 'ano_censo',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'ano_censo'
+    }
+}).addValue({
+    name: 'city',
+    table: 'municipio',
+    tableField: ['nome', 'id'],
+    resultField: ['city_name', 'city_id'],
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'id'
+    },
+    join: {
+        primary: 'id',
+        foreign: 'municipio_id',
+        foreignTable: 'simcaq_novas_salas'
+    }
+}, 'dims').addValue({
+    name: 'state',
+    table: 'estado',
+    tableField: ['nome', 'id'],
+    resultField: ['state_name', 'state_id'],
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'id'
+    },
+    join: {
+        primary: 'id',
+        foreign: 'estado_id',
+        foreignTable: 'simcaq_novas_salas'
+    }
+}, 'dims').addValue({
+    name: 'school',
+    table: 'escola_agregada',
+    tableField: 'id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'id'
+    },
+    join: {
+        primary: 'id',
+        foreign: 'escola_id',
+        foreignTable: 'simcaq_novas_salas'
+    }
+}, 'dims').addValue({
+    name: 'adm_dependency_public_id',
+    table: 'simcaq_novas_salas',
+    tableField: 'rede',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'rede'
+    }
+});
+
+simcaqNewClassesApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => {
+    req.sql.from('simcaq_novas_salas')
+        .field('SUM(num_salas_urbana)', 'number_of_urban_classes')
+        .field('SUM(num_salas_rural)', 'number_of_rural_classes')
+        .field('SUM(num_salas_total)', 'total_number_of_classes')
+        .field('simcaq_novas_salas.rede', 'adm_dependency_public_id')
+        .field('simcaq_novas_salas.ano_censo', 'year')
+        .group('simcaq_novas_salas.rede')
+        .group('simcaq_novas_salas.ano_censo');
+    next();
+}, query, id2str.transform(), response('simcaqNewClasses'));
+
+module.exports = simcaqNewClassesApp;
diff --git a/src/libs/routes_v2/simcaqNumberOfEmployees.js b/src/libs/routes_v2/simcaqNumberOfEmployees.js
index 58a28ffa..3496aa09 100644
--- a/src/libs/routes_v2/simcaqNumberOfEmployees.js
+++ b/src/libs/routes_v2/simcaqNumberOfEmployees.js
@@ -118,7 +118,7 @@ rqf.addField({
 
 simcaqNumberOfEmployeesApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => {
     req.sql.from('simcaq_numero_de_funcionarios')
-        .field('SUM(num_funcionarios)', 'average_number_of_employees')
+        .field('SUM(num_funcionarios)', 'number_of_employees')
         .field('simcaq_numero_de_funcionarios.rede', 'adm_dependency_public_id')
         .field('simcaq_numero_de_funcionarios.ano_censo', 'year')
         .group('simcaq_numero_de_funcionarios.rede')
-- 
GitLab