From 40db46fdd1d48e889f8bb259b512e4617a2ef34c Mon Sep 17 00:00:00 2001
From: Gabriel Ruschel <grc15@inf.ufpr.br>
Date: Thu, 13 Jul 2017 13:06:51 -0300
Subject: [PATCH] Fix filters

---
 src/libs/routes/idhme.js | 28 +++++++++++++++-------------
 src/libs/routes/idhml.js | 26 ++++++++++++++------------
 2 files changed, 29 insertions(+), 25 deletions(-)

diff --git a/src/libs/routes/idhme.js b/src/libs/routes/idhme.js
index ac52c5d4..d762c95d 100644
--- a/src/libs/routes/idhme.js
+++ b/src/libs/routes/idhme.js
@@ -90,12 +90,12 @@ rqf.addField({
         relation: '=',
         type: 'integer',
         field: 'estado_id',
-        table: 'adh_idh_uf'
+        table: '@'
     },
     join: {
         primary: 'id',
         foreign: 'estado_id',
-        foreignTable: 'adh_idh_uf'
+        foreignTable: '@'
     }
 });
 
@@ -107,22 +107,24 @@ idhmeApp.get('/', rqf.parse(), (req, res, next) => {
             message: 'Wrong/No filter specified'
         });
     }
-    if ("state" in req.filter) {
-        // console.log("sim");
-        req.sql.from('adh_idh_uf')
-        .field('adh_idh_uf.idhm_e', 'total')
-        .field('adh_idh_uf.ano_censo', 'year')
-        .field('adh_idh_uf.estado_id', 'state_id');
-    } else if ("city" in req.filter) {
+
+    if (!("state" in req.filter) && !("city" in req.filter)) {
+        next({
+            status: 400,
+            message: 'Wrong/No filter specified'
+        });
+    }
+    if ("city" in req.filter) {
         req.sql.from('adh_idh')
         .field('adh_idh.idhm_e', 'total')
         .field('adh_idh.ano_censo', 'year')
         .field('adh_idh.municipio_id', 'city_id');
     } else {
-        next({
-            status: 400,
-            message: 'Wrong/No filter specified'
-        });
+        // console.log("sim");
+        req.sql.from('adh_idh_uf')
+        .field('adh_idh_uf.idhm_e', 'total')
+        .field('adh_idh_uf.ano_censo', 'year')
+        .field('adh_idh_uf.estado_id', 'state_id');
     }
     next();
 }, rqf.build(), query, response('idhme'));
diff --git a/src/libs/routes/idhml.js b/src/libs/routes/idhml.js
index fed978c8..81b920cd 100644
--- a/src/libs/routes/idhml.js
+++ b/src/libs/routes/idhml.js
@@ -90,12 +90,12 @@ rqf.addField({
         relation: '=',
         type: 'integer',
         field: 'estado_id',
-        table: 'adh_idh_uf'
+        table: '@'
     },
     join: {
         primary: 'id',
         foreign: 'estado_id',
-        foreignTable: 'adh_idh_uf'
+        foreignTable: '@'
     }
 });
 
@@ -107,21 +107,23 @@ idhmlApp.get('/', rqf.parse(), (req, res, next) => {
             message: 'Wrong/No filter specified'
         });
     }
-    if ("state" in req.filter) {
-        req.sql.from('adh_idh_uf')
-        .field('adh_idh_uf.idhm_l', 'total')
-        .field('adh_idh_uf.ano_censo', 'year')
-        .field('adh_idh_uf.estado_id', 'state_id');
-    } else if ("city" in req.filter) {
+    if (!("state" in req.filter) && !("city" in req.filter)) {
+        next({
+            status: 400,
+            message: 'Wrong/No filter specified'
+        });
+    }
+    if ("city" in req.filter) {
         req.sql.from('adh_idh')
         .field('adh_idh.idhm_l', 'total')
         .field('adh_idh.ano_censo', 'year')
         .field('adh_idh.municipio_id', 'city_id');
     } else {
-        next({
-            status: 400,
-            message: 'Wrong/No filter specified'
-        });
+        // console.log("sim");
+        req.sql.from('adh_idh_uf')
+        .field('adh_idh_uf.idhm_l', 'total')
+        .field('adh_idh_uf.ano_censo', 'year')
+        .field('adh_idh_uf.estado_id', 'state_id');
     }
     next();
 }, rqf.build(), query, response('idhml'));
-- 
GitLab