-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroutes.js
93 lines (66 loc) · 2.4 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
87
88
89
90
91
92
93
module.exports = function(app){
// Homepage
app.get('/', function(req, res){
res.render('home');
// Find all dilemmas
/*dilemmas.find({}, function(err, all_dilemmas){
res.render('home');
// Find the current user
users.find({ip: req.ip}, function(err, u){
var voted_on = [];
if(u.length == 1){
voted_on = u[0].votes;
}
// Find which dilemmas the user hasn't still voted on
var not_voted_on = all_dilemmas.filter(function(dilemma){
return voted_on.indexOf(dilemma._id) == -1;
});
var dilemma_to_show = null;
if(not_voted_on.length > 0){
// Choose a random dilemma from the array
dilemma_to_show = not_voted_on[Math.floor(Math.random()*not_voted_on.length)];
}
res.render('home');
});
}); */
});
// This is executed before the next two post requests
app.post('*', function(req, res, next){
// Register the user in the database by ip address
users.insert({
ip: req.ip,
votes: []
}, function(){
// Continue with the other routes
next();
});
});
app.post('/like', vote);
app.post('/dislike', vote);
function vote(req, res){
// Which field to increment, depending on the path
var what = {
'/like': {a:1},
'/dislike': {b:1}
};
// Find the dilemma, increment the vote counter and mark that the user has voted on it
var time = Date();
console.log(time);
dilemmas.find({ name: req.body.dilemma }, function(err, found){
if(found.length == 1){
dilemmas.update(found[0], {$inc : what[req.path]});
//dilemmas.update(found[0], {time : time});
//var current = dilemmas.find({name: req.body.dilemma});
console.log("current:");
console.log(found);
found.time = time;
users.update({ip: req.ip}, { $addToSet: { votes: found[0]._id}}, function(){
res.redirect('../');
});
}
else{
res.redirect('../');
}
});
};
};