From df675691a434e00216cf3fa45f5f3ab8c57201ad Mon Sep 17 00:00:00 2001
From: Vytor Calixto <vytorcalixto@gmail.com>
Date: Mon, 26 Mar 2018 10:27:05 -0300
Subject: [PATCH] First steps in classroom count

---
 src/libs/routes/api.js            |   3 +
 src/libs/routes/classroom.js      |   2 +-
 src/libs/routes/classroomCount.js | 190 ++++++++++++++++++++++++++++++
 3 files changed, 194 insertions(+), 1 deletion(-)
 create mode 100644 src/libs/routes/classroomCount.js

diff --git a/src/libs/routes/api.js b/src/libs/routes/api.js
index 2b569193..802c503c 100644
--- a/src/libs/routes/api.js
+++ b/src/libs/routes/api.js
@@ -60,6 +60,8 @@ const distributionFactor = require(`${libs}/routes/distributionFactor`);
 
 const siope = require(`${libs}/routes/siope`);
 
+const classroomCount = require(`${libs}/routes/classroomCount`);
+
 api.get('/', (req, res) => {
     res.json({ msg: 'SimCAQ API is running' });
 });
@@ -92,5 +94,6 @@ api.use('/downloads', downloads);
 api.use('/infrastructure', infrastructure);
 api.use('/distribution_factor', distributionFactor);
 api.use('/siope', siope);
+api.use('/classroom_count', classroomCount);
 
 module.exports = api;
diff --git a/src/libs/routes/classroom.js b/src/libs/routes/classroom.js
index 246e3d9b..2a8ee391 100644
--- a/src/libs/routes/classroom.js
+++ b/src/libs/routes/classroom.js
@@ -1,4 +1,4 @@
- const express = require('express');
+const express = require('express');
 
 const classroomApp = express.Router();
 
diff --git a/src/libs/routes/classroomCount.js b/src/libs/routes/classroomCount.js
new file mode 100644
index 00000000..a2e94b13
--- /dev/null
+++ b/src/libs/routes/classroomCount.js
@@ -0,0 +1,190 @@
+const express = require('express');
+
+const classroomCountApp = 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 id2str = require(`${libs}/middlewares/id2str`);
+
+const addMissing = require(`${libs}/middlewares/addMissing`);
+
+const config = require(`${libs}/config`); 
+
+const cache = require('apicache').options({ debug: config.debug, statusCodes: {include: [200]} }).middleware;
+
+let rqf = new ReqQueryFields();
+
+rqf.addField({
+    name: 'filter',
+    field: false,
+    where: true
+}).addField({
+    name: 'dims',
+    field: true,
+    where: false
+}).addValueToField({
+    name: 'city',
+    table: 'municipio',
+    tableField: 'nome',
+    resultField: 'city_name',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'municipio_id',
+        table: 'matricula'
+    },
+    join: {
+        primary: 'id',
+        foreign: 'municipio_id',
+        foreignTable: 'matricula'
+    }
+}, 'filter').addValueToField({
+    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: 'matricula'
+    }
+}, 'dims').addValue({
+    name: 'state',
+    table: 'estado',
+    tableField: 'nome',
+    resultField: 'state_name',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'estado_id',
+        table: 'matricula'
+    },
+    join: {
+        primary: 'id',
+        foreign: 'estado_id',
+        foreignTable: 'matricula'
+    }
+}).addValue({
+    name: 'region',
+    table: 'regiao',
+    tableField: 'nome',
+    resultField: 'region_name',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'id'
+    },
+    join: {
+        primary: 'id',
+        foreign: 'regiao_id',
+        foreignTable: 'matricula'
+    }
+}).addValue({
+    name: 'min_year',
+    table: 'matricula',
+    tableField: 'ano_censo',
+    resultField: 'year',
+    where: {
+        relation: '>=',
+        type: 'integer',
+        field: 'ano_censo'
+    }
+}).addValue({
+    name: 'max_year',
+    table: 'matricula',
+    tableField: 'ano_censo',
+    resultField: 'year',
+    where: {
+        relation: '<=',
+        type: 'integer',
+        field: 'ano_censo'
+    }
+}).addValue({
+    name: 'period',
+    table: 'matricula',
+    tableField: 'turma_turno_id',
+    resultField: 'period_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'turma_turno_id'
+    }
+}).addValue({
+    name: 'school_year',
+    table: 'matricula',
+    tableField: 'serie_ano_id',
+    resultField: 'school_year_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'serie_ano_id'
+    }
+}).addValue({
+    name: 'location',
+    table: 'matricula',
+    tableField: 'localizacao_id',
+    resultField: 'location_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'localizacao_id'
+    }
+}).addValue({
+    name: 'education_level_short',
+    table: 'matricula',
+    tableField: 'etapa_resumida',
+    resultField: 'education_level_short_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'etapa_resumida'
+    }
+});
+
+classroomCountApp.post('/', rqf.parse(), (req, res, next) => {
+    console.log('body');
+    console.log(req.body);
+    
+    let classSize = JSON.parse(req.body.class_size) || null;
+    let integralTime = JSON.parse(req.body.integral_time) || null;
+
+    console.log(classSize, integralTime);
+
+    req.dims.state = true;
+    req.dims.city = true;
+    req.dims.period = true;
+    req.dims.education_level_short = true;
+    req.dims.location = true;
+
+    req.sql.field('COUNT(*)', 'total')
+    .field("'Brasil'", 'name')
+    .field('matricula.ano_censo', 'year')
+    .from('matricula')
+    .group('matricula.ano_censo')
+    .order('matricula.ano_censo')
+    .where('matricula.tipo<=3');
+
+    next();
+}, rqf.build(), query, id2str.transform(), (req, res, next) => {
+    let result = req.result;
+    // result.forEach((r) => {
+
+    // });
+    
+    next();
+}, response('classroom_count'));
+
+module.exports = classroomCountApp;
\ No newline at end of file
-- 
GitLab