From d924288b0b62d1e669384896675441bb2692c057 Mon Sep 17 00:00:00 2001
From: Gabriel Ruschel <grc15@inf.ufpr.br>
Date: Tue, 18 Apr 2017 11:38:42 -0300
Subject: [PATCH] Classroom count route

---
 src/libs/routes/classroom.js | 136 +++++++++++++++++++++++++++++++++++
 1 file changed, 136 insertions(+)
 create mode 100644 src/libs/routes/classroom.js

diff --git a/src/libs/routes/classroom.js b/src/libs/routes/classroom.js
new file mode 100644
index 00000000..354154f2
--- /dev/null
+++ b/src/libs/routes/classroom.js
@@ -0,0 +1,136 @@
+const express = require('express');
+
+const classroomApp = express.Router();
+
+const libs = `${process.cwd()}/libs`;
+
+const squel = require('squel');
+
+const query = require(`${libs}/middlewares/query`);
+
+const response = require(`${libs}/middlewares/response`);
+
+const ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`);
+
+let rqf = new ReqQueryFields();
+
+rqf.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'
+    }
+});
+
+classroomApp.get('/count', rqf.parse(), rqf.build(), (req, res, next) => {
+    console.log(req.filter);
+    req.sql.from('escola')
+        .field('SUM(escola.num_salas)', '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.local_func_predio_escolar = 1');
+    next();
+}, query, response('classroom'));
+
+module.exports = classroomApp;
-- 
GitLab