From 757acd9014233eae2b1889e892ef376e7220f9eb Mon Sep 17 00:00:00 2001 From: Nikola Petrov Date: Mon, 26 Jun 2023 00:59:11 +0200 Subject: [PATCH] Dodal delete movies --- controllers/movieController.js | 19 ++++++++++++------- routes/movies/movieRouter.js | 2 +- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/controllers/movieController.js b/controllers/movieController.js index 2c9935d..33fa7c9 100644 --- a/controllers/movieController.js +++ b/controllers/movieController.js @@ -104,16 +104,21 @@ module.exports = { */ remove: function (req, res) { var id = req.params.id; - - MovieModel.findByIdAndRemove(id, function (err, movie) { - if (err) { - return res.status(500).json({ - message: 'Error when deleting the movie.', - error: err + console.log(id); + MovieModel.findOneAndDelete({ code: id }) + .then(movie => { + if (!movie) { + return res.status(404).json({ + message: 'No such movie' }); } - return res.status(204).json(); + }) + .catch(err => { + return res.status(500).json({ + message: 'Error when deleting the movie.', + error: err + }); }); }, }; diff --git a/routes/movies/movieRouter.js b/routes/movies/movieRouter.js index c7d3892..49c0bf7 100644 --- a/routes/movies/movieRouter.js +++ b/routes/movies/movieRouter.js @@ -13,6 +13,6 @@ router.post('/', movieController.create); //router.put('/:id', movieController.update); -//router.delete('/:id', movieController.remove); +router.delete('/:id', movieController.remove); module.exports = router; \ No newline at end of file