Skip to content
Snippets Groups Projects
Commit 60745534 authored by Leon A. Okida Gonçalves's avatar Leon A. Okida Gonçalves
Browse files

Add functional version of middleware, add flag to check whether it runs...

Add functional version of middleware, add flag to check whether it runs default query or modified query
parent 413cff49
No related branches found
No related tags found
4 merge requests!377prd_version of simcaq,!368update filter in v2 school,!348Merge branch development into homologa,!343Add functional version of the middleware that changes the default query based on request body
......@@ -2,6 +2,32 @@ class ReqBody {
constructor() {
}
add_metrics(req, column, metricsArray) {
for (let i in metricsArray) {
switch(metricsArray[i]["function"]) {
case "max":
req.sql.field("max(" + column + ")", metricsArray[i]["return_name"]);
req.hasMetrics = true;
break;
case "min":
req.sql.field("min(" + column + ")", metricsArray[i]["return_name"]);
req.hasMetrics = true;
break;
case "count":
req.sql.field("count(" + column + ")", metricsArray[i]["return_name"]);
req.hasMetrics = true;
break;
case "sum":
req.sql.field("sum(" + column + ")", metricsArray[i]["return_name"]);
req.hasMetrics = true;
break;
default:
break;
}
}
}
parse() {
return(req, res, next) => {
// Gets body of the HTTP requisition
......@@ -10,8 +36,17 @@ class ReqBody {
// Chooses operation based on the mode field of the body
switch(body["mode"]) {
case "add_metrics":
console.log(body);
// adds flag to check whether it runs the default query or not
req.hasMetrics = false;
// Gets all column names
let columns = Object.keys(body["add_metrics"]);
// Calls function to add metrics that were specified to req.sql
for (let i in columns) {
this.add_metrics(req, columns[i], body["add_metrics"][columns[i]]);
}
break;
default:
break;
}
......
......@@ -176,12 +176,21 @@ rqf.addField({
});
testApp.get('/', rqf.parse(), rqf.build(), reqBody.parse(), (req, res, next) => {
// Runs default query
if (!req.hasMetrics) {
req.sql.from('escola')
.field('ano_censo')
.field('count(*)', 'total')
.group('ano_censo').order('ano_censo')
.where('escola.situacao_funcionamento_pareada = 1 AND (escola.ensino_regular = 1 OR escola.ensino_eja=1 or escola.educacao_profissional=1)')
console.log(req.sql.toString());
}
// Runs modified query
else {
req.sql.from('escola')
.field('ano_censo')
.group('ano_censo').order('ano_censo')
.where('escola.situacao_funcionamento_pareada = 1 AND (escola.ensino_regular = 1 OR escola.ensino_eja=1 or escola.educacao_profissional=1)')
}
next();
}, query, response('school'));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment