-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
297 lines (252 loc) · 8.91 KB
/
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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
var express = require('express');
var bodyParser = require('body-parser');
var _ = require('underscore');
var db = require('./db.js');
var bcrypt = require('bcrypt');
var middleware = require('./middleware.js')(db);
var app = express();
var PORT = process.env.PORT || 3000;
var todos = [];
var todoNextId = 1;
app.use(bodyParser.json()); // can then access using req.body
app.get('/', function (req, res) {
res.send('Todo API Root');
});
// GET /todos
app.get('/todos', middleware.requireAuthentication, function (req, res) {
var query = req.query;
var where = {
userId: req.user.get('id')
};
if (query.hasOwnProperty('completed') && query.completed === 'true') {
where.completed = true;
} else if (query.hasOwnProperty('completed') && query.completed === 'false') {
where.completed = false;
}
if (query.hasOwnProperty('q') && query.q.length > 0) {
where.description = {
$like: '%' + query.q + '%'
};
}
db.todo.findAll({
where: where
}).then(function (todos) {
res.json(todos);
}, function (e) {
res.status(500).send();
});
// var queryParams = req.query; // e.g. /todos?completed=true
// // note: will receive "true" or "false" as a string, ~bool
// var filteredTodos = todos;
// var body = _.pick(req.body, 'description', 'completed');
// if (queryParams.hasOwnProperty('completed') && queryParams.completed === 'true') {
// filteredTodos = _.where(filteredTodos, {completed: true});
// } else if (queryParams.hasOwnProperty('completed') && queryParams.completed === 'false') {
// filteredTodos = _.where(filteredTodos, {completed: false})
// }
// if (queryParams.hasOwnProperty('q') && queryParams.q.length > 0) {
// filteredTodos = _.filter(filteredTodos, function (todo) {
// return todo.description.toLowerCase().indexOf(queryParams.q.toLowerCase()) >= 0;
// });
// }
// res.json(filteredTodos); // don't need to use JSON.stringify...
})
// GET /todos/:id
app.get('/todos/:id', middleware.requireAuthentication, function (req, res) {
var todoId = parseInt(req.params.id, 10);
// db.todo.findById(todoId).then(function (todo) {
db.todo.findOne({
where: {
id: todoId,
userId: req.user.get('id')
}}).then(function (todo) {
if (!!todo) {
res.json(todo.toJSON());
} else {
res.status(404).send();
}
}, function (e) {
res.status(500).send();
})
// var todoId = parseInt(req.params.id, 10);
// var matchedTodo = _.findWhere(todos, {id: todoId});
// if (matchedTodo) {
// res.json(matchedTodo);
// } else {
// res.status(404).send();
// }
});
app.post('/todos', middleware.requireAuthentication, function (req, res) {
var body = _.pick(req.body, 'description', 'completed');
// update for sequelize
db.todo.create(body).then(function (todo) {
// res.json(todo.toJSON());
req.user.addTodo(todo).then(function () { // in middleware.js, had added req.user
return todo.reload();
}).then(function (todo) {
res.json(todo.toJSON());
});
}, function (e) {
return res.status(400).json(e);
// }).catch(function (e) {
// console.log(e);
});
// if (!_.isBoolean(body.completed) || !_.isString(body.description) || body.description.trim().length === 0) {
// return res.status(400).send();
// }
// body.description = body.description.trim();
// body.id = todoNextId++;
// todos.push(body);
// // console.log('description: ' + body.description);
// res.json(body);
});
app.delete('/todos/:id', middleware.requireAuthentication, function (req, res) {
var todoId = parseInt(req.params.id, 10);
// db.todo.findById(todoId).then(function(todo) {
// if (!!todo) {
// res.json(todo);
// todo.destroy();
// } else {
// res.status(404).send();
// }
// }, function (e) {
// res.status(500).send();
// });
db.todo.destroy({
where: {
id: todoId,
userId: req.user.get('id')
}
}).then(function (rowsDeleted) {
if (rowsDeleted === 0) {
res.status(404).json({
error: 'No todo with id'
});
} else {
res.status(204).send(); // OK and no response to send
}
});
// var matchedTodo = _.findWhere(todos, {id: todoId});
// if (!matchedTodo) {
// res.status(404).json({"error": "no todo found with that id"});
// } else {
// todos = _.without(todos, matchedTodo);
// res.json(matchedTodo);
// // res.status(200).send();
// }
});
app.put('/todos/:id', middleware.requireAuthentication, function (req, res) {
var todoId = parseInt(req.params.id, 10);
// var matchedTodo = _.findWhere(todos, {id: todoId});
var body = _.pick(req.body, 'description', 'completed');
var attributes = {};
// var validAttributes = {};
// if (!matchedTodo) {
// return res.status(404).send();
// }
if (body.hasOwnProperty('completed')) { // && _.isBoolean(body.completed)) {
attributes.completed = body.completed;
// validAttributes.completed = body.completed;
} // else if (body.hasOwnProperty('completed')) {
// return res.status(400).send();
// }
if (body.hasOwnProperty('description')) { // && _.isString(body.description) && body.description.trim().length > 0) {
attributes.description = body.description;
// validAttributes.description = body.description;
} // else if (body.hasOwnProperty('description')) {
// return res.status(400).send();
// }
// _.extend(matchedTodo, attributes); // validAttributes); // passed by reference, so modified
// res.json(matchedTodo);
// db.todo.findById(todoId).then(function (todo) {
db.todo.findOne({
where: {
id: todoId,
userId: req.user.get('id')
}
}).then(function (todo) {
// if (todo) {
// return todo.update(attributes);
// } else {
// res.status(404).send();
// }
if (todo) {
return 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();
// }).then(function (todo) {
// res.json(todo.toJSON());
// }, function (e) {
// res.status(400).json(e);
});
});
app.post('/users', function(req, res) {
var body = _.pick(req.body, 'email', 'password');
db.user.create(body).then(function (user) {
res.json(user.toPublicJSON()); // this toPublicJSON instance method is defined in user.js
}, function (e) {
res.status(400).json(e);
});
});
// route; users resource, login action
app.post('/users/login', function (req, res) {
var body = _.pick(req.body, 'email', 'password');
var userInstance;
db.user.authenticate(body).then(function (user) {
var token = user.generateToken('authentication');
userInstance = user;
return db.token.create({
token: token
});
// if (token) {
// res.header('Auth', token).json(user.toPublicJSON());
// } else {
// res.status(401).send(); // don't give good descriptions for login!
// }
}).then(function (tokenInstance) {
res.header('Auth', tokenInstance.get('token')).json(userInstance.toPublicJSON());
}).catch(function () {
res.status(401).send(); // don't give good descriptions for login!
});
// if (typeof body.email !== 'string' || typeof body.password !== 'string') {
// res.status(400).send();
// }
// // findById, findAll, findOne
// db.user.findOne({
// where: {
// email: body.email
// }
// }).then(function (user) {
// if (!user || !bcrypt.compareSync(body.password, user.get('password_hash'))) {
// return res.status(401).send(); // 401: authentication possible, but it failed
// }
// res.json(user.toPublicJSON());
// }, function (e) {
// res.status(500).send();
// });
});
app.delete('/users/login', middleware.requireAuthentication, function (req, res) {
req.token.destroy().then(function () {
res.status(204).send();
}).catch(function () {
res.status(500).send();
});
})
// db.sequelize.sync().then(function () {
db.sequelize.sync({
force: true
}).then(function () {
app.listen(PORT, function () {
console.log('Express listening on port ' + PORT + '!');
});
});
// app.listen(PORT, function () {
// console.log('Express listening on port ' + PORT + '!');
// });