From e75952fb522aa05d0f2b7c2a59e07f103111c8bd Mon Sep 17 00:00:00 2001 From: Nikola Petrov Date: Sun, 25 Jun 2023 16:09:43 +0200 Subject: [PATCH] Dodal da se nemore dodati filem ce je ze v DB --- controllers/movieController.js | 39 +++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/controllers/movieController.js b/controllers/movieController.js index bf56893..2c9935d 100644 --- a/controllers/movieController.js +++ b/controllers/movieController.js @@ -34,18 +34,37 @@ module.exports = { genre: req.body.genre ? req.body.genre : req.body.Genre, code: req.body.code ? req.body.code : req.body.Code }); - - movie.save() - .then(savedMovie => { - // Handle successful save operation - return res.json(savedMovie); - }) - .catch(err => { - return res.status(500).json({ - message: 'Error when creating movie', - error: err + + MovieModel.findOne({ code: movie.code }) + .then(movieFound => { + if (movieFound) { + return res.status(409).json({ + message: 'Movie already exists' }); + } + else + { + movie.save() + .then(savedMovie => { + // Handle successful save operation + return res.json(savedMovie); + }) + .catch(err => { + return res.status(500).json({ + message: 'Error when creating movie', + error: err + }); + }); + } + }) + .catch(err => { + return res.status(500).json({ + message: 'Error when getting movie', + error: err + }) }); + + }, /**