diff --git a/config/database.js b/config/database.js index 24102b6d5..38e2709d7 100644 --- a/config/database.js +++ b/config/database.js @@ -3,6 +3,7 @@ const mongoose = require("mongoose"); const connectDB = async () => { try { const conn = await mongoose.connect(process.env.DB_STRING, { + dbName: 'socials', useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false, diff --git a/controllers/comments.js b/controllers/comments.js new file mode 100644 index 000000000..e2e0f30aa --- /dev/null +++ b/controllers/comments.js @@ -0,0 +1,19 @@ +const Comment = require("../models/Comment"); + +module.exports = { + createComment: async (req, res) => { + try { + console.log(req.body); // Check what's in req.body + await Comment.create({ + comment: req.body.comment, + post: 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); + } + }, +}; diff --git a/controllers/posts.js b/controllers/posts.js index a3e2dab5d..5e0ee3061 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,16 @@ 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 }) + .populate("user") + .sort({ createdAt: "desc" }) + .lean(); + console.log(comments); + 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..d41bbc695 --- /dev/null +++ b/models/Comment.js @@ -0,0 +1,29 @@ +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, // Referencing ObjectId + ref: "Post", // Reference to the "Post" model + required: true, + }, + createdAt: { + type: Date, + default: Date.now, + }, + user: { + type: mongoose.Schema.Types.ObjectId, + ref: 'User', + required: true + } +}); + +module.exports = mongoose.model("Comment", CommentSchema); diff --git a/routes/comments.js b/routes/comments.js new file mode 100644 index 000000000..098706541 --- /dev/null +++ b/routes/comments.js @@ -0,0 +1,9 @@ +const express = require("express"); +const router = express.Router(); +const commentsController = require("../controllers/comments"); +const { ensureAuth, ensureGuest } = require("../middleware/auth"); + +//Post Routes - simplified for now +router.post("/createComment/:id", commentsController.createComment) + +module.exports = router; diff --git a/server.js b/server.js index 1718db010..5bf739b3a 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..f22771dc4 100644 --- a/views/post.ejs +++ b/views/post.ejs @@ -27,6 +27,24 @@
<%= post.caption %>