-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
69 lines (61 loc) · 2.21 KB
/
index.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
'use strict';
const joi = require('joi');
const db = require('@arangodb').db;
const aql = require('@arangodb').aql;
const errors = require('@arangodb').errors;
const DOC_NOT_FOUND = errors.ERROR_ARANGO_DOCUMENT_NOT_FOUND.code;
const restaurants = db._collection('restaurants');
const neighborhoods = db._collection('neighborhoods');
const createRouter = require('@arangodb/foxx/router');
const router = createRouter();
module.context.use(router);
router.get('/restaurants', function (req, res) {
try {
// const data = foxxColl.document(req.pathParams.key);
const data = restaurants.any();
res.send(data);
} catch (e) {
if (!e.isArangoError || e.errorNum !== DOC_NOT_FOUND) {
throw e;
}
res.throw(404, 'The entry does not exist', e);
}
})
.response(joi.object().required(), 'Returns a random entry.')
.summary('Retrieve a random restaurant entry.')
.description('Returns a random restaurant entry of the restaurants collection.');
router.get('/neighborhoods', function (req, res) {
try {
// const data = foxxColl.document(req.pathParams.key);
const data = neighborhoods.any();
res.send(data);
} catch (e) {
if (!e.isArangoError || e.errorNum !== DOC_NOT_FOUND) {
throw e;
}
res.throw(404, 'The entry does not exist', e);
}
})
.response(joi.object().required(), 'Returns a random entry.')
.summary('Retrieve a random neighborhood entry.')
.description('Returns a random neighborhoods entry of the neighborhoods collection.');
router.get('/pointsInNeighborhood/:id', function (req, res) {
try {
var id = req.pathParams.id;
var neighborhood = neighborhoods.document(id);
const keys = db._query(aql`
FOR restaurant IN restaurants
FILTER IS_IN_POLYGON(${neighborhood.geometry.coordinates[0]}, restaurant.location.coordinates[0], restaurant.location.coordinates[1])
RETURN restaurant
`);
res.send(keys);
} catch (e) {
if (!e.isArangoError || e.errorNum !== DOC_NOT_FOUND) {
throw e;
}
res.throw(404, 'The entry does not exist', e);
}
})
.response(joi.object().required(), 'Returns restaurants within a given neighborhood.')
.summary('Restaurants in a neighborhood.')
.description('Returns restaurants within a given neighborhood.');