diff options
| author | Kumar Damani <kumar.damani@mail.utoronto.ca> | 2019-03-25 15:10:03 +0000 |
|---|---|---|
| committer | Kumar Damani <kumar.damani@mail.utoronto.ca> | 2019-03-25 15:10:03 +0000 |
| commit | c093b27e101f19e7667124479aa5c4baaf33adad (patch) | |
| tree | be74c6fd26bd7ab92b59b9cf889a6c554218986a /web | |
| parent | 7c98e3c782f3d8da4cc289dce9ecb0a6cfd9b26d (diff) | |
added data validation before inserting into db
Diffstat (limited to 'web')
| -rw-r--r-- | web/package.json | 7 | ||||
| -rw-r--r-- | web/routes/index.js | 30 |
2 files changed, 27 insertions, 10 deletions
diff --git a/web/package.json b/web/package.json index a669ecb..8a59f6e 100644 --- a/web/package.json +++ b/web/package.json @@ -9,10 +9,11 @@ "cookie-parser": "~1.4.3", "debug": "~2.6.9", "express": "~4.16.0", + "geojson-geometries-lookup": "^0.3.0", "http-errors": "~1.6.2", - "morgan": "~1.9.0", - "pug": "2.0.0-beta11", "mongodb": "*", - "mongoose": "*" + "mongoose": "*", + "morgan": "~1.9.0", + "pug": "2.0.0-beta11" } } 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(); |
