This repository has been archived by the owner on Dec 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from SU-CS308-22FA/comment_sevval
comment/ like feature added
- Loading branch information
Showing
11 changed files
with
930 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import Comment from "../models/commentModel.js"; | ||
import asyncHandler from "express-async-handler"; | ||
import User from "../models/userModel.js"; | ||
|
||
// @desc Get logged in user comments | ||
// @route GET /api/comments | ||
// @access Private | ||
const getComments = asyncHandler(async (req, res) => { | ||
|
||
const comments = await Comment.find(); //{ user: req.user._id } | ||
res.json(comments); | ||
|
||
}); | ||
|
||
|
||
//@description Fetch single Comment | ||
//@route GET /api/comments/:id | ||
//@access Public | ||
const getCommentById = asyncHandler(async (req, res) => { | ||
const comment = await Comment.findById(req.params.id); | ||
|
||
if (comment) { | ||
res.json(comment); | ||
} else { | ||
res.status(404).json({ message: "Comment not found" }); | ||
} | ||
|
||
res.json(comment); | ||
}); | ||
|
||
//@description Create single Comment | ||
//@route GET /api/comments/create | ||
//@access Private | ||
const CreateComment = asyncHandler(async (req, res) => { | ||
const { title, content,username} = req.body; | ||
|
||
if (!title || !content) { | ||
res.status(400); | ||
throw new Error("Please Fill all the feilds"); | ||
return; | ||
} else { | ||
|
||
|
||
const comment = new Comment({ user: req.user._id, title,content,username}); | ||
|
||
const createdComment = await comment.save(); | ||
|
||
res.status(201).json(createdComment); | ||
} | ||
}); | ||
|
||
//@description Delete single Comment | ||
//@route GET /api/comments/:id | ||
//@access Private | ||
const DeleteComment = asyncHandler(async (req, res) => { | ||
const comment = await Comment.findById(req.params.id); | ||
|
||
if (comment.user.toString() !== req.user._id.toString()) { | ||
res.status(401); | ||
throw new Error("You can't perform this action"); | ||
} | ||
|
||
if (comment) { | ||
await comment.remove(); | ||
res.json({ message: "Comment Removed" }); | ||
} else { | ||
res.status(404); | ||
throw new Error("Comment not Found"); | ||
} | ||
}); | ||
|
||
// @desc Update a comment | ||
// @route PUT /api/comments/:id | ||
// @access Private | ||
const UpdateComment = asyncHandler(async (req, res) => { | ||
const { title, content} = req.body; | ||
|
||
const comment = await Comment.findById(req.params.id); | ||
|
||
if (comment.user.toString() !== req.user._id.toString()) { | ||
res.status(401); | ||
throw new Error("You can't perform this action"); | ||
} | ||
|
||
if (comment) { | ||
comment.title = title; | ||
comment.content = content; | ||
|
||
|
||
const updatedComment = await comment.save(); | ||
res.json(updatedComment); | ||
} else { | ||
res.status(404); | ||
throw new Error("Comment not found"); | ||
} | ||
}); | ||
|
||
|
||
|
||
const LikeState = asyncHandler(async (req, res) => { | ||
const { title, content,likes,username} = req.body; | ||
|
||
const comment = await Comment.findById(req.params.id); | ||
console.log(comment.usersThatLikedTheComment); | ||
console.log(comment.usersThatLikedTheComment.includes(username)); | ||
|
||
|
||
if (comment) { | ||
if(comment.usersThatLikedTheComment.includes(username)==false){ | ||
comment.usersThatLikedTheComment.push(username); | ||
comment.title = title; | ||
comment.content = content; | ||
comment.likes=likes+1; | ||
const updatedComment = await comment.save(); | ||
res.json(updatedComment); | ||
} | ||
else{ | ||
comment.usersThatLikedTheComment.splice(comment.usersThatLikedTheComment.indexOf(username,0),1); | ||
comment.title = title; | ||
comment.content = content; | ||
comment.likes=likes-1; | ||
const updatedComment = await comment.save(); | ||
res.json(updatedComment); | ||
} | ||
|
||
} else { | ||
res.status(404); | ||
throw new Error("Comment not found"); | ||
} | ||
|
||
|
||
}); | ||
|
||
|
||
|
||
export { getCommentById, getComments, CreateComment, DeleteComment, UpdateComment,LikeState}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import mongoose from "mongoose"; | ||
|
||
|
||
const commentSchema = mongoose.Schema( | ||
{ | ||
title: { | ||
type: String, | ||
required: true, | ||
}, | ||
content: { | ||
type: String, | ||
required: true, | ||
}, | ||
username: { | ||
type: String, | ||
required:true, | ||
unique: false, | ||
}, | ||
likes: { | ||
type: Number, | ||
default: 0 | ||
}, | ||
|
||
usersThatLikedTheComment:{ | ||
type: Array, | ||
default: [], | ||
}, | ||
user: { | ||
type: mongoose.Schema.Types.ObjectId, | ||
required: true, | ||
ref: "User", | ||
}, | ||
}, | ||
{ | ||
timestamps: true, | ||
} | ||
); | ||
|
||
const Comment = mongoose.model("Comment", commentSchema); | ||
|
||
export default Comment; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
import express from "express"; | ||
import { | ||
getCommentById, | ||
getComments, | ||
CreateComment, | ||
DeleteComment, | ||
UpdateComment, | ||
LikeState, | ||
|
||
} from "../controllers/commentController.js"; | ||
const router = express.Router(); | ||
import { protect } from "../middleware/authMiddleware.js"; | ||
|
||
router.route("/").get(getComments); // protect, | ||
router | ||
.route("/:id") | ||
.get(getCommentById) | ||
.delete(protect, DeleteComment) | ||
.put(protect, UpdateComment); | ||
router.route("/create").post(protect,CreateComment); | ||
router.route("/likes/:id").put(protect,LikeState); | ||
|
||
|
||
|
||
|
||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.