refactoring, fix todo removal from lists

This commit is contained in:
2018-05-19 21:45:12 +03:00
parent 97921d6ccc
commit 87ec0ac4d4
7 changed files with 55 additions and 8 deletions

View File

@@ -10,7 +10,7 @@ module.exports = asyncHelper(async (req, res, next) => {
const { slug } = req.params;
const list = await TodoList.findOne({ slug }).exec();
if (!list) {
throw new NotFoundError();
throw new NotFoundError('cant find list');
}
res.locals.listId = list._id;
next();

View File

@@ -16,7 +16,7 @@ router.get(
const lists = await TodoList.find({})
.populate('todos')
.exec();
res.json(lists);
res.json({ success: true, data: lists.map(list => list.toJson()) });
}),
);

View File

@@ -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 });
}),
);