Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
simcaq-node
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
simcaq
simcaq-node
Commits
7e4819e9
There was a problem fetching the pipeline summary.
Unverified
Commit
7e4819e9
authored
8 years ago
by
João Victor Risso
Browse files
Options
Downloads
Patches
Plain Diff
Refactor enrollments route to use function chaining
parent
5ebad2f8
No related branches found
No related tags found
1 merge request
!7
Refactor enrollments route to include query building
Pipeline
#
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
libs/routes/api.js
+92
-25
92 additions, 25 deletions
libs/routes/api.js
with
92 additions
and
25 deletions
libs/routes/api.js
+
92
−
25
View file @
7e4819e9
'
use strict
'
;
var
express
=
require
(
'
express
'
)
var
express
=
require
(
'
express
'
)
var
xml
=
require
(
'
js2xmlparser
'
)
var
xml
=
require
(
'
js2xmlparser
'
)
var
router
=
express
.
Router
()
var
router
=
express
.
Router
()
...
@@ -56,39 +58,34 @@ router.get('/data', function(req, res) {
...
@@ -56,39 +58,34 @@ router.get('/data', function(req, res) {
})
})
})
})
router
.
get
(
'
/enrollments
'
,
function
(
req
,
res
)
{
router
.
get
(
'
/enrollments
'
,
function
(
req
,
res
,
next
)
{
var
params
=
req
.
query
;
const
params
=
req
.
query
;
var
id
=
0
;
var
location_id
=
0
;
var
adm_dependency_id
=
0
;
var
census_year
=
0
;
var
enrollmentSql
=
""
;
if
(
params
.
id
)
if
(
params
.
id
)
{
{
req
.
id
=
parseInt
(
params
.
id
,
10
);
id
=
parseInt
(
params
.
id
,
10
);
}
}
if
(
params
.
location_id
)
if
(
params
.
location_id
)
{
{
req
.
location_id
=
parseInt
(
params
.
location_id
,
10
);
location_id
=
parseInt
(
params
.
location_id
,
10
);
}
}
if
(
params
.
adm_dependency_id
)
if
(
params
.
adm_dependency_id
)
{
{
req
.
adm_dependency_id
=
parseInt
(
params
.
adm_dependency_id
,
10
);
adm_dependency_id
=
parseInt
(
params
.
adm_dependency_id
,
10
);
}
}
if
(
params
.
census_year
)
if
(
params
.
census_year
)
{
{
req
.
census_year
=
parseInt
(
params
.
census_year
,
10
);
census_year
=
parseInt
(
params
.
census_year
,
10
);
}
}
/**
if
(
params
.
aggregate
)
{
* FIXME: parameter substitution in the queries is not safe (vulnerable to
log
.
debug
(
'
aggregate parameter detected
'
);
* SQL injection). Substitution from MonetDB module is not working for some
next
(
'
route
'
);
* reason.
}
else
{
*/
log
.
debug
(
'
No aggregate parameter detected
'
);
next
();
}
/*
switch(params.aggregate)
switch(params.aggregate)
{
{
case "city":
case "city":
...
@@ -132,7 +129,77 @@ router.get('/enrollments', function(req, res) {
...
@@ -132,7 +129,77 @@ router.get('/enrollments', function(req, res) {
});
});
}
}
log.debug("All resources were released");
log.debug("All resources were released");
}, function(error) {
});
});
})
*/
},
function
(
req
,
res
,
next
)
{
/** When no +aggregate+ parameter value is specified on the request, then
* assign the query to compute the result for the whole country.
*/
log
.
debug
(
'
Using SQL query for the whole country
'
);
req
.
sql_query
=
'
SELECT * FROM turmas LIMIT 1
'
;
next
(
'
route
'
);
});
router
.
get
(
'
/enrollments
'
,
function
(
req
,
res
,
next
)
{
const
params
=
req
.
query
;
if
(
!
params
.
aggregate
)
{
next
(
'
route
'
);
}
else
if
(
params
.
aggregate
==
'
region
'
)
{
log
.
debug
(
'
Using enrollments query for regions
'
);
req
.
sql_query
=
'
SELECT * FROM turmas LIMIT 1
'
;
}
next
(
'
route
'
);
});
router
.
get
(
'
/enrollments
'
,
function
(
req
,
res
,
next
)
{
const
params
=
req
.
query
;
if
(
!
params
.
aggregate
)
{
next
(
'
route
'
);
}
else
if
(
params
.
aggregate
==
'
state
'
)
{
log
.
debug
(
'
Using enrollments query for states
'
);
req
.
sql_query
=
'
SELECT * FROM turmas LIMIT 1
'
;
}
next
(
'
route
'
);
});
router
.
get
(
'
/enrollments
'
,
function
(
req
,
res
,
next
)
{
const
params
=
req
.
query
;
if
(
!
params
.
aggregate
)
{
next
(
'
route
'
);
}
else
if
(
params
.
aggregate
==
'
city
'
)
{
log
.
debug
(
'
Using enrollments query for cities
'
);
req
.
sql_query
=
'
SELECT * FROM turmas LIMIT 1
'
;
}
next
(
'
route
'
);
});
router
.
get
(
'
/enrollments
'
,
function
(
req
,
res
,
next
)
{
log
.
debug
(
'
Request parameters: ${req}?
'
);
if
(
!
req
.
sql_query
)
{
/* Should only happen if there is a bug in the chaining of the
* '/enrollments' route, since when no +aggregate+ parameter is given,
* it defaults to use the query for the whole country.
*/
log
.
error
(
'
BUG -- No SQL query was found to be executed!
'
);
res
.
status
(
501
).
end
();
}
else
{
log
.
debug
(
'
SQL query: ${req.sql_query}?
'
);
conn
.
query
(
req
.
sql_query
,
true
).
then
(
function
(
result
)
{
log
.
debug
(
result
);
if
(
req
.
query
.
format
===
'
csv
'
)
{
res
.
csv
(
result
.
data
);
}
else
if
(
req
.
query
.
format
===
'
xml
'
)
{
res
.
send
(
xml
(
'
result
'
,
JSON
.
stringify
({
enrollments
:
result
.
data
})));
}
else
{
res
.
json
({
result
:
result
.
data
});
}
},
function
(
error
)
{
log
.
error
(
'
SQL query error: ${error}?
'
);
res
.
status
(
501
).
end
();
});
}
});
module
.
exports
=
router
module
.
exports
=
router
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment