From bcf8b40dd1fdb65a4582c0bba5dd3724b295c2a7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=A3o=20Victor=20Tozatti=20Risso?= <jvtr12@inf.ufpr.br>
Date: Fri, 2 Sep 2016 13:25:56 -0300
Subject: [PATCH] Merge states.js with state.js
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: João Victor Tozatti Risso <jvtr12@inf.ufpr.br>
---
 src/libs/routes/state.js  | 60 +++++++++------------------------------
 src/libs/routes/states.js | 44 ----------------------------
 2 files changed, 14 insertions(+), 90 deletions(-)
 delete mode 100644 src/libs/routes/states.js

diff --git a/src/libs/routes/state.js b/src/libs/routes/state.js
index a1fa64a5..c5493f20 100644
--- a/src/libs/routes/state.js
+++ b/src/libs/routes/state.js
@@ -1,58 +1,26 @@
 const express = require('express');
-
-const xml = require('js2xmlparser');
-
 const stateApp = express();
-
 const libs = `${process.cwd()}/libs`;
-
 const log = require(`${libs}/log`)(module);
-
-const dbQuery = require(`${libs}/db/query_exec`);
-
-function response(req, res) {
-    if (req.query.format === 'csv') {
-        res.csv(req.result.data);
-    } else if (req.query.format === 'xml') {
-        res.send(xml('result', JSON.stringify({ state: req.result.data })));
-    } else {
-        res.json({ result: req.result.data });
-    }
-}
+const squel = require('squel');
+const query = require(`${libs}/middlewares/query`);
+const response = require(`${libs}/middlewares/response`);
 
 stateApp.get('/', (req, res, next) => {
-    const stateSql = 'SELECT * FROM estados';
-    dbQuery(stateSql).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.query = squel.select().from('estados').toParam();
+    next();
+}, query, response);
 
 stateApp.get('/:id', (req, res, next) => {
-    const stateSql = 'SELECT * FROM estados WHERE pk_estado_id = ?';
-    const stateId = parseInt(req.params.id, 10);
-    dbQuery(stateSql, [stateId]).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.query = squel.select().from('estados').where('pk_estado_id = ?',
+        parseInt(req.params.id, 10)).toParam();
+    next();
+}, query, response);
 
 stateApp.get('/region/:id', (req, res, next) => {
-    const stateSql = 'SELECT * FROM estados WHERE fk_regiao_id = ?';
-    const regionId = parseInt(req.params.id, 10);
-    dbQuery(stateSql, [regionId]).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.query = squel.select().from('estados').where('fk_regiao_id = ?',
+        parseInt(req.params.id, 10)).toParam();
+    next();
+}, query, response);
 
 module.exports = stateApp;
diff --git a/src/libs/routes/states.js b/src/libs/routes/states.js
deleted file mode 100644
index 79959ef8..00000000
--- a/src/libs/routes/states.js
+++ /dev/null
@@ -1,44 +0,0 @@
-var express = require('express')
-var xml = require('js2xmlparser')
-var router = express.Router()
-var squel = require('squel')
-
-var libs = process.cwd() + '/libs/'
-
-var log = require(libs + 'log')(module)
-var config = require(libs + 'config')
-
-var conn = require(libs + 'db/monet')
-
-var query = require(libs + 'middlewares/query')
-
-function response(req, res) {
-    console.log('respostas :)')
-    if (req.query.format === 'csv') {
-        res.csv(req.result.data)
-    } else if (req.query.format === 'xml') {
-        res.send(xml("result", JSON.stringify({state: req.result.data})))
-    }
-    else {
-        res.json({
-            result: req.result.data
-        })
-    }
-}
-
-router.get('/', function(req, res, next) {
-    req.query = squel.select().from('estados').toParam()
-    next()
-}, query, response)
-
-router.get('/:id', function(req, res, next) {
-    req.query = squel.select().from('estados').where('pk_estado_id = ?', parseInt(req.params.id, 10)).toParam()
-    next()
-}, query, response)
-
-router.get('/region/:id', function(req, res, next) {
-    req.query = squel.select().from('estados').where('fk_regiao_id = ?', parseInt(req.params.id, 10)).toParam()
-    next()
-}, query, response)
-
-module.exports = router
-- 
GitLab