From 5ee3afbec1e1b9da867252d94b6c264b0c6c80b3 Mon Sep 17 00:00:00 2001 From: Natacha Key Date: Wed, 12 Jul 2023 13:36:44 +0200 Subject: [PATCH] added new option to updateReview --- controllers/reviewController.js | 61 ++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 20 deletions(-) diff --git a/controllers/reviewController.js b/controllers/reviewController.js index 3e8fe29..cc12d50 100644 --- a/controllers/reviewController.js +++ b/controllers/reviewController.js @@ -78,29 +78,50 @@ const updateReview = async (req, res) => { // await review.save(); // res.status(StatusCodes.OK).json({ review }); -//OPTION 2 -const { id: reviewId } = req.params; //TAKE ID FROM REQ.PARAMS AND ASSIGN IT TO REVIEWiD +//GENERAL !!!!! OPTION 2 +// const { id: reviewId } = req.params; //TAKE ID FROM REQ.PARAMS AND ASSIGN IT TO REVIEWiD -const review = await Review.findOne({ _id: reviewId }); +// const review = await Review.findOne({ _id: reviewId }); + +// if (!review) { +// throw new CustomError.NotFoundError(`No review with id ${reviewId}`); +// } +// checkPermissions(req.user, review.user); +// // can simplify with: review.rating = req.body?.rating || review.rating (but hasOwnProperty is better because then that allows us to send/unset values like this: {rating: null}) +// review.rating = req.body.hasOwnProperty('rating') +// ? req.body.rating +// : review.rating; +// review.title = req.body.hasOwnProperty('title') +// ? req.body.title +// : review.title; +// review.comment = req.body.hasOwnProperty('comment') +// ? req.body.comment +// : review.comment; + +// await review.save(); +// res.status(StatusCodes.OK).json({ review }); + +//option 2.o with findoneandupdate() +const { id: reviewId } = req.params; +const review = await Review.findOne({ _id: reviewId }); if (!review) { - throw new CustomError.NotFoundError(`No review with id ${reviewId}`); -} - -checkPermissions(req.user, review.user); -// can simplify with: review.rating = req.body?.rating || review.rating (but hasOwnProperty is better because then that allows us to send/unset values like this: {rating: null}) -review.rating = req.body.hasOwnProperty('rating') -? req.body.rating -: review.rating; -review.title = req.body.hasOwnProperty('title') -? req.body.title -: review.title; -review.comment = req.body.hasOwnProperty('comment') -? req.body.comment -: review.comment; - -await review.save(); -res.status(StatusCodes.OK).json({ review }); + throw new CustomError.NotFoundError(`No review with id ${reviewId}`); + } + +const updatedReview = await Review.findOneAndUpdate( + { _id: reviewId }, + { + $set: { + rating: req.body.hasOwnProperty('rating') ? req.body.rating : review.rating, + title: req.body.hasOwnProperty('title') ? req.body.title : review.title, + comment: req.body.hasOwnProperty('comment') ? req.body.comment : review.comment + } + }, + { new: true } // This option returns the updated document +); + +res.status(StatusCodes.OK).json({ review: updatedReview }); //option 3 //When we do the object destructuring (const { rating, title, comment } = req.body;), then any fields that didn't exist in req.body will just be saved to the variables on the left-hand-side as undefined.