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 comments functionality #767

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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@
# Run

`npm start`

---
43 changes: 43 additions & 0 deletions controllers/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const Comment = require("../models/Comments");

module.exports = {
createComment: async (req, res) => {
try {
await Comment.create({
comment: req.body.comment,
likes: 0,
post: req.params.id,
user: req.user.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.id },
{ $inc: { likes: 1 } }
);
console.log("Likes +1");
res.redirect(`/post/${req.body.postId}`);
} catch (err) {
console.log(err);
}
},
deleteComment: async (req, res) => {
try {
// Find comment by id
let post = await Comment.findByIdAndDelete(req.params.id);
console.log("Deleted Comment");
res.redirect(`/post/${req.body.postId}`);
} catch (err) {
console.log(err);
}
},
};

//COMMNETS:
//Try to fix likePost and deletePost to make them work
31 changes: 28 additions & 3 deletions controllers/posts.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
const cloudinary = require("../middleware/cloudinary");
const Post = require("../models/Post");
const Comment = require("../models/Comments");

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();
const posts = await Post
.find()
.sort({ createdAt: "desc" })
.populate('user', 'userName')
.lean();
res.render("feed.ejs", { posts: posts });
} catch (err) {
console.log(err);
Expand All @@ -21,7 +29,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 })
.sort({ createdAt: "asc" })
.populate('user', 'userName')
.lean();
res.render("post.ejs", {
post: post,
user: req.user,
comments: comments
});
} catch (err) {
console.log(err);
}
Expand Down Expand Up @@ -74,3 +91,11 @@ module.exports = {
}
},
};

//COMMENTS:

//In getFeed ".populate('user', 'userName')" is a mongoose method.
//It gets the user info associated with each post.
//This is necessary to show the user name of the creator
//of each post in the feed. The next thing we do is reference it
//in the feed.ejs file
14 changes: 14 additions & 0 deletions goals.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FUNCTIONALITY TO CHANGE/ADD

- Add comments - DONE
- Add likes to comments - DONE
- Add a route for comment's likes - DONE
- Include usernames with posts in feed - DONE
- Include post title in feed - DONE
- Include post caption in feed - DONE
- Button to make a post while in feed - DONE but needs fixing
- fix likePost and deletePost to make them work in the comments.js controller - DONE
- Allow OP to delete any comment on it's post
- Add nested comments. Watch Rascal's stream for that
- Allow users to change their username
- Add profile pictures for users
2 changes: 1 addition & 1 deletion middleware/multer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const path = require("path");
module.exports = multer({
storage: multer.diskStorage({}),
fileFilter: (req, file, cb) => {
let ext = path.extname(file.originalname);
let ext = path.extname(file.originalname).toLowerCase();
if (ext !== ".jpg" && ext !== ".jpeg" && ext !== ".png") {
cb(new Error("File type is not supported"), false);
return;
Expand Down
26 changes: 26 additions & 0 deletions models/Comments.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,
},
post: {
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);
7 changes: 7 additions & 0 deletions models/Post.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,10 @@ const PostSchema = new mongoose.Schema({
});

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

//COMMENTS:

//The user field is referencing the User.js model
//thru an ObjectId which basically ties the schemas together.
//We can use this to reference and later show the user name
//of the creator of each post in the feed.
10 changes: 8 additions & 2 deletions models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ const bcrypt = require("bcrypt");
const mongoose = require("mongoose");

const UserSchema = new mongoose.Schema({
userName: { type: String, unique: true },
email: { type: String, unique: true },
userName: {
type: String,
unique: true
},
email: {
type: String,
unique: true
},
password: String,
});

Expand Down
Loading