-
-
Notifications
You must be signed in to change notification settings - Fork 214
/
routes.js
74 lines (61 loc) · 1.93 KB
/
routes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const express = require('express');
const router = express.Router();
const dbOperations = require('./db/db-operations');
// request to /civilian.html?userId=ashwin will render our civilian.html page
router.get('/civilian.html', (req, res) => {
res.render('civilian.html', {
userId: req.query.userId
});
});
router.get('/cop.html', (req, res) => {
res.render('cop.html', {
userId: req.query.userId
});
});
router.get('/data.html', (req, res) => {
res.render('data.html');
});
router.get('/cops', async (req, res) => {
/*
extract the latitude and longitude info from the request query params.
Then, fetch the nearest cops using MongoDB's geospatial queries and return it back to the client.
*/
const latitude = Number(req.query.lat);
const longitude = Number(req.query.lng);
const nearestCops = await dbOperations.fetchNearestCops([longitude, latitude], 2000);
res.json({
cops: nearestCops
});
});
router.get('/cops/info', async (req, res) => {
const userId = req.query.userId // xtract userId from query params
const copDetails = await dbOperations.fetchCopDetails(userId);
res.json({
copDetails: copDetails
});
});
// fetch all requests
router.get('/requests/info', async (req, res) => {
const results = await dbOperations.fetchRequests();
const features = [];
for (let i = 0; i < results.length; i++) {
features.push({
type: 'Feature',
geometry: {
type: 'Point',
coordinates: results[i].location.coordinates
},
properties: {
status: results[i].status,
requestTime: results[i].requestTime,
address: results[i].location.address
}
});
}
const geoJsonData = {
type: 'FeatureCollection',
features: features
}
res.json(geoJsonData);
});
module.exports = router;