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

Comment section added #745

Open
wants to merge 4 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
21 changes: 1 addition & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1 @@
# Install

`npm install`

---

# Things to add

- Create a `.env` file in config folder and add the following as `key = value`
- PORT = 2121 (can be any port example: 3000)
- DB_STRING = `your database URI`
- CLOUD_NAME = `your cloudinary cloud name`
- API_KEY = `your cloudinary api key`
- API_SECRET = `your cloudinary api secret`

---

# Run

`npm start`
added comment section and changed branch name to comment for PR
40 changes: 28 additions & 12 deletions controllers/posts.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
const cloudinary = require("../middleware/cloudinary");
const Post = require("../models/Post");
const cloudinary = require('../middleware/cloudinary');
const Post = require('../models/Post');
const Comment = require('../models/Comment');

module.exports = {
getProfile: async (req, res) => {
try {
const posts = await Post.find({ user: req.user.id });
res.render("profile.ejs", { posts: posts, user: req.user });
res.render('profile.ejs', { posts: posts, user: req.user });
} catch (err) {
console.log(err);
}
},
getFeed: async (req, res) => {
try {
const posts = await Post.find().sort({ createdAt: "desc" }).lean();
res.render("feed.ejs", { posts: posts });
const posts = await Post.find().sort({ createdAt: 'desc' }).lean();
res.render('feed.ejs', { posts: posts });
} catch (err) {
console.log(err);
}
},
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({ postId: req.params.id }).populate('user');
res.render('post.ejs', { post: post, comments: comments, user: req.user });
} catch (err) {
console.log(err);
}
Expand All @@ -39,8 +41,22 @@ module.exports = {
likes: 0,
user: req.user.id,
});
console.log("Post has been added!");
res.redirect("/profile");
console.log('Post has been added!');
res.redirect('/profile');
} catch (err) {
console.log(err);
}
},
createComment: async (req, res) => {
try {
await Comment.create({
comment: req.body.comment,
postId: req.params.id,
user: req.user.id,
});
console.log('Comment has been added!');
console.log(req.body.comment, req.params.id, req.user.id);
res.redirect(`/post/${req.params.id}`);
} catch (err) {
console.log(err);
}
Expand All @@ -53,7 +69,7 @@ module.exports = {
$inc: { likes: 1 },
}
);
console.log("Likes +1");
console.log('Likes +1');
res.redirect(`/post/${req.params.id}`);
} catch (err) {
console.log(err);
Expand All @@ -67,10 +83,10 @@ module.exports = {
await cloudinary.uploader.destroy(post.cloudinaryId);
// Delete post from db
await Post.remove({ _id: req.params.id });
console.log("Deleted Post");
res.redirect("/profile");
console.log('Deleted Post');
res.redirect('/profile');
} catch (err) {
res.redirect("/profile");
res.redirect('/profile');
}
},
};
22 changes: 22 additions & 0 deletions models/Comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const mongoose = require('mongoose');

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

module.exports = mongoose.model('Comment', CommentSchema);
6 changes: 3 additions & 3 deletions models/Post.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const mongoose = require("mongoose");
const mongoose = require('mongoose');

const PostSchema = new mongoose.Schema({
title: {
Expand All @@ -23,12 +23,12 @@ const PostSchema = new mongoose.Schema({
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
ref: 'User',
},
createdAt: {
type: Date,
default: Date.now,
},
});

module.exports = mongoose.model("Post", PostSchema);
module.exports = mongoose.model('Post', PostSchema);
Loading