Dodal da se nemore dodati filem ce je ze v DB

This commit is contained in:
Nikola Petrov 2023-06-25 16:09:43 +02:00
parent 6f0632db6b
commit e75952fb52

View File

@ -34,18 +34,37 @@ module.exports = {
genre: req.body.genre ? req.body.genre : req.body.Genre, genre: req.body.genre ? req.body.genre : req.body.Genre,
code: req.body.code ? req.body.code : req.body.Code code: req.body.code ? req.body.code : req.body.Code
}); });
movie.save() MovieModel.findOne({ code: movie.code })
.then(savedMovie => { .then(movieFound => {
// Handle successful save operation if (movieFound) {
return res.json(savedMovie); return res.status(409).json({
}) message: 'Movie already exists'
.catch(err => {
return res.status(500).json({
message: 'Error when creating movie',
error: err
}); });
}
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
})
}); });
}, },
/** /**