-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path72-server.js
157 lines (138 loc) · 3.38 KB
/
72-server.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
var express = require('express');
var bodyParser = require('body-parser');
var _ = require('underscore');
var db = require('./70-db.js');
var app = express();
var PORT = process.env.PORT || 3000;
var todos = [];
var todoNextId = 1;
// var todos = [ {
// "description": "Walk the dog",
// "completed": false,
// "id": 1
// },
// {
// "description": "talk to the Dog",
// "completed": true,
// "id": 2
// },
// {
// "description": "talk to the computer",
// "completed": true,
// "id": 3
// },
// {
// "description": "yell at the computer",
// "completed": false,
// "id": 4
// }];
// var todoNextId = 5;
app.use(bodyParser.json());
app.get('/', function (req, res) {
res.send('Todo API Root');
});
// GET /todos
// GET /todos?completed=[true|false]
// GET /todos?q=string
app.get('/todos', function (req, res) {
var query = req.query;
var where = {};
// /todos?completed=[true|false]
if (query.hasOwnProperty('completed')) {
if (query.completed.toLowerCase() === 'true') {
where.completed = true;
}
else if (query.completed.toLowerCase() === 'false') {
where.completed = false;
}
}
// /todos?q=string
if (query.hasOwnProperty('q') && query.q.length > 0) {
where.description = {
$like :'%' + query.q + '%'
};
}
db.todo.findAll({
where: where
}).then(function (todos) {
if (todos.length > 0) {
res.json(todos);
} else {
res.status(404).send();
}
}, function (e) {
res.status(500).json(e);
});
});
// GET /todos/# - display todo #
app.get('/todos/:id', function (req, res) {
var id = parseInt(req.params.id);
db.todo.findById(id).then(function (todo){
if (todo) {
res.json(todo.toJSON());
}
else {
res.status(404).send();
}
}, function (e) {
res.status(500).json(e);
});
});
// POST /todos - add incoming todo json to todos array
app.post('/todos', function (req, res) {
var body = _.pick(req.body, 'description', 'completed');
db.todo.create(body).then(function (todo){
res.json(todo.toJSON());
}, function (e) {
res.status(400).json(e);
});
});
// DELETE /todo/# - delete todo #
app.delete('/todo/:id', function (req, res) {
var id = parseInt(req.params.id, 10);
db.todo.destroy({
where: { id: id }
}).then(function (rowsDeleted){
if (rowsDeleted === 0) {
res.status(404).json({error: 'No todo with that id: '+id});
}
else {
res.status(204).send();
}
}, function (e) {
res.status(500).json(e);
});
});
// PUT /todos/# - update todo #
app.put('/todos/:id', function (req, res) {
var id = parseInt(req.params.id, 10);
var body = _.pick(req.body, 'description', 'completed');
var attributes = {};
if (body.hasOwnProperty('completed')) {
attributes.completed = body.completed;
}
if (body.hasOwnProperty('description')) {
attributes.description = body.description.trim();
}
db.todo.findById(id).then(function (todo){
if (todo) {
todo.update(attributes).then(function(todo) {
res.json(todo.toJSON());
}, function (e) {
res.status(400).json(e);
});
}
else {
res.status(404).send();
}
}, function () {
res.status(500).send();
});
});
db.sequelize.sync({
// force: true
}).then(function () {
app.listen(PORT, function () {
console.log('Express listening on port ' + PORT);
});
});