Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const express = require('express');
const idhmlApp = express.Router();
const libs = `${process.cwd()}/libs`;
const squel = require('squel');
const query = require(`${libs}/middlewares/query`);
const response = require(`${libs}/middlewares/response`);
const id2str = require(`${libs}/middlewares/id2str`);
const ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`);
let rqf = new ReqQueryFields();
idhmlApp.get('/year_range', (req, res, next) => {
req.sql.from('adh_idh')
.field('MIN(adh_idh.ano_censo)', 'start_year')
.field('MAX(adh_idh.ano_censo)', 'end_year');
next();
}, query, (req, res, next) => {
req.sql.from('adh_idh_uf')
.field('MIN(adh_idh_uf.ano_censo)', 'start_year')
.field('MAX(adh_idh_uf.ano_censo)', 'end_year');
req.old_result = req.result;
next();
}, query, (req, res, next) => {
// console.log(req.old_result[0].start_year);
// console.log(req.result[0].start_year);
if (req.old_result[0].start_year < req.result[0].start_year) {
req.result[0].start_year = req.old_result[0].start_year;
}
if (req.old_result[0].end_year > req.result[0].end_year) {
req.result[0].end_year = req.old_result[0].old_result;
}
next();
}, query, response('range'));
rqf.addField({
name: 'filter',
field: false,
where: true
}).addValue({
name: 'city',
table: 'municipio',
tableField: 'nome',
resultField: 'city_name',
where: {
relation: '=',
type: 'integer',
field: 'municipio_id',
table: 'adh_idh'
},
join: {
primary: 'id',
foreign: 'municipio_id',
foreignTable: 'adh_idh'
}
}).addValue({
name: 'min_year',
table: '@',
tableField: 'ano_censo',
resultField: 'year',
where: {
relation: '>=',
type: 'integer',
table: '@',
field: 'ano_censo'
}
}).addValue({
name: 'max_year',
table: '@',
tableField: 'ano_censo',
resultField: 'year',
where: {
relation: '<=',
type: 'integer',
table: '@',
field: 'ano_censo'
}
}).addValue({
name: 'state',
table: 'estado',
tableField: 'nome',
resultField: 'state_name',
where: {
relation: '=',
type: 'integer',
field: 'estado_id',
table: 'adh_idh_uf'
},
join: {
primary: 'id',
foreign: 'estado_id',
foreignTable: 'adh_idh_uf'
}
});
idhmlApp.get('/', rqf.parse(), (req, res, next) => {
if(typeof req.filter === 'undefined' || Object.keys(req.filter).length === 0) {
res.status(400);
next({
status: 400,
message: 'Wrong/No filter specified'
});
}
if ("state" in req.filter) {
req.sql.from('adh_idh_uf')
.field('adh_idh_uf.ano_censo', 'year')
.field('adh_idh_uf.estado_id', 'state_id');
} else if ("city" in req.filter) {
req.sql.from('adh_idh')
.field('adh_idh.ano_censo', 'year')
.field('adh_idh.municipio_id', 'city_id');
} else {
next({
status: 400,
message: 'Wrong/No filter specified'
});
}
next();
}, rqf.build(), query, response('idhml'));
module.exports = idhmlApp;