This repository has been archived by the owner on Jan 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
49 lines (43 loc) · 1.82 KB
/
main.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
'use strict';
require('dotenv').load();
const logger = require('./lib/logger');
const express = require('express');
const cors = require('cors');
const Q = require('q');
const api = require('./lib/model/api');
api.load();
// Create the app and listen for API requests
let app = express();
app.use(cors());
app.get('/api/events/search/:text/', (req, res) => {
let redex = `.*${req.params.text}.*`;
let page = req.query.page;
let limit = req.query.limit;
let redexQuery = {$regex: redex, $options: 'i'};
let queryTitle = {$or: [
{title: redexQuery},
{description: redexQuery}
]};
return Q.all([
api.getEventForRelationship(api.model.models.Tag, 'tags', {name: redexQuery}, page, limit),
api.getEventForRelationship(api.model.models.Category, 'categories', {name: redexQuery}, page, limit),
api.execQuery(api.model.models.Event, queryTitle, page, limit)
])
.then((querys) => {
return {tags: querys[0], categories: querys[1], title: querys[2]};
})
.then((events) => res.status(200).json(events))
.catch((err) => res.status(500).json(err));
});
app.get('/api/events/:long/:lat/', (req, res) => {
let distance = parseFloat(req.query.distance || process.env.DISTANCE);
let cords = [parseFloat(req.params.long), parseFloat(req.params.lat)];
let query = {location: {$near: {$geometry: {type: 'Point', coordinates: cords}, $maxDistance: distance}}};
return api.execQuery(api.model.models.Event, query, req.query.page, req.query.limit)
.then((events) => res.status(200).json(events))
.catch((err) => res.status(500).json(err));
});
app.use('/api', api.baucis());
app.listen(process.env.PORT, process.env.HOST, () => {
logger.info(`eventoL-api listening on port ${process.env.PORT} and host ${process.env.HOST}`)
});