-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.js
54 lines (48 loc) · 1.41 KB
/
router.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
const router = require("express").Router();
const { forEach } = require("./message");
const messages = require("./message");
router.get('/messages', (req, res) => {
return res.send(messages);
});
router.get('/messages/:id', (req, res) => {
const id = req.params.id;
for (const message of messages) {
if (message.id === parseInt(id)) {
return res.send(message);
}
}
return res.send("message id not found");
});
router.post('/messages', (req, res) => {
const message = req.body.message;
const newId = messages.length > 0 ? messages[messages.length - 1].id + 1 : 1;
messages.push({
id: newId,
text: message
})
return res.send(messages[messages.length - 1]);
});
router.put('/messages/:id', (req, res) => {
const message = req.body.message;
const id = req.params.id;
for (const message of messages) {
if (message.id === parseInt(id)) {
message.text = req.body.message;
return res.send(message);
}
}
return res.send('message id not found');
});
router.delete('/messages/:id', (req, res) => {
const id = req.params.id;
let i = 0;
for (const message of messages) {
if (message.id === parseInt(id)) {
messages.splice(i,1);
return res.send("message deleted");
}
i++;
}
return res.send('message id not found');
});
module.exports = router;