-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.js
34 lines (29 loc) · 1003 Bytes
/
todo.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
module.exports = function(app){
app.get('/todo', index);
app.post('/todo/addItem', addItem);
app.post('/todo/changeItem', changeItem);
app.post('/todo/clearItems', clearItems);
var collections = ["todo"]
var mongo = require("mongojs")
var db = mongo.connect("localhost", collections)
function index(req, res){
db.todo.find().sort({"_id":1}, function(err, items){
res.render('todo', { title: 'TODO List', items: items })
})
};
function addItem(req, res) {
var item = req.body.item
item.checked = false
db.todo.save(req.body.item, function() {
res.render('todoItem', { item: item })
})
}
function changeItem(req, res) {
db.todo.update({_id: mongo.ObjectId(req.body.id)}, { $set: { checked : req.body.item.checked == "true" } })
}
function clearItems(req, res) {
db.todo.remove({}, function() {
res.json({ status: "ok"})
})
}
}