Skip to content

Added Comment Functionality to App #783

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

Open
wants to merge 1 commit 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
47 changes: 47 additions & 0 deletions controllers/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const Comment = require("../models/Comment")

module.exports = {
createComment: async (req, res) => {
try {
console.log(req.params)
await Comment.create({
comment: req.body.comment,
postId: req.params.id,
likes: 0,
user: req.user.id
});
console.log("Comment has been added!");
res.redirect("/post/"+req.params.id);
} catch (err) {
console.log(err);
}
// },
// likePost: async (req, res) => {
// try {
// await Post.findOneAndUpdate(
// { _id: req.params.id },
// {
// $inc: { likes: 1 },
// }
// );
// console.log("Likes +1");
// res.redirect(`/post/${req.params.id}`);
// } catch (err) {
// console.log(err);
// }
// },
// deletePost: async (req, res) => {
// try {
// // Find post by id
// let post = await Post.findById({ _id: req.params.id });
// // Delete image from cloudinary
// await cloudinary.uploader.destroy(post.cloudinaryId);
// // Delete post from db
// await Post.remove({ _id: req.params.id });
// console.log("Deleted Post");
// res.redirect("/profile");
// } catch (err) {
// res.redirect("/profile");
// }
}
};
4 changes: 3 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,8 @@ 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 comment = await Comment.find({postId: req.params.id}).sort({ createdAt: "desc" }).lean();
res.render("post.ejs", { post: post, user: req.user, comment: comment });
} 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,
},
postId: {
type: mongoose.Schema.Types.ObjectId,
ref: "Post",
},
likes: {
type: Number,
required: true,
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
},
createdAt: {
type: Date,
default: Date.now,
},
});

module.exports = mongoose.model("Comment", CommentSchema);
13 changes: 13 additions & 0 deletions routes/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const express = require("express");
const router = express.Router();
const commentsController = require("../controllers/comments");
const { ensureAuth, ensureGuest } = require("../middleware/auth");

//Comments Routes - simplified for now
router.post("/createComment/:id", commentsController.createComment);

// router.put("/likePost/:id", postsController.likePost);

// router.delete("/deletePost/:id", postsController.deletePost);

module.exports = router;
2 changes: 2 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const logger = require("morgan");
const connectDB = require("./config/database");
const mainRoutes = require("./routes/main");
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
22 changes: 22 additions & 0 deletions views/post.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,28 @@
<div class="col-3 mt-5">
<p><%= post.caption %></p>
</div>

<div class="mt-5">
<h2>Add a comment:</h2>
<form action="/comment/createComment/<%=post._id%>" method="POST">
<div class="mb-3">
<input type="text" id="comment" name="comment">
</div>
<button class="btn btn-primary" type="submit">Submit</button>
</form>
</div>

<div class="mt-5">
<h4>Comments:</h4>
<ul class="column list-unstyled">
<% for(var i=0; i<comment.length; i++) {%>
<li class="col-6 justify-content-between mt-5">
<%= comment[i].comment %>
</li>
<% } %>
</ul>
</div>

<div class="col-6 mt-5">
<a class="btn btn-primary" href="/profile">Return to Profile</a>
<a class="btn btn-primary" href="/feed">Return to Feed</a>
Expand Down