From fed71d59e3f27679b1c872237ce12a6fccd98e0d Mon Sep 17 00:00:00 2001 From: Stepan Usatiuk Date: Fri, 18 May 2018 10:36:43 +0300 Subject: [PATCH] add removeWithTodos to TodoList model --- models/TodoList.js | 7 +++++++ routes/lists.js | 6 ++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/models/TodoList.js b/models/TodoList.js index 1546be3..8cba30a 100644 --- a/models/TodoList.js +++ b/models/TodoList.js @@ -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); diff --git a/routes/lists.js b/routes/lists.js index b19b6d4..d57f0ac 100644 --- a/routes/lists.js +++ b/routes/lists.js @@ -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 }); }), );