From 6694746e8c545725c96498c69d10e51cc0cd95af Mon Sep 17 00:00:00 2001
From: Vytor Calixto <vytorcalixto@gmail.com>
Date: Tue, 6 Sep 2016 11:34:18 -0300
Subject: [PATCH] Start updating enrollment routes

---
 src/libs/routes/enrollment.js | 51 +++++++++--------------------------
 1 file changed, 13 insertions(+), 38 deletions(-)

diff --git a/src/libs/routes/enrollment.js b/src/libs/routes/enrollment.js
index e3e6c399..a9feb711 100644
--- a/src/libs/routes/enrollment.js
+++ b/src/libs/routes/enrollment.js
@@ -8,9 +8,11 @@ const log = require(`${libs}/log`)(module);
 
 const squel = require('squel');
 
-const dbQuery = require('../db/query_exec');
+const query = require(`${libs}/middlewares/query`);
+
+const response = require(`${libs}/middlewares/response`);
 
-const response = require('../middlewares/response');
+const dbQuery = require('../db/query_exec');
 
 /**
  * Complete range of the enrollments dataset
@@ -18,46 +20,19 @@ const response = require('../middlewares/response');
  * Returns a tuple of start and ending years of the complete enrollments dataset.
  */
 enrollmentApp.get('/year_range', (req, res, next) => {
-    const yearSql = 'SELECT MIN(t.ano_censo) AS start_year, MAX(t.ano_censo)'
-        + 'AS end_year FROM turmas AS t';
-
-    dbQuery(yearSql).then((result) => {
-        const record = result['0'];
-        log.debug(record);
-        req.result = { start_year: record.start_year, end_year: record.end_year };
-        return response(req, res);
-    }, (error) => {
-        log.error(`[${req.originalUrl}] SQL query error: ${error}`);
-        next('Internal error, request could not be satisfied at this moment. Please, '
-            + 'try again later');
-    });
-});
+    req.sql = squel.select().from('turmas').field('MIN(turmas.ano_censo)', 'start_year').field('MAX(turmas.ano_censo)', 'end_year').toParam();
+    next();
+}, query, response('range'));
 
 enrollmentApp.get('/education_level', (req, res, next) => {
-    const edLevelSql = 'SELECT pk_etapa_ensino_id AS id, desc_etapa AS '
-        + 'education_level FROM etapa_ensino';
-
-    dbQuery(edLevelSql).then((result) => {
-        req.result = result;
-        return response(req, res);
-    }, (error) => {
-        log.error(`[${req.originalUrl}] SQL query error: ${error}`);
-        next('Internal error, request could not be satisfied at this moment. Please, '
-            + 'try again later');
-    });
-});
+    req.sql = squel.select().from('etapa_ensino').field('pk_etapa_ensino_id', 'id').field('desc_etapa', 'name').toParam();
+    next();
+}, query, response('education_level'));
 
 enrollmentApp.get('/data', (req, res, next) => {
-    const schoolClassSql = 'SELECT * FROM turmas';
-    dbQuery(schoolClassSql).then((result) => {
-        req.result = result;
-        return response(req, res);
-    }, (error) => {
-        log.error(`[${req.originalUrl}] SQL query error: ${error}`);
-        next('Internal error, request could not be satisfied at this moment. Please, '
-            + 'try again later');
-    });
-});
+    req.sql = squel.select().from('turmas').toParam();
+    next();
+}, query, response('data'));
 
 enrollmentApp.use('/', (req, res, next) => {
     const params = req.query;
-- 
GitLab