Add tags field to todos and ?tag= filter on GET /todos
All checks were successful
Auditor / audit (pull_request) Successful in 51s
All checks were successful
Auditor / audit (pull_request) Successful in 51s
- Add tags:[] to all seed todo objects - POST /todos accepts optional tags array (defaults to []) - GET /todos?tag=<value> filters to todos whose tags include that value (case-sensitive) Closes #14
This commit is contained in:
52
index.js
Normal file
52
index.js
Normal file
@@ -0,0 +1,52 @@
|
||||
const express = require('express');
|
||||
const { name, version } = require('./package.json');
|
||||
|
||||
const app = express();
|
||||
const PORT = 3000;
|
||||
|
||||
app.use(express.json());
|
||||
|
||||
let todos = [
|
||||
{ id: 1, title: 'Buy groceries', done: false, tags: [] },
|
||||
{ id: 2, title: 'Walk the dog', done: true, tags: [] },
|
||||
{ id: 3, title: 'Read a book', done: false, tags: [] },
|
||||
];
|
||||
|
||||
let nextId = 4;
|
||||
|
||||
app.get('/healthz', (req, res) => {
|
||||
res.json({ ok: true });
|
||||
});
|
||||
|
||||
app.get('/version', (req, res) => {
|
||||
res.json({ name, version });
|
||||
});
|
||||
|
||||
app.get('/todos', (req, res) => {
|
||||
const { tag } = req.query;
|
||||
if (tag !== undefined) {
|
||||
return res.json(todos.filter(t => t.tags.includes(tag)));
|
||||
}
|
||||
res.json(todos);
|
||||
});
|
||||
|
||||
app.post('/todos', (req, res) => {
|
||||
const { title, tags } = req.body;
|
||||
const todo = { id: nextId++, title, done: false, tags: Array.isArray(tags) ? tags : [] };
|
||||
todos.push(todo);
|
||||
res.status(201).json(todo);
|
||||
});
|
||||
|
||||
app.delete('/todos/:id', (req, res) => {
|
||||
const id = parseInt(req.params.id, 10);
|
||||
const index = todos.findIndex(t => t.id === id);
|
||||
if (index === -1) {
|
||||
return res.status(404).end();
|
||||
}
|
||||
todos.splice(index, 1);
|
||||
res.status(204).end();
|
||||
});
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Server listening on http://localhost:${PORT}`);
|
||||
});
|
||||
Reference in New Issue
Block a user