Skip to content

Commit 94ee220

Browse files
committed
Done with the edit and delelte part
1 parent b026590 commit 94ee220

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

routes/campgrounds.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ router.get("/index/new",isLoggedIn, function (req, res) {
7979
});
8080

8181
//Edit campground----->>>
82-
router.get("/index/:id/edit", function (req, res) {
82+
router.get("/index/:id/edit",checkCampgroundOwnership ,function (req, res) {
8383
Campground.findById(req.params.id, function (err, foundCampground) {
8484
if (err) {
8585
console.log(err);
@@ -92,7 +92,7 @@ router.get("/index/:id/edit", function (req, res) {
9292
});
9393

9494
// UPDATE CAMPGROUND ROUTE
95-
router.put("/index/:id" , function(req, res){
95+
router.put("/index/:id", checkCampgroundOwnership ,function(req, res){
9696
// find and update the correct campground
9797
Campground.findByIdAndUpdate(req.params.id, req.body.campground, function(err, updatedCampground){
9898
if(err){
@@ -105,6 +105,19 @@ router.put("/index/:id" , function(req, res){
105105
});
106106
});
107107

108+
//Destroy campground rote
109+
router.delete("/index/:id",checkCampgroundOwnership ,function (req, res) {
110+
console.log("I'm here");
111+
Campground.findByIdAndRemove(req.params.id, function (err) {
112+
if(err){
113+
console.log(err);
114+
res.redirect("/index");
115+
}else{
116+
res.redirect("/index");
117+
}
118+
});
119+
});
120+
108121

109122
function isLoggedIn(req, res, next) {
110123
if(req.isAuthenticated()){
@@ -114,5 +127,25 @@ router.put("/index/:id" , function(req, res){
114127
}
115128
}
116129

130+
//Check campground ownership
131+
function checkCampgroundOwnership(req, res, next) {
132+
if(req.isAuthenticated()){
133+
Campground.findById(req.params.id, function(err, foundCampground){
134+
if(err){
135+
res.redirect("back");
136+
} else {
137+
// does user own the campground?
138+
if(foundCampground.Author.id.equals(req.user._id)) {
139+
next();
140+
} else {
141+
res.redirect("back");
142+
}
143+
}
144+
});
145+
} else {
146+
res.redirect("back");
147+
}
148+
}
149+
117150
module.exports = router;
118151

views/campgrounds/show.ejs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@
2020
<em> Submitted By: <%= campground.Author.username %> </em>
2121
</p>
2222
<% if(currentUser && campground.Author.id.equals(currentUser._id)){ %>
23-
<a class="btn btn-xs btn-warning" href="/index/<%= campground._id %>/edit">Edit</a>
24-
<% } %>
23+
<a class="btn btn-sm btn-warning" href="/index/<%= campground._id %>/edit">Edit</a>
24+
<form style="display: inline" class="form-inline" action="/index/<%=campground._id%>?_method=DELETE" method="POST">
25+
<button class="btn btn-sm btn-danger">Delete</button>
26+
</form>
27+
<% } %>
28+
2529
</div>
2630
</div>
2731
<div class="card card-body bg-light">

0 commit comments

Comments
 (0)