-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathroutes.js
86 lines (76 loc) · 2.37 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
75
76
77
78
79
80
81
82
83
84
85
86
module.exports = function(app, talks) {
var _ = require("underscore");
app.get("/api/talks", function(req, res, next) {
talks.find({}).sort({ "added": -1 }).exec(function (err, docs) {
res.send(docs);
});
});
app.get("/api/locations", function(req, res, next) {
talks.find({}, function (err, docs) {
var locations = [];
for(var i = 0; i < docs.length; i++) {
locations.push(docs[i].location);
}
res.send(uniqSort(locations));
});
});
app.get("/api/speakers", function(req, res, next) {
talks.find({}, function (err, docs) {
var speakers = [];
for(var i = 0; i < docs.length; i++) {
for(var j = 0; j < docs[i].speakers.length; j++) {
speakers.push(docs[i].speakers[j]);
}
}
res.send(uniqSort(speakers));
});
});
app.get("/api/tags", function(req, res, next) {
talks.find({}, function (err, docs) {
var tags = [];
for(var i = 0; i < docs.length; i++) {
for(var j = 0; j < docs[i].tags.length; j++) {
tags.push(docs[i].tags[j]);
}
}
res.send(uniqSort(tags));
});
});
app.get("/api/locations/:location", function(req, res, next) {
talks.find({ "location.key": req.params.location }).sort({ added: -1 }).exec(function (err, docs) {
res.send(docs);
});
});
app.get("/api/speakers/:speaker", function(req, res, next) {
talks.find({ "speakers.key": req.params.speaker }).sort({ added: -1 }).exec(function (err, docs) {
res.send(docs);
});
});
app.get("/api/tags/:tag", function(req, res, next) {
talks.find({ "tags.key": req.params.tag }).sort({ added: -1 }).exec(function (err, docs) {
res.send(docs);
});
});
app.get("/api/search", function(req, res, next) {
if(req.query.query) {
var conditions = [];
var split = req.query.query.split(" ");
for(var i = 0; i < split.length; i++) {
if(split[i].length > 0) {
conditions.push({ "title": new RegExp(split[i], "i") });
conditions.push({ "description": new RegExp(split[i], "i") });
}
}
talks.find({ $or: conditions }, function (err, docs) {
res.send(docs);
});
} else {
res.send([]);
}
});
function uniqSort(arr) {
return _.uniq(_.sortBy(arr, "title"), true, function(item, key, arr) {
return item.key;
});
}
}