-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
269 lines (232 loc) · 7.09 KB
/
api.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/**
* Handles all routing and logic of the API
* First half defines routing, the second half declares functions
*/
// External dependencies
const path = require('path');
const express = require('express');
var router = express.Router();
module.exports = function(io, db) {
// Routing
router.route('/')
.get(GETapiGuide);
router.route('/bobs')
.get(GETallBobs)
.post(POSTcreateNewBob);
router.route('/bobs/active')
.get(GETallActiveBobs);
router.route('/bobs/flagged')
.get(ensureAuthenticated, GETallFlaggedBobs);
router.route('/bobs/:bobid')
.get(GETbob)
.put(PUTbob)
.delete(ensureAuthenticated, DELETEbob)
.post(function(req, res) {
res.status(405)
.set('Access-Control-Allow-Methods', 'GET, PUT, DELETE')
.send("error: cannot POST, use PUT to edit bobs");
});
router.route('/bobs/:bobid/votes')
.get(GETvotes)
.post(POSTupvoteBob);
router.route('/bobs/:bobid/flags')
.get(GETflag)
.post(POSTflagBob);
router.route('/flavors')
.get(GETflavors);
router.route('/flavors/:flavorname')
.get(GETflavor);
router.route('/tags')
.get(GETtags);
router.route('/tags/:tagid')
.get(GETtag);
// API functions
/**
* Checks for authentication.
* Currently only checks for req.headers.auth, in the future it will use a more robust authentication method
*/
const adminPassword = process.env.ADMIN_PASSWORD;
function ensureAuthenticated(req, res, next) {
if (req.headers.auth === adminPassword | req.query.auth === adminPassword){
next();
} else {
res.status(401).send("User not authenticated");
}
}
function GETapiGuide(req, res) {
res.sendFile(path.join(__dirname, '..', '/templates/api.html'));
}
function GETallActiveBobs(req, res) {
db.Bob.getActiveBobs().then(function(bobs) {
res.send(bobs);
});
}
function GETallBobs(req, res) {
db.Bob.getBobs().then(function(bobs) {
res.send(bobs);
});
}
function GETallFlaggedBobs(req, res) {
db.Bob.getFlaggedBobs().then(function(bobs) {
res.send(bobs);
});
}
function POSTcreateNewBob(req, res, _next) {
if (!req.body.endDate){
req.body.endDate = Date.now() + 2 * 60 * 60 * 12; // Default to two days
}
var bob = {
data: req.body.data,
flavor: req.body.flavor,
description: req.body.description,
startDate: req.body.startDate,
endDate: req.body.endDate,
tags: req.body.tags
};
// Save bob in db
db.Bob.saveBob(bob).then(function success(bobData) {
// Send to all boards if the media is ready
if (bobData.mediaReady){
io.emit('add_element', bobData);
}
res.send("success");
}, function error(err) {
res.status(500).send(err);
});
}
// Sends back one bob by id
function GETbob(req, res) {
if (!req.params.bobid) return res.status(400).send("bob id not defined");
db.Bob.getOneBob({ _id: db.ObjectId(req.params.bobid) } ).then(function success(data) {
res.send(data);
}, function error(err) {
res.status(500).send(err);
});
}
function GETvotes(req, res) {
if (!req.params.bobid) return res.status(404).send("bob not found");
db.Bob.getOneBob(db.ObjectId(req.params.bobid)).then(function success(data) {
if (data){
res.send({ votes: data.votes });
} else {
res.status(404).send("bob not found");
}
}, function error(err) {
res.status(500).send(err);
});
}
function POSTupvoteBob(req, res) {
db.Bob.upvoteBob(db.ObjectId(req.params.bobid)).then(function success(data) {
io.emit('upvote', { id:req.params.bobid, votes: data.votes + 1 });
if (data){
res.send("upvoted");
} else {
res.status(404).send("bob not found");
}
}, function error(err) {
res.status(500).send(err);
});
}
function GETflag(req, res) {
db.Bob.getOneBob(db.ObjectId(req.params.bobid)).then(function success(data) {
if (data){
res.send({ flag: data.flag });
} else {
res.send("bob not found");
}
}, function error(err) {
res.status(500).send(err);
});
}
function POSTflagBob(req, res) {
db.Bob.flagBob(db.ObjectId(req.params.bobid)).then(function success(data) {
if (data){
io.emit('delete', req.params.bobid);
res.send("flagged");
} else {
console.log("searching for bob");
db.Bob.getOneBob(db.ObjectId(req.params.bobid)).then(function success(data) {
if (data){
res.send({ flag: data.flag });
} else {
res.send("bob not found");
}
});
// flagBob searches for bobId and flag: 0.
// It returns null if the bob does not exist, or if has already been flagged
res.send("bob already flagged");
}
}, function error(err) {
res.status(500).send(err);
});
}
// Updates an existing bob
function PUTbob(req, res) {
var bob = {
_id: db.ObjectId(req.body.id), // Used for identifying bob in db.Bob.updateBob
data: req.body.data,
flavor: req.body.flavor, // Is not updated in db.Bob.updateBob
startDate: req.body.startDate,
endDate: req.body.endDate,
tags: req.body.tags
};
db.Bob.updateBob(bob).then(function success(data) {
io.emit('update_element', data);
res.send("update successful");
}, function error(err) {
res.status(500).send(err);
});
}
// Delete a bob by id
function DELETEbob(req, res) {
db.Bob.deleteBob(db.ObjectId(req.params.bobid)).then(function success(data) {
io.emit('delete_element', data);
res.send("Success");
}, function error(err) {
res.status(500).send(err);
});
}
// Get all flavors
function GETflavors(req, res) {
db.Flavors.getFlavors().then(function success(data) {
res.send(data);
}, function error(err) {
res.status(500).send(err);
});
}
// Get one flavor by name or id
function GETflavor(req, res) {
db.Flavors.getFlavor({ name: req.params.flavorname }).then(function success(data) {
// ObjectId is 24 characters long. If nothing is found by name, check by _id
if (data === null && req.params.flavorname.length == 24){
db.Flavors.getFlavor({ _id: db.ObjectId(req.params.flavorname) }).then(function success(data) {
res.send(data);
}, function error(err) {
res.status(500).send(err);
});
} else {
res.send(data);
}
}, function error(err) {
res.status(500).send(err);
});
}
// Get all tags
function GETtags(req, res) {
db.Tag.getTags().then(function success(data) {
res.send(data);
}, function error(err) {
res.status(500).send(err);
});
}
// Get a single tag by id
function GETtag(req, res) {
db.Tag.getTag({ _id: db.ObjectId(req.params.tagid) }).then(function success(data) {
res.send(data);
}, function error(err) {
res.status(500).send(err);
});
}
router.GETallBobs = GETallBobs;
return router;
};