Skip to content

Commit 99cb261

Browse files
committed
Added posts routes and comment create route
1 parent 71f9bc8 commit 99cb261

File tree

8 files changed

+194
-5
lines changed

8 files changed

+194
-5
lines changed

app.js

+90-2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ app.get("/",(req,res)=>{
5252
});
5353

5454
});
55+
// Posts Routes
5556
// Create Post route
5657
app.get("/f/:id/posts/create",isLoggedIn,(req,res)=>{
5758
res.render("posts/new");
@@ -61,20 +62,107 @@ app.post("/",isLoggedIn,(req, res)=>{
6162
id: req.user._id,
6263
username: req.user.username
6364
}
64-
Post.create(req.body.post,(err,newPost)=>{
65+
var post = {title:req.body.post.title,description:req.body.post.description,channel:req.body.post.channel,author:author};
66+
Post.create(post,(err,newPost)=>{
6567
if(err)
6668
{
6769
req.flash("error",err.message);
6870
return res.redirect("back");
6971
}
7072
else
7173
{
72-
console.log(newPost);
7374
res.redirect("/");
7475
}
7576
});
7677

7778
});
79+
// Show Post Route
80+
app.get("/f/:channel/:id",isLoggedIn,(req, res)=>{
81+
Post.findById(req.params.id).populate("comments").exec((err,foundPost)=>{
82+
if(err)
83+
{
84+
req.flash("error",err.message);
85+
return res.redirect("back");
86+
}
87+
else
88+
{
89+
res.render("posts/show",{post:foundPost});
90+
}
91+
});
92+
});
93+
// Edit Post Route
94+
app.get("/f/:channel/:id/edit",isLoggedIn,(req, res)=>{
95+
Post.findById(req.params.id,(err,foundPost)=>{
96+
if(err)
97+
{
98+
req.flash("error",err.message);
99+
return res.redirect("back");
100+
}
101+
else
102+
{
103+
res.render("posts/edit",{post:foundPost});
104+
}
105+
});
106+
});
107+
app.put("/f/:channel/:id",isLoggedIn,(req, res)=>{
108+
Post.findByIdAndUpdate(req.params.id,req.body.post,(err,updatedPost)=>{
109+
if(err)
110+
{
111+
req.flash("error",err.message);
112+
return res.redirect("back");
113+
}
114+
else
115+
{
116+
res.redirect("/f/"+req.params.channel+"/"+req.params.id);
117+
}
118+
});
119+
});
120+
// Comment Routes
121+
app.get("/f/:channel/:id/comments/new",isLoggedIn,(req, res)=>{
122+
Post.findById(req.params.id,(err,foundPost)=>{
123+
if(err)
124+
{
125+
req.flash("error",err.message);
126+
return res.redirect("back");
127+
}
128+
else
129+
{
130+
res.render("comments/new",{post:foundPost});
131+
}
132+
});
133+
});
134+
app.post("/f/:channel/:id/comments/new",isLoggedIn,(req, res)=>{
135+
Post.findById(req.params.id,(err,foundPost)=>{
136+
if(err)
137+
{
138+
req.flash("error",err.message);
139+
return res.redirect("back");
140+
}
141+
else
142+
{
143+
Comment.create(req.body.comment,(err,comment)=>{
144+
if(err)
145+
{
146+
req.flash("error",err.message);
147+
return res.redirect("back");
148+
}
149+
else
150+
{
151+
comment.author.id = req.user._id;
152+
comment.author.username = req.user.username;
153+
comment.parents.push(comment._id);
154+
comment.markModified('parents');
155+
comment.save();
156+
foundPost.comments.push(comment);
157+
foundPost.markModified('comments');
158+
foundPost.save();
159+
req.flash("success","Successfully added comment");
160+
res.redirect("/f/"+req.params.channel+"/"+req.params.id);
161+
}
162+
})
163+
}
164+
})
165+
})
78166
// ================
79167
// AUTH ROUTES
80168
// ================

models/posts.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ var postsSchema = new mongoose.Schema({
99
ref: "User"
1010
},
1111
username: String
12-
}
12+
},
13+
comments: [
14+
{
15+
type: mongoose.Schema.Types.ObjectId,
16+
ref: "Comment",
17+
}
18+
]
1319
});
1420
module.exports = mongoose.model("Post",postsSchema);

views/comments/new.ejs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<%-include("../partials/header.ejs")%>
2+
<div class="row">
3+
<form action="/f/<%=post.channel%>/<%=post._id%>/comments/new" class="col s12" method="POST">
4+
<div class="container">
5+
<div class="row">
6+
<div class="col s12 input-field">
7+
<label for="">Comment</label>
8+
<input type="text" name="comment[text]">
9+
</div>
10+
</div>
11+
<div class="row">
12+
<div class="col s12 input-field center-align">
13+
<button class="btn waves-effect waves-light" type="submit" name="action">Submit
14+
<i class="material-icons right">send</i>
15+
</button>
16+
<p>Go <a href="/f/<%=post.channel%>/<%=post._id%>">back</a></p>
17+
</div>
18+
</div>
19+
</div>
20+
</form>
21+
</div>
22+
<%-include("../partials/footer.ejs")%>

views/index.ejs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<h6><%=posts[i].author.username%></h6>
1313
</div>
1414
<div class="card-action">
15-
<a href="" class="waves-effect blue lighten-1 white-text btn">View</a>
15+
<a href="/f/<%=posts[i].channel%>/<%=posts[i]._id%>" class="waves-effect blue lighten-1 white-text btn">View</a>
1616
</div>
1717
</div>
1818
</div>

views/partials/header.ejs

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
<nav>
1515
<div class="nav-wrapper">
1616
<div class="container">
17-
<a href="#" class="brand-logo">Forum</a>
17+
<a href="/" class="brand-logo">Forum</a>
1818
<ul id="nav-mobile" class="right hide-on-med-and-down">
1919
<%if(currentUser){%>
2020
<li><a href="/f/<%=currentUser._id%>/posts/create">Create Post</a></li>
21+
<li><a href="/"><%=currentUser.username%></a></li>
2122
<%}%>
2223
<li><a href="/login">Login</a></li>
2324
<li><a href="/logout">Logout</a></li>

views/posts/edit.ejs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<%-include("../partials/header.ejs")%>
2+
<div class="container">
3+
<h3 class="center-align">Edit Form</h3>
4+
<div class="row">
5+
<form action="/f/<%=post.channel%>/<%=post._id%>?_method=PUT" method="POST" class="col s12">
6+
<div class="container">
7+
<div class="row">
8+
<div class="col s12 input-field">
9+
<label for="">Title</label>
10+
<input type="text" required value="<%=post.title%>" name="post[title]">
11+
</div>
12+
</div>
13+
<div class="row">
14+
<div class="col s12 input-field">
15+
<label for="">Description</label>
16+
<textarea name="post[description]" required cols="30" rows="10"><%=post.description%></textarea>
17+
</div>
18+
</div>
19+
<div class="row">
20+
<div class="col s12 input-field">
21+
<label for="">Channel</label>
22+
<input type="text" required value="<%=post.channel%>" name="post[channel]">
23+
</div>
24+
</div>
25+
<div class="row">
26+
<div class="col s12 input-field center-align">
27+
<button class="btn waves-effect waves-light" type="submit" name="action">Submit
28+
<i class="material-icons right">send</i>
29+
</button>
30+
</div>
31+
</div>
32+
</div>
33+
</form>
34+
</div>
35+
</div>
36+
<%-include("../partials/footer.ejs")%>

views/posts/new.ejs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<%-include("../partials/header.ejs")%>
22
<div class="row">
33
<form action="/" method="POST" class="col s12">
4+
<h3 class="center-align">New Post</h3>
45
<div class="container">
56
<div class="row">
67
<div class="col s12 input-field">

views/posts/show.ejs

+35
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,37 @@
11
<%-include("../partials/header.ejs")%>
2+
<div class="container">
3+
<div class="row">
4+
<div class="col s12">
5+
<h3 class="center-align"><%=post.title%></h3>
6+
<div class="card">
7+
<div class="card-content">
8+
<p><%=post.description%></p>
9+
<h5>By: <%=post.author.username%></h5>
10+
</div>
11+
<div class="card-action">
12+
<a href="/f/<%=post.channel%>/<%=post._id%>/edit" class="btn waves-effect orange lighten-1 white-text">Edit</a>
13+
<form action="/f/<%=post.channel%>/<%=post._id%>?_method=DELETE" style="display: inline;">
14+
<button class="btn waves-effect red lighten-1 white-text">Delete</button>
15+
</form>
16+
<a href="/f/<%=post.channel%>/<%=post._id%>/comments/new" class="btn waves-effect blue lighten-1 white-text">Add Comment</a>
17+
</div>
18+
</div>
19+
</div>
20+
</div>
21+
<%if(post.comments.length!=0){%>
22+
<%for(var i=0;i<post.comments.length;i++){%>
23+
<h5>Comments</h5>
24+
<div class="row">
25+
<div class="col s12">
26+
<div class="card">
27+
<div class="card-content">
28+
<h6><strong><%=post.comments[i].text%></strong></h6>
29+
<p>By: <strong><%=post.comments[i].author.username%></strong></p>
30+
</div>
31+
</div>
32+
</div>
33+
</div>
34+
<%}%>
35+
<%}%>
36+
</div>
237
<%-include("../partials/footer.ejs")%>

0 commit comments

Comments
 (0)