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 + }) }); + + }, /**