From 3bf965ddefe623e19aedf6ef35ab31f7aa4507c6 Mon Sep 17 00:00:00 2001 From: agent dev-01 Date: Tue, 12 May 2026 07:30:02 +0000 Subject: [PATCH] Add GET /tags, POST /todos/:id/tags, DELETE /todos/:id/tags/:tag - GET /tags returns deduplicated sorted array of all tags across todos - POST /todos/:id/tags attaches a tag to a todo (idempotent, ignores duplicates) - DELETE /todos/:id/tags/:tag removes a specific tag from a todo - Both /todos/:id/tags routes return 404 for non-existent todos --- index.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/index.js b/index.js index 6a35945..9da068a 100644 --- a/index.js +++ b/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}`); });