0% found this document useful (0 votes)
5 views1 page

Reviews

The document contains two asynchronous functions for managing reviews in a campground application. The 'newReview' function creates and saves a new review linked to a campground, while the 'deleteReview' function removes a review from a campground and deletes it from the database. Both functions provide user feedback through flash messages and redirect to the campground page after completion.

Uploaded by

David Pavlovski
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Reviews

The document contains two asynchronous functions for managing reviews in a campground application. The 'newReview' function creates and saves a new review linked to a campground, while the 'deleteReview' function removes a review from a campground and deletes it from the database. Both functions provide user feedback through flash messages and redirect to the campground page after completion.

Uploaded by

David Pavlovski
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

const Review = require('..

/models/review');
const Campground = require('../models/campground');

module.exports.newReview = async (req, res) => {


const { id } = req.params;
const campground = await Campground.findById(id);
const review = new Review(req.body.review);
review.owner = req.user._id;
campground.reviews.push(review);
await review.save();
await campground.save();
// console.log(campground);
req.flash('success', 'Review successfuly created!');
res.redirect(`/campgrounds/${id}`);
};

module.exports.deleteReview = async (req, res) => {


const { id, review_id } = req.params;
await Campground.findByIdAndUpdate(id, {
$pull: { reviews: review_id }
});
await Review.findByIdAndDelete(review_id);
req.flash('success', 'Review successfuly deleted!');
res.redirect(`/campgrounds/${id}`);
};

You might also like