0% found this document useful (0 votes)
15 views2 pages

Listing - Js Routes

Uploaded by

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

Listing - Js Routes

Uploaded by

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

//REVIEW.JS .

/ROUTES

const express = require("express");

const router = express.Router({ mergeParams: true });

const wrapAsync = require("../utils/wrapAsync.js");

const ExpressError = require("../utils/ExpressError.js");

const { validateReview, isLoggedIn } = require("../middleware.js");

const Review = require("../models/review.js");

const Listing = require("../models/listing.js");

//Reviews

//Post Route

router.post(

"/",

isLoggedIn,

validateReview,

wrapAsync(async (req, res) => {

let listing = await Listing.findById(req.params.id);

let newReview = new Review(req.body.review);

newReview.author = req.user._id;

listing.reviews.push(newReview);

await newReview.save();

await listing.save();

req.flash("success", "Review Added");

res.redirect(`/listings/${listing._id}`);

})

);

//Delete Review Route


router.delete(

"/:reviewId",

wrapAsync(async (req, res) => {

let { id, reviewId } = req.params;

await Listing.findByIdAndUpdate(id, { $pull: { reviews: reviewId } });

await Review.findByIdAndDelete(reviewId);

req.flash("success", "Review Deleted");

res.redirect(`/listings/${id}`);

})

);

module.exports = router;

You might also like