Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added user comments with like functionality #771

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions controllers/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const Comment = require("../models/Comment");

module.exports = {
createComment: async (req, res) => {
try {
await Comment.create({
comment: req.body.comment,
likes: 0,
user: req.user.id,
post: req.params.id,
});
console.log("Comment has been added!");
res.redirect(`/post/${req.params.id}`);
} catch (err) {
console.log(err);
}
},
likeComment: async (req, res) => {
try {
await Comment.findOneAndUpdate(
{ _id: req.params.commentid },
{
$inc: { likes: 1 },
}
);
console.log("Likes +1");
res.redirect(`/post/${req.params.postid}`);
} catch (err) {
console.log(err);
}
},
};
7 changes: 6 additions & 1 deletion controllers/posts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const cloudinary = require("../middleware/cloudinary");
const Post = require("../models/Post");
const Comment = require("../models/Comment");

module.exports = {
getProfile: async (req, res) => {
Expand All @@ -21,7 +22,11 @@ module.exports = {
getPost: async (req, res) => {
try {
const post = await Post.findById(req.params.id);
res.render("post.ejs", { post: post, user: req.user });
const comments = await Comment
.find({post: req.params.id})
.sort({createdAt: "desc"})
.lean();
res.render("post.ejs", { post: post, user: req.user, comments: comments });
} catch (err) {
console.log(err);
}
Expand Down
26 changes: 26 additions & 0 deletions models/Comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const mongoose = require("mongoose");

const CommentSchema = new mongoose.Schema({
comment: {
type: String,
required: true,
},
likes: {
type: Number,
required: true,
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
},
createdAt: {
type: Date,
default: Date.now,
},
post: {
type: mongoose.Schema.Types.ObjectId,
ref: "Post",
}
});

module.exports = mongoose.model("Comment", CommentSchema);
17 changes: 9 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"bcrypt": "^5.0.1",
"cloudinary": "^1.25.1",
"connect-mongo": "^3.2.0",
"dotenv": "^8.2.0",
"dotenv": "^8.6.0",
"ejs": "^3.1.6",
"express": "^4.17.1",
"express-flash": "^0.0.2",
Expand Down
10 changes: 10 additions & 0 deletions routes/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const express = require("express");
const router = express.Router();
const commentsController = require("../controllers/comments");
const { ensureAuth, ensureGuest } = require("../middleware/auth");

//Comment Routes - simplified for now
router.post("/createComment/:id", commentsController.createComment);
router.put("/likeComment/:postid/:commentid", commentsController.likeComment);

module.exports = router;
4 changes: 3 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const flash = require("express-flash");
const logger = require("morgan");
const connectDB = require("./config/database");
const mainRoutes = require("./routes/main");
const postRoutes = require("./routes/posts");
const postRoutes = require("./routes/posts")
const commentRoutes = require("./routes/comments")

//Use .env file in config folder
require("dotenv").config({ path: "./config/.env" });
Expand Down Expand Up @@ -56,6 +57,7 @@ app.use(flash());
//Setup Routes For Which The Server Is Listening
app.use("/", mainRoutes);
app.use("/post", postRoutes);
app.use("/comment", commentRoutes);

//Server Running
app.listen(process.env.PORT, () => {
Expand Down
29 changes: 28 additions & 1 deletion views/post.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<h2><%= post.title %></h2>
<img class="img-fluid" src="<%= post.image%>" />
<div class="row justify-content-between">
<form
<form
class="col-1"
action="/post/likePost/<%= post.id %>?_method=PUT"
method="POST"
Expand All @@ -23,8 +23,35 @@
</form>
<%}%>
</div>
<div>
<h2> Add a Comment</h2>
<form action="/comment/createComment/<%=post._id %>" method="POST">
<div class="mb-3">
<label for ="comment" class="form-label">Comment</label>
<input type ="text" class="form-control" id="comment" name="comment">
</div>
<button class="btn btn-primary" type="submit">comment</button>
</form>
</div>
<ul>
<% for(let i=0; i<comments.length; i++) {%>
<li class="col-6 justify-content-between mt-5">
<strong>User <%=comments[i].user%></strong>
<%=comments[i].comment%>
<%=comments[i].likes%>
<form
class="col-1"
action="/comment/likeComment/<%=post._id%>/<%=comments[i]._id%>?_method=PUT"
method="POST"
>
<button class="btn btn-primary fa fa-heart" type="submit"></button>
</form>
</li>
<% } %>
</ul>
</div>
<div class="col-3 mt-5">
<p><strong>User:</strong> <%= post.user%></p>
<p><%= post.caption %></p>
</div>
<div class="col-6 mt-5">
Expand Down