From 92b9b0dc3b38bff558bf966c19fc06a1676a7f95 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20Victor=20Frans=20Pondaco=20Winandy?=
 <jvfpw18@inf.ufpr.br>
Date: Wed, 20 May 2020 11:42:35 -0300
Subject: [PATCH] Add mesoregion and microregion routes

---
 src/libs/routes/api.js         |  7 +++-
 src/libs/routes/city.js        | 24 ++---------
 src/libs/routes/mesoregion.js  | 76 ++++++++++++++++++++++++++++++++++
 src/libs/routes/microregion.js | 76 ++++++++++++++++++++++++++++++++++
 4 files changed, 161 insertions(+), 22 deletions(-)
 create mode 100644 src/libs/routes/mesoregion.js
 create mode 100644 src/libs/routes/microregion.js

diff --git a/src/libs/routes/api.js b/src/libs/routes/api.js
index bc8da67c..86333d7d 100644
--- a/src/libs/routes/api.js
+++ b/src/libs/routes/api.js
@@ -118,6 +118,9 @@ const educationalBudget = require(`${libs}/routes/educationalBudget`);
 
 const schoolLocation = require(`${libs}/routes/schoolLocation`);
 
+const microregion = require(`${libs}/routes/microregion`);
+
+const mesoregion = require(`${libs}/routes/mesoregion`);
 
 api.get('/', (req, res) => {
     res.json({ msg: 'SimCAQ API is running' });
@@ -168,5 +171,7 @@ api.use('/university_enrollment', universityEnrollment);
 api.use('/university', university);
 api.use('/university_teacher', universityTeacher);
 api.use('/course_count', courseCount);
-api.use('/school_location', schoolLocation)
+api.use('/school_location', schoolLocation);
+api.use('/mesoregion', mesoregion);
+api.use('/microregion', microregion);
 module.exports = api;
diff --git a/src/libs/routes/city.js b/src/libs/routes/city.js
index 6714206d..ef28500f 100644
--- a/src/libs/routes/city.js
+++ b/src/libs/routes/city.js
@@ -69,26 +69,6 @@ rqf.addField({
         foreign: 'estado_id',
         foreignTable: 'municipio'
     }
-}).addValue({
-    name: 'mesoregion',
-    table: 'municipio',
-    tableField: ['nome_mesorregiao', 'mesorregiao_id'],
-    resultField: ['mesoregion_name', 'mesoregion_id'],
-    where: {
-        relation: '=',
-        type: 'integer',
-        field: 'mesorregiao_id',
-    }
-}).addValue({
-    name: 'microregion',
-    table: 'municipio',
-    tableField: ['nome_microrregiao', 'microrregiao_id'],
-    resultField: ['microregion_name', 'microregion_id'],
-    where: {
-        relation: '=',
-        type: 'integer',
-        field: 'microrregiao_id',
-    }
 }).addField({
     name: 'search',
     field: false,
@@ -111,7 +91,9 @@ cityApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => {
     .field('municipio.id')
     .field('municipio.estado_id', 'state_id')
     .field('municipio.longitude', 'longitude')
-    .field('municipio.latitude', 'latitude');
+    .field('municipio.latitude', 'latitude')
+    .field('municipio.mesorregiao_id', 'mesoregion_id')
+    .field('municipio.microrregiao_id', 'microregion_id');
     next();
 }, query, response('city'));
 
diff --git a/src/libs/routes/mesoregion.js b/src/libs/routes/mesoregion.js
new file mode 100644
index 00000000..1180f8ca
--- /dev/null
+++ b/src/libs/routes/mesoregion.js
@@ -0,0 +1,76 @@
+/*
+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 mesoregion = 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 config = require(`${libs}/config`); 
+
+const cache = require('apicache').options({ debug: config.debug, statusCodes: {include: [200]} }).middleware;
+
+let rqf = new ReqQueryFields();
+
+mesoregionApp.use(cache('15 day'));
+
+rqf.addField({
+    name: 'filter',
+    field: false,
+    where: true
+}).addValue({
+    name: 'state',
+    table: 'estado',
+    tableField: ['nome', 'id'],
+    resultField: ['state_name', 'state_id'],
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'estado_id',
+        table: 'municipio'
+    },
+    join: {
+        primary: 'id',
+        foreign: 'estado_id',
+        foreignTable: 'municipio'
+    }
+});
+
+mesoregionApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => {
+    req.sql.from('municipio')
+        .field('municipio.nome_mesorregiao', 'name')
+        .field('municipio.mesorregiao_id')
+        .field('municipio.estado_id', 'state_id')
+        .group('municipio.nome_mesorregiao')
+        .group('municipio.mesorregiao_id')
+        .group('municipio.estado_id')
+    next();
+}, query, response('city'));
+
+module.exports = mesoregionApp;
diff --git a/src/libs/routes/microregion.js b/src/libs/routes/microregion.js
new file mode 100644
index 00000000..a7de2511
--- /dev/null
+++ b/src/libs/routes/microregion.js
@@ -0,0 +1,76 @@
+/*
+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 microregion = 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 config = require(`${libs}/config`); 
+
+const cache = require('apicache').options({ debug: config.debug, statusCodes: {include: [200]} }).middleware;
+
+let rqf = new ReqQueryFields();
+
+microregionApp.use(cache('15 day'));
+
+rqf.addField({
+    name: 'filter',
+    field: false,
+    where: true
+}).addValue({
+    name: 'state',
+    table: 'estado',
+    tableField: ['nome', 'id'],
+    resultField: ['state_name', 'state_id'],
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'estado_id',
+        table: 'municipio'
+    },
+    join: {
+        primary: 'id',
+        foreign: 'estado_id',
+        foreignTable: 'municipio'
+    }
+});
+
+microregionApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => {
+    req.sql.from('municipio')
+        .field('municipio.nome_microrregiao', 'name')
+        .field('municipio.microrregiao_id')
+        .field('municipio.estado_id', 'state_id')
+        .group('municipio.nome_microrregiao')
+        .group('municipio.microrregiao_id')
+        .group('municipio.estado_id')
+    next();
+}, query, response('city'));
+
+module.exports = microregionApp;
-- 
GitLab