Add GET /tags, POST /todos/:id/tags, DELETE /todos/:id/tags/:tag #17

Open
danny8632 wants to merge 2 commits from agent/issue-15-add-get-tags-post-todos-id-tags-delete-todos-id-ta into main
Showing only changes of commit 3bf965ddef - Show all commits

View File

@@ -47,6 +47,34 @@ app.delete('/todos/:id', (req, res) => {
res.status(204).end(); 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, () => { app.listen(PORT, () => {
console.log(`Server listening on http://localhost:${PORT}`); console.log(`Server listening on http://localhost:${PORT}`);
}); });