mirror of
https://github.com/usatiuk/ustk-todolist.git
synced 2025-10-28 23:57:49 +01:00
refactoring, fix todo removal from lists
This commit is contained in:
@@ -7,13 +7,14 @@ const Todo = mongoose.model('Todo');
|
||||
|
||||
const asyncHelper = require('../asyncHelper');
|
||||
|
||||
const { NotFoundError } = require('../errors');
|
||||
// index
|
||||
router.get(
|
||||
'/',
|
||||
asyncHelper(async (req, res) => {
|
||||
const { listId } = res.locals;
|
||||
const todos = await Todo.find({ list: listId }).exec();
|
||||
res.json(todos);
|
||||
res.json({ success: true, data: todos.map(todo => todo.toJson()) });
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -52,7 +53,11 @@ router.delete(
|
||||
'/:todoId',
|
||||
asyncHelper(async (req, res) => {
|
||||
const { todoId } = req.params;
|
||||
await Todo.findByIdAndRemove(todoId).exec();
|
||||
const todo = await Todo.findById(todoId).exec();
|
||||
if (!todo) {
|
||||
throw new NotFoundError('cant find todo');
|
||||
}
|
||||
await todo.remove();
|
||||
res.json({ success: true });
|
||||
}),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user