campground 와 review 의 작성자 정보를 DB에 저장하여 작성자에게만 수정/삭제할 수 있는 권한을 부여하였다.
👇코드 보러가기
https://github.com/Sara-Jo/BeWild/commit/2c44455c9906e931840805fbb5ea8e11b7502640
const CampgroundSchema = new Schema ({
title: String,
image: String,
price: Number,
description: String,
location: String,
**author: {
type: Schema.Types.ObjectId,
ref: "User"
},**
reviews: [
{
type: Schema.Types.ObjectId,
ref: 'Review'
}
]
});
const reviewSchema = new Schema({
body: String,
rating: Number,
**author: {
type: Schema.Types.ObjectId,
ref: "User"
}**
});
// Campground Post
router.post("/", isLoggedIn, validateCampground, catchAsync(async(req, res) => {
const campground = new Campground(req.body.campground);
**campground.author = req.user._id;** // Store the author DB when a new campground is created
await campground.save();
req.flash("success", "Successfully made a new campground!");
res.redirect(`/campgrounds/${campground._id}`);
}));
// Review Post
router.post("/", isLoggedIn, validateReview, catchAsync(async(req, res) => {
const campground = await Campground.findById(req.params.id);
const review = new Review (req.body.review);
**review.author = req.user._id;** // store the author DB when review is created
campground.reviews.push(review);
await review.save();
await campground.save();
req.flash("success", "Created a new review!");
res.redirect(`/campgrounds/${campground._id}`);
}));