Add GET /tags, POST /todos/:id/tags, DELETE /todos/:id/tags/:tag
All checks were successful
Auditor / audit (pull_request) Successful in 57s
All checks were successful
Auditor / audit (pull_request) Successful in 57s
- 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
This commit is contained in:
28
index.js
28
index.js
@@ -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}`);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user