Skip to content

Commit 11763fc

Browse files
committed
Added the comment delete feature
1 parent 94ee220 commit 11763fc

File tree

4 files changed

+121
-12
lines changed

4 files changed

+121
-12
lines changed

routes/comments.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,60 @@ router.get("/index/:id/comments/new", isLoggedIn, function (req, res) {
5454
});
5555
});
5656

57+
//Comment edit and delete
58+
router.get("/index/:id/comments/:comment_id/edit", function(req, res){
59+
Campground.findById(req.params.id, function (err, foundCampground) {
60+
if(err){
61+
console.log(err);
62+
res.redirect("/index/");
63+
}else{
64+
Comment.findById(req.params.comment_id, function (err, foundComment) {
65+
if(err){
66+
console.log(err);
67+
res.redirect("/index");
68+
}else{
69+
res.render("comments/edit.ejs", {campground:foundCampground, comment:foundComment});
70+
}
71+
});
72+
}
73+
});
74+
});
75+
76+
//Post route for editing the comment
77+
router.put("/index/:id/comments/:comment_id", function (req, res) {
78+
Campground.findById(req.params.id, function(err, foundCampground){
79+
if(err){
80+
console.log(err);
81+
res.redirect("/index");
82+
}else{
83+
Comment.findByIdAndUpdate(req.params.comment_id, req.body.comment, function (err) {
84+
if(err){
85+
console.log(err);
86+
res.redirect("back");
87+
88+
}else{
89+
res.redirect("/index/"+req.params.id);
90+
91+
}
92+
})
93+
}
94+
});
95+
});
96+
97+
98+
//Delete a comment
99+
router.delete("/index/:id/comments/:comment_id", function (req, res) {
100+
Comment.findByIdAndRemove(req.params.comment_id, function (err) {
101+
if (err) {
102+
res.redirect("back");
103+
}else{
104+
res.redirect("/index/"+req.params.id);
105+
}
106+
});
107+
});
108+
109+
110+
57111
function isLoggedIn(req, res, next) {
58112
if(req.isAuthenticated()){
59113
return next();

views/campgrounds/show.ejs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
</div>
1111
</div>
1212
<div class="col-md-9">
13-
<div class="img-thumbnail">
14-
<img style="width: 100%" class="img-responsive" src="<%= campground.Image %>" alt="">
13+
<div class="img-thumbnail shadow">
14+
<img style="width: 100% " class="img-responsive shadow-sm" src="<%= campground.Image %>" alt="">
1515
<div class="caption-full" style="padding: 9px">
1616
<h4 class="float-right">$9.00/night</h4>
1717
<h4><a><%= campground.Name %></a></h4>
@@ -20,27 +20,33 @@
2020
<em> Submitted By: <%= campground.Author.username %> </em>
2121
</p>
2222
<% if(currentUser && campground.Author.id.equals(currentUser._id)){ %>
23-
<a class="btn btn-sm btn-warning" href="/index/<%= campground._id %>/edit">Edit</a>
23+
<a class="btn btn-sm btn-warning" style="display: inline;" href="/index/<%= campground._id %>/edit">Edit</a>
2424
<form style="display: inline" class="form-inline" action="/index/<%=campground._id%>?_method=DELETE" method="POST">
2525
<button class="btn btn-sm btn-danger">Delete</button>
2626
</form>
2727
<% } %>
2828

2929
</div>
3030
</div>
31-
<div class="card card-body bg-light">
31+
<div class="card card-body bg-light shadow">
3232
<div class="text-right">
3333
<a class="btn btn-sucess btn-primary btn-large" href="/index/<%= campground._id %>/comments/new">Add Comment</a>
3434
</div>
3535
<hr>
3636
<% campground.Comments.forEach(function(comment){ %>
3737
<div class="row">
38-
<div class="col-md-12">
38+
<div class="col-md-12 mb-3">
3939
<strong><%= comment.author.username %></strong>
4040
<span class="float-right">10 days ago</span>
41-
<p>
41+
<p class="mb-0">
4242
<%= comment.content %>
4343
</p>
44+
45+
<a class="btn btn-sm btn-warning" href="/index/<%= campground._id %>/comments/<%= comment._id%>/edit ">Edit</a>
46+
<form action="/index/<%=campground._id%>/comments/<%= comment._id %>?_method=DELETE" method="POST" id="delete-form" style="display: inline" class="form-inline" >
47+
<button class="btn btn-sm btn-danger">Delete</button>
48+
</form>
49+
4450
</div>
4551
</div>
4652
<% }) %>

views/comments/edit.ejs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<%- include("../partials/header") %>
2+
3+
<div class="container">
4+
<!-- Heading -->
5+
<h1 class="d-flex justify-content-center mt-4">Edit <%= campground.Name %> </h1>
6+
<!-- Form starts here -->
7+
<div class="form-group">
8+
<form action="/index/<%=campground._id %>/comments/<%= comment._id %>?_method=PUT" method="POST" >
9+
<div class="row justify-content-center">
10+
<div class="col-4 align-self-center mb-2">
11+
<!-- Two inputs will be taken -->
12+
<input type="text" name="comment[content]" placeholder="Text" class="form-control mb-1" value="<%=comment.content%>">
13+
14+
<!-- <input type="text" name="comment[author]" placeholder="Author" class="form-control mb-1"> -->
15+
</div>
16+
</div>
17+
18+
<!-- The button to redirect the page and submit the form -->
19+
<div class="row justify-content-center">
20+
<div class="col-4">
21+
<button type="button justify-content-center" class="btn btn-primary btn-block">Submit</button>
22+
</div>
23+
</div>
24+
25+
<!-- Link to go back -->
26+
<div class="row">
27+
<div class="col-4">
28+
</div>
29+
<div class="col-4">
30+
<a href="/index"> Go back </a>
31+
</div>
32+
</div>
33+
34+
</form>
35+
</div>
36+
37+
38+
</div>
39+
40+
41+
<%- include("../partials/footer") %>

views/login.ejs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
<%- include("./partials/header.ejs") %>
22

3-
<h1>Login!</h1>
4-
<form action="/login" method="POST">
5-
<input type="text" name="username" placeholder="username">
6-
<input type="password" name="password" placeholder="password">
7-
<input type="submit" value="Login!">
8-
</form>
3+
<div class="container">
4+
<div class="row">
5+
<div class="col-2"></div>
6+
<div class="col-8">
7+
<h1>Login!</h1>
8+
<form action="/login" style="padding: 0;" method="POST">
9+
<input type="text" name="username" placeholder="username">
10+
<input type="password" name="password" placeholder="password">
11+
<input type="submit" value="Login!">
12+
</form>
13+
</div>
14+
<div class="col-2"></div>
15+
</div>
16+
</div>
917

1018
<%- include("./partials/footer.ejs") %>

0 commit comments

Comments
 (0)