diff --git a/controllers/comments.js b/controllers/comments.js new file mode 100644 index 000000000..a4bccfcea --- /dev/null +++ b/controllers/comments.js @@ -0,0 +1,17 @@ +const Comment = require("../models/Comment"); + +module.exports = { + createComment: async (req, res) => { + try { + await Comment.create({ + comment: req.body.comment, + likes: 0, + post: req.params.id, + }); + console.log("Comment has been added!"); + res.redirect("/post/"+req.params.id); + } catch (err) { + console.log(err); + } + }, +}; diff --git a/controllers/posts.js b/controllers/posts.js index a3e2dab5d..52248030b 100644 --- a/controllers/posts.js +++ b/controllers/posts.js @@ -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) => { @@ -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 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); } diff --git a/models/Comment.js b/models/Comment.js new file mode 100644 index 000000000..96d105779 --- /dev/null +++ b/models/Comment.js @@ -0,0 +1,22 @@ +const mongoose = require("mongoose"); + +const CommentSchema = new mongoose.Schema({ + comment: { + type: String, + required: true, + }, + likes: { + type: Number, + required: true, + }, + post: { + type: mongoose.Schema.Types.ObjectId, + ref: "Post", + }, + createdAt: { + type: Date, + default: Date.now, + }, +}); + +module.exports = mongoose.model("Comment", CommentSchema); diff --git a/public/css/style.css b/public/css/style.css index e69de29bb..e74ebd682 100644 --- a/public/css/style.css +++ b/public/css/style.css @@ -0,0 +1,3 @@ +body { + background-color: #999; +} \ No newline at end of file diff --git a/routes/comments.js b/routes/comments.js new file mode 100644 index 000000000..132806610 --- /dev/null +++ b/routes/comments.js @@ -0,0 +1,11 @@ +const express = require("express"); +const router = express.Router(); +const commentsController = require("../controllers/comments"); +const { ensureAuth, ensureGuest } = require("../middleware/auth"); + +//Comments Routes + +router.post("/createComment/:id", commentsController.createComment); + +module.exports = router; + \ No newline at end of file diff --git a/server.js b/server.js index 1718db010..a2d4541c5 100644 --- a/server.js +++ b/server.js @@ -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" }); @@ -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, () => { diff --git a/views/post.ejs b/views/post.ejs index c36a1c946..a38173cf4 100644 --- a/views/post.ejs +++ b/views/post.ejs @@ -27,6 +27,23 @@

<%= post.caption %>

+
+

Add a comment

+
+
+ + +
+ +
+
+
Return to Profile Return to Feed