Skip to content

Commit

Permalink
review bug fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
dipexplorer committed Oct 15, 2024
1 parent 3440e22 commit 0bdbb2b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
6 changes: 1 addition & 5 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ app.use((req, res, next) => {
res.locals.success = req.flash('success');
res.locals.error = req.flash('error');
res.locals.currUser=req.user; //storecurrent session user info in currUser
console.log(res.locals);
// console.log(res.locals);
next();
});

Expand Down Expand Up @@ -180,10 +180,6 @@ const listingController = require('./controllers/listing.js');
// app.get("/new",isLoggedIn, asyncwrap(newpost));
app.get("/listing/new", isLoggedIn, asyncwrap(listingController.newpost));

//create new listing
// Create new listing form route
app.get("/listing/new", isLoggedIn, asyncwrap(listingController.newpost));


//index route
app.get("/listing",asyncwrap(index));
Expand Down
43 changes: 27 additions & 16 deletions controllers/reviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,39 @@ const listing=require("../models/listing.js");
const {
SUCCESS_REVIEW_ADDED,
ERROR_LISTING_NOT_FOUND
} = require('../constants.js')
} = require('../constants.js');

//add review
module.exports.reviewPost = (async (req, res) => {
const { id } = req.params;
const list = await listing.findById(id).populate("reviews");
const list = await listing.findById(id);

if (!list) {
req.flash('error', ERROR_LISTING_NOT_FOUND);
return res.redirect('/listing');
try{
if (!list) {
req.flash('error', ERROR_LISTING_NOT_FOUND);
return res.redirect('/listing');
}

const newReview = new review(req.body.review);
newReview.author = req.user._id;
console.log("author is", newReview.author);
list.reviews.push(newReview);

await newReview.save();
await list.save();
console.log(list)
req.flash('success', SUCCESS_REVIEW_ADDED);
res.redirect(`/listing/${list.id}`);
}catch(e) {
console.error(e);
req.flash('error', 'An error occurred while adding review.');
console.log(e);
// res.redirect(`/listing/${id}`);
}

const newReview = new review(req.body.review);
newReview.author = req.user._id;
list.reviews.push(newReview);

await newReview.save();
await list.save();

req.flash('success', SUCCESS_REVIEW_ADDED);
res.redirect(`/listing/${list._id}`);
});

//delete a review
module.exports.deleteReview =(async (req,res) =>{

let {id,rid} =req.params;
await listing.findByIdAndUpdate(id,{$pull:{reviews:rid}}); //update the listing-reviews array where review id matched rid
await review.findByIdAndDelete(rid); //deconstructing parameters
Expand Down
4 changes: 1 addition & 3 deletions views/show.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,7 @@
class="card listing-card card-body col-lg-11 col-md-8 col-sm-10 col-xs-10 offset-lg-3 offset-md-2 offset-sm-1">
<!-- Form for add reviews. -->
<form action="/listings/<%= list._id %>/review" method="post" novalidate
class="needs-validation">
<form action="/listing/<%= list._id %>/review" method="post" novalidate class="needs-validation">
<div>
<label for="rating" class="form-label bold2">Rating:</label>
<fieldset class="starability-grow">
Expand Down

0 comments on commit 0bdbb2b

Please sign in to comment.