Add GET /tags, POST /todos/:id/tags, DELETE /todos/:id/tags/:tag #17
28
index.js
28
index.js
@@ -47,6 +47,34 @@ app.delete('/todos/:id', (req, res) => {
|
||||
res.status(204).end();
|
||||
});
|
||||
|
||||
app.get('/tags', (req, res) => {
|
||||
const tags = [...new Set(todos.flatMap(t => t.tags))].sort();
|
||||
res.json(tags);
|
||||
});
|
||||
|
||||
app.post('/todos/:id/tags', (req, res) => {
|
||||
const id = parseInt(req.params.id, 10);
|
||||
const todo = todos.find(t => t.id === id);
|
||||
if (!todo) {
|
||||
return res.status(404).end();
|
||||
}
|
||||
const { tag } = req.body;
|
||||
if (!todo.tags.includes(tag)) {
|
||||
todo.tags.push(tag);
|
||||
}
|
||||
res.json(todo);
|
||||
});
|
||||
|
||||
app.delete('/todos/:id/tags/:tag', (req, res) => {
|
||||
const id = parseInt(req.params.id, 10);
|
||||
const todo = todos.find(t => t.id === id);
|
||||
if (!todo) {
|
||||
return res.status(404).end();
|
||||
}
|
||||
todo.tags = todo.tags.filter(t => t !== req.params.tag);
|
||||
res.status(204).end();
|
||||
});
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Server listening on http://localhost:${PORT}`);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user