From 2d041c223feac8289c4c2e7265b1b81d3fbade8b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Leon=20A=2E=20Okida=20Gon=C3=A7alves?= <laog19@inf.ufpr.br>
Date: Tue, 28 Feb 2023 11:19:31 -0300
Subject: [PATCH] Add testroute, path to testroute, Add new middleware's body
 parsing

---
 src/libs/middlewares/reqBody.js |  24 ++++
 src/libs/routes/api.js          |   4 +
 src/libs/routes/testroute.js    | 188 ++++++++++++++++++++++++++++++++
 3 files changed, 216 insertions(+)
 create mode 100644 src/libs/middlewares/reqBody.js
 create mode 100644 src/libs/routes/testroute.js

diff --git a/src/libs/middlewares/reqBody.js b/src/libs/middlewares/reqBody.js
new file mode 100644
index 00000000..e1f80cb1
--- /dev/null
+++ b/src/libs/middlewares/reqBody.js
@@ -0,0 +1,24 @@
+class ReqBody {
+    constructor() {
+
+    }
+    parse() {
+        return(req, res, next) => {
+            // Gets body of the HTTP requisition
+            let body = req.body;
+
+            // Chooses operation based on the mode field of the body
+            switch(body["mode"]) {
+                case "add_metrics":
+                    console.log(body);
+                    break;
+                default:
+                    break;
+            }
+
+            next();
+        }
+    }
+}
+
+module.exports = ReqBody;
\ No newline at end of file
diff --git a/src/libs/routes/api.js b/src/libs/routes/api.js
index f6715eaa..1a68a1d4 100644
--- a/src/libs/routes/api.js
+++ b/src/libs/routes/api.js
@@ -134,6 +134,8 @@ const message = require(`${libs}/routes/message`);
 
 const courseStudents = require(`${libs}/routes/courseStudents`);
 
+const testRoute = require(`${libs}/routes/testroute`);
+
 api.get('/', (req, res) => {
     res.json({ msg: 'SimCAQ API is running' });
 });
@@ -193,4 +195,6 @@ api.use('/universityLocalOffer', universityLocalOffer);
 api.use('/message', message);
 api.use('/course_students', courseStudents);
 
+api.use('/test', testRoute);
+
 module.exports = api;
diff --git a/src/libs/routes/testroute.js b/src/libs/routes/testroute.js
new file mode 100644
index 00000000..2f108eb7
--- /dev/null
+++ b/src/libs/routes/testroute.js
@@ -0,0 +1,188 @@
+/*
+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 testApp = 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();
+let reqBody = new ReqBody();
+
+testApp.use(cache('15 day'));
+
+rqf.addField({
+    name: 'filter',
+    field: false,
+    where: true
+}).addValue({
+    name: 'min_year',
+    table: 'escola',
+    tableField: 'ano_censo',
+    where: {
+        relation: '>=',
+        type: 'integer',
+        field: 'ano_censo'
+    }
+}).addValue({
+    name: 'max_year',
+    table: 'escola',
+    tableField: 'ano_censo',
+    where: {
+        relation: '<=',
+        type: 'integer',
+        field:'ano_censo'
+    }
+}).addValue({
+    name: 'region',
+    table: 'escola',
+    tableField: 'regiao_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'regiao_id'
+    }
+}).addValue({
+    name: 'region_not',
+    table: 'escola',
+    tableField: 'regiao_id',
+    where: {
+        relation: '<>',
+        type: 'integer',
+        field: 'regiao_id'
+    }
+}).addValue({
+    name: 'city',
+    table: 'escola',
+    tableField: 'municipio_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'municipio_id'
+    }
+}).addValue({
+    name: 'city_not',
+    table: 'escola',
+    tableField: 'municipio_id',
+    where: {
+        relation: '<>',
+        type: 'integer',
+        field: 'municipio_id'
+    }
+}).addValue({
+    name: 'state',
+    table: 'escola',
+    tableField: 'estado_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'estado_id'
+    }
+}).addValue({
+    name: 'state_not',
+    table: 'escola',
+    tableField: 'estado_id',
+    where: {
+        relation: '<>',
+        type: 'integer',
+        field: 'estado_id'
+    }
+}).addValue({
+    name: 'dep_admin',
+    table: 'escola',
+    tableField: 'dependencia_adm_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'dependencia_adm_id'
+    }
+}).addValue({
+    name: 'dep_pub_ins',
+    table: 'escola',
+    tableField: 'dependencia_convenio_publico',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'dependencia_convenio_publico'
+    }
+}).addValue({
+    name: 'arrangement',
+    table: 'escola',
+    tableField: 'arranjo',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'arranjo'
+    }
+}).addValue({
+    name: 'location',
+    table: 'escola',
+    tableField: 'localizacao_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'localizacao_id'
+    }
+}).addValue({
+    name: 'diff_location',
+    table: 'escola',
+    tableField: 'localizacao_diferenciada_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'localizacao_diferenciada_id'
+    }
+}).addValue({
+    name: 'full_time',
+    table: 'escola',
+    tableField: 'tempo_integral',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'tempo_integral'
+    }
+});
+
+testApp.get('/', rqf.parse(), rqf.build(), reqBody.parse(), (req, res, next) => {
+    req.sql.from('escola')
+        .field('ano_censo')
+        .field('count(*)', 'total')
+        .group('ano_censo').order('ano_censo')
+        .where('escola.situacao_funcionamento_pareada = 1 AND (escola.ensino_regular = 1 OR escola.ensino_eja=1 or escola.educacao_profissional=1)')
+    console.log(req.sql.toString());
+    next();
+}, query, response('school'));
+
+module.exports = testApp;
\ No newline at end of file
-- 
GitLab