diff options
| author | Samarth-Agarwal1 <agarwalsamarth@hotmail.com> | 2019-03-26 21:13:01 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-03-26 21:13:01 +0000 |
| commit | 933d8276bde1617e3d7cc4aec53c9ba7112ef11b (patch) | |
| tree | 4f181114e3a1b9fcf6442b9a96424f31aabd437c /web | |
| parent | 8c81bbac1024e6f8c0b378e80da6bb774c56f92a (diff) | |
| parent | c0723a3b208c560e183aa78464699d6d8e4ce8ef (diff) | |
Merge branch 'qa' into feat/30
Diffstat (limited to 'web')
| -rw-r--r-- | web/package.json | 3 | ||||
| -rw-r--r-- | web/routes/index.js | 30 |
2 files changed, 25 insertions, 8 deletions
diff --git a/web/package.json b/web/package.json index 9278b90..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", "mongodb": "*", "mongoose": "*", "morgan": "~1.9.0", - "pug": "^2.0.3" + "pug": "2.0.0-beta11" } } diff --git a/web/routes/index.js b/web/routes/index.js index 23fcf36..0b32d0e 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(422).send({ error: errMssg }); + } + + if (!glookup.hasContainers(point)) { + var errMssg = "Sorry, we currently only support locations strictly within the City of Toronto."; + return res.status(422).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(); |
