Skip to content

Commit a3fe0e0

Browse files
committed
add comments
1 parent 06e0d0e commit a3fe0e0

File tree

6 files changed

+77
-68
lines changed

6 files changed

+77
-68
lines changed

app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var express = require("express")
1111
marked = require("marked")
1212

1313
var indexRoutes = require("./routes/index")
14-
// var commentRoutes = require("./routes/comments")
14+
var commentRoutes = require("./routes/comments")
1515
var blogRoutes = require("./routes/blogs")
1616

1717
mongoose.connect("mongodb://localhost/Blog_Website", {useNewUrlParser: true, useUnifiedTopology: true}, function() {
@@ -45,7 +45,7 @@ app.use(function(req, res, next){
4545
})
4646

4747
app.use(indexRoutes)
48-
// app.use("/campgrounds/:id/comments", commentRoutes)
48+
app.use(commentRoutes)
4949
app.use(blogRoutes)
5050

5151
//-------------LISTEN ON PORT 5000--------------

public/img/2header.jpg

-311 KB
Binary file not shown.

public/img/header.jpg

-108 KB
Binary file not shown.

routes/comments.js

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,65 @@
11
var express = require("express");
22
var router = express.Router({mergeParams: true});
3-
var Campground = require("../models/campground");
3+
var Blog = require("../models/blog");
44
var Comment = require("../models/comment");
55
var middleware = require("../middleware")
66

7-
router.get("/new", middleware.isLoggedIn, function(req, res){
8-
Campground.findById(req.params.id, function(err, campground){
9-
if(err){
10-
console.log(err)
11-
}
12-
else{
13-
res.render("comments/new", {campground: campground})
14-
}
15-
})
16-
})
17-
187
//POST
19-
router.post("/", middleware.isLoggedIn, function(req, res){
20-
Campground.findById(req.params.id, function(err, campground){
8+
router.post("/blog/:id/comment", function(req, res){
9+
console.log(req.params.id)
10+
Blog.findById(req.params.id, function(err, blog){
2111
if(err){
2212
console.log(err)
2313
}
2414
else{
25-
Comment.create(req.body.comment, function(err, comment){ //1st argument is a object, 2nd is callback function
15+
Comment.create({text: req.body.comment}, function(err, comment){ //1st argument is a object, 2nd is callback function
2616
if(err){
2717
console.log(err)
2818
}
2919
else{
3020
comment.author.id = req.user._id
3121
comment.author.username = req.user.username
3222
comment.save()
33-
campground.comments.push(comment._id)
34-
campground.save() //imp
35-
req.flash("success", "Successfully added comment")
36-
res.redirect("/campgrounds/"+campground._id)
23+
blog.comments.push(comment._id)
24+
blog.save() //imp
25+
// req.flash("success", "Successfully added comment")
26+
res.redirect("/blog/"+blog._id)
3727
}
3828
})
3929
}
4030
})
4131
})
4232

43-
//EDIT
44-
router.get("/:comment_id/edit", middleware.checkCommentOwnership, function(req, res){
45-
Comment.findById(req.params.comment_id, function(err, foundComment){
46-
if(err){
47-
res.redirect("back")
48-
} else{
49-
res.render("comments/edit", {campgroundid: req.params.id, comment: foundComment})
50-
}
51-
})
52-
})
33+
// //EDIT
34+
// router.get("/:comment_id/edit", middleware.checkCommentOwnership, function(req, res){
35+
// Blog.findById(req.params.comment_id, function(err, foundComment){
36+
// if(err){
37+
// res.redirect("back")
38+
// } else{
39+
// res.render("comments/edit", {campgroundid: req.params.id, comment: foundComment})
40+
// }
41+
// })
42+
// })
5343

54-
//UPDATE
55-
router.put("/:comment_id", middleware.checkCommentOwnership, function(req, res){
56-
Comment.findByIdAndUpdate(req.params.comment_id, req.body.comment, function(err, updatedComment){
57-
if(err){
58-
res.redirect("back")
59-
} else{
60-
res.redirect("/campgrounds/"+req.params.id)
61-
}
62-
})
63-
})
44+
// //UPDATE
45+
// router.put("/:comment_id", middleware.checkCommentOwnership, function(req, res){
46+
// Comment.findByIdAndUpdate(req.params.comment_id, req.body.comment, function(err, updatedComment){
47+
// if(err){
48+
// res.redirect("back")
49+
// } else{
50+
// res.redirect("/campgrounds/"+req.params.id)
51+
// }
52+
// })
53+
// })
6454

6555
//DELETE
6656
router.delete("/:comment_id", middleware.checkCommentOwnership, function(req, res){
67-
Comment.findByIdAndRemove(req.params.comment_id, function(err){
57+
Blog.findByIdAndRemove(req.params.comment_id, function(err){
6858
if(err){
6959
res.redirect("back")
7060
} else{
71-
req.flash("success", "Comments Deleted!")
72-
res.redirect("/campgrounds/"+req.params.id)
61+
// req.flash("success", "Comments Deleted!")
62+
res.redirect("/blog/"+req.params.id)
7363
}
7464
})
7565
})

views/blogs/info.ejs

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,29 +46,48 @@
4646
</div>
4747

4848

49-
<div class="main-content">
50-
<div class="container">
51-
<div class="columns is-centered is-multiline has-text-centered">
52-
<div class="column is-6">
53-
<img src="<%=blog.image%>" alt="">
54-
<p style="text-align:justify; padding-top: 20px;"><%-blog.body%></p>
55-
</div>
49+
<div class="container">
50+
<div class="columns is-centered is-multiline">
51+
<div class="column is-6">
52+
<img src="<%=blog.image%>" alt="">
53+
<p style="padding-top: 15px;"><%-blog.body%></p>
5654
</div>
5755
</div>
5856
</div>
59-
<center>
60-
<%if(currentUser && blog.author.id.equals(currentUser._id)) {%>
61-
<hr width="43%">
62-
<span style="display: inline-flex;">
63-
<form action="/blog/<%=blog._id%>/edit/">
64-
<button class="button is-info is-outlined" style="margin-right: 50px;">Edit</button>
65-
</form>
66-
<form action="/blog/<%=blog._id%>?_method=DELETE" method="POST">
67-
<button class="button is-danger is-outlined">Delete</button>
57+
<center>
58+
<%if(currentUser && blog.author.id.equals(currentUser._id)) {%>
59+
<hr width="43%">
60+
<span style="display: inline-flex;">
61+
<form action="/blog/<%=blog._id%>/edit/">
62+
<button class="button is-info is-outlined" style="margin-right: 50px;">Edit</button>
63+
</form>
64+
<form action="/blog/<%=blog._id%>?_method=DELETE" method="POST">
65+
<button class="button is-danger is-outlined">Delete</button>
66+
</form>
67+
</span>
68+
<%}%>
69+
</center>
70+
<hr>
71+
<div class="container">
72+
<%if(currentUser){%>
73+
<div class="columns is-centered is-multiline">
74+
<div class="column is-6">
75+
<form action="/blog/<%=blog._id%>/comment" method="POST">
76+
<div class="field">
77+
<label class="label">Write a comment:</label>
78+
<input class="input is-success" type="text" name="comment" placeholder="Comment..." required>
79+
</div>
80+
<button class="button is-success is-outlined">Comment</button>
6881
</form>
69-
</span>
70-
<%}%>
71-
</center>
72-
73-
<hr/>
82+
</div>
83+
</div>
84+
<%}%>
85+
<% blog.comments.forEach(function(comment){%>
86+
<div class="columns is-centered is-multiline">
87+
<div class="column is-6">
88+
<strong><%=comment.author.username%></strong> - <%=comment.text%>
89+
</div>
90+
</div>
91+
<%})%>
92+
</div>
7493
<%- include('../partials/footer') %>

views/partials/header.ejs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</head>
1212
<body>
1313
<!-- ------------------------------------MODAL---- -->
14-
<div class="modal animated fadeInDown" id="regmodal">
14+
<div class="modal animated fadeIn" id="regmodal">
1515
<div class="modal-background"></div>
1616
<!-- ----------------------------------------
1717
SIGN UP AND LOGIN FORM----------------
@@ -37,10 +37,10 @@
3737
</div>
3838
<footer class="card-footer">
3939
<p class="card-footer-item">
40-
<button formaction="/login" class="button is-primary is-outlined btn-grad" style="width: 100%;">Login</button>
40+
<button formaction="/login" class="button is-primary is-rounded" style="width: 100%;">Login</button>
4141
</p>
4242
<p class="card-footer-item">
43-
<button class="button is-primary is-outlined" style="width: 100%;">Sign Up</button>
43+
<button class="button is-info is-outlined is-rounded" style="width: 100%;">Sign Up</button>
4444
</p>
4545
</footer>
4646
</div>

0 commit comments

Comments
 (0)