aboutsummaryrefslogtreecommitdiff
path: root/web/routes/index.js
diff options
context:
space:
mode:
authorKumar Damani <kumar.damani@mail.utoronto.ca>2019-03-25 15:10:03 +0000
committerKumar Damani <kumar.damani@mail.utoronto.ca>2019-03-25 15:10:03 +0000
commitc093b27e101f19e7667124479aa5c4baaf33adad (patch)
treebe74c6fd26bd7ab92b59b9cf889a6c554218986a /web/routes/index.js
parent7c98e3c782f3d8da4cc289dce9ecb0a6cfd9b26d (diff)
added data validation before inserting into db
Diffstat (limited to 'web/routes/index.js')
-rw-r--r--web/routes/index.js30
1 files changed, 23 insertions, 7 deletions
diff --git a/web/routes/index.js b/web/routes/index.js
index 23fcf36..3c066a9 100644
--- a/web/routes/index.js
+++ b/web/routes/index.js
@@ -2,7 +2,13 @@ var express = require('express');
var router = express.Router();
var Mockpoints = require('../models/mockpoints');
var Points = require('../models/points');
+var fs = require("fs");
+var GeoJsonGeometriesLookup = require('geojson-geometries-lookup');
+var torontoNeighbourhoords = JSON.parse(fs.readFileSync(__dirname + '/../../data/Neighbourhoods.geojson', "utf8"));
+
const connections = [];
+const glookup = new GeoJsonGeometriesLookup(torontoNeighbourhoords);
+
/* GET home page. */
router.get('/', function(req, res, next) {
// for now just redirect to /map
@@ -23,24 +29,34 @@ router.get('/map', function(req,res) {
});
router.post('/mobilerequest', function(req, res) {
- var data = req.body;
- Points.save_request(data, function(err, result) {
+ var point = req.body;
+
+ if (!point.coordinates.length) {
+ var errMssg = "Coordinates field must not be empty in a request.";
+ return res.status(200).send({ error: errMssg });
+ }
+
+ if (!glookup.hasContainers(point)) {
+ var errMssg = "Sorry, we currently only support locations strictly within the City of Toronto";
+ return res.status(200).send({ error: errMssg });
+ }
+
+ // here we are guaranteed to have a valid point in Toronto
+ Points.save_request(point, function(err, result) {
if (err) {
console.log(`err inserting mobile request into db: ${err}`);
- res.status(500).send({ error: "boo:(" });
+ res.status(500).send({ error: "Sorry, something failed on our end." });
} else {
// send event to all connections
for(var i = 0; i < connections.length; i++) {
- connections[i].sseSend(data);
+ connections[i].sseSend(point);
}
console.log(result);
- res.status(200).send({'status': 'success', 'data': data});
+ res.status(201).send({'status': 'success'});
}
});
-
});
-
router.get('/stream', function(req, res){
// set up server side event (communication line between front end and server)
res.sseSetup();