-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
95 lines (64 loc) · 1.97 KB
/
app.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
var express = require('express')
, mongolian = require('mongolian')
, app = express.createServer()
var MONGO_DB = process.env.MONGOHQ_URL || 'mongodb://localhost/the-issues'
var PORT = process.env.PORT || 2222
var db = new mongolian(MONGO_DB)
var ObjectId = mongolian.ObjectId
ObjectId.prototype.toJSON = ObjectId.prototype.toString
app.configure('development', function() {
app.use(express.bodyParser())
app.use(express.cookieParser())
app.use(express.session({secret: "sooo secret"}))
app.use(express.static(__dirname + '/public'))
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }))
});
//// JSON API ////
var collection = db.collection("issues")
app.get("/issues", function(req, res) {
collection.find().toArray(function(err, issues) {
res.send(issues)
})
})
app.get("/issues/:id", function(req, res) {
collection.findOne({_id: new ObjectId(req.params.id)}, function(err, issue) {
res.send(issue)
})
})
app.del("/issues/:id", function(req, res) {
collection.remove({_id: new ObjectId(req.params.id)}, function(err) {
res.send(200)
})
})
app.post("/issues", function(req, res) {
var issue = req.body
issue.first.votes = 0
issue.second.votes = 0
issue.votes = []
issue.created = new Date()
collection.save(issue, function(err, data) {
res.send(200)
})
})
app.post("/issues/:id/votes", function(req, res) {
var vote = req.body
vote.created = new Date()
collection.findOne({_id: new ObjectId(req.params.id)}, function(err, issue) {
issue.votes.push(vote)
var option = (issue.first.name == vote.name) ? issue.first : issue.second
option.votes++
collection.save(issue, function(err, issue) {
res.send(200)
})
})
})
app.post("/login/:username", function(req, res) {
req.session.username = req.param.username
res.send(200)
})
app.get("/login", function(req, res) {
res.send(req.session.username)
})
app.listen(PORT, function() {
console.log("Listening on " + PORT)
});