add removeWithTodos to TodoList model

This commit is contained in:
2018-05-18 10:36:43 +03:00
parent 325d01e945
commit fed71d59e3
2 changed files with 9 additions and 4 deletions

View File

@@ -27,4 +27,11 @@ TodoListSchema.methods.slugify = function () {
this.slug = slugify(this.name);
};
TodoListSchema.methods.removeWithTodos = function () {
this.todos.forEach((todo) => {
todo.remove();
});
this.remove();
};
mongoose.model('TodoList', TodoListSchema);

View File

@@ -31,6 +31,7 @@ router.post(
}),
);
// delete
router.delete(
'/:slug',
asyncHelper(async (req, res) => {
@@ -41,10 +42,7 @@ router.delete(
if (!list) {
throw new NotFoundError();
}
list.todos.forEach((todo) => {
todo.remove();
});
list.remove();
await list.removeWithTodos();
res.json({ success: true });
}),
);