From eb4d2f2ec6fe5a3ff213fe96c6b43b51061b7835 Mon Sep 17 00:00:00 2001 From: Sreesanjay Date: Sun, 10 Mar 2024 02:17:46 +0530 Subject: [PATCH 1/2] feat: added create comment and get comment api --- server/controller/discussion.js | 491 ++++++++++++++++---------------- server/models/commentModel.js | 27 ++ server/routes/communityRoute.js | 10 +- 3 files changed, 287 insertions(+), 241 deletions(-) diff --git a/server/controller/discussion.js b/server/controller/discussion.js index d315201..25ec014 100644 --- a/server/controller/discussion.js +++ b/server/controller/discussion.js @@ -1,5 +1,6 @@ const Discussion = require("../models/discussionModel"); const Members = require("../models/membersModel"); +const Comment = require("../models/commentModel"); const mongoose = require('mongoose') /** @@ -369,12 +370,12 @@ const dislikeDiscussion = async (req, res, next) => { res.status(400); throw new Error('discussion not found') } - const likedDiscussion = await Discussion.findOneAndUpdate({ _id: id }, { $pull: { likes: req.user?._id } }, { new: true }) - if (likedDiscussion) { + const dislikedDiscussion = await Discussion.findOneAndUpdate({ _id: id }, { $pull: { likes: req.user?._id } }, { new: true }) + if (dislikedDiscussion) { res.status(200).json({ success: true, message: 'discussion disliked', - likedDiscussion + dislikedDiscussion }) } else { throw new Error('internal server error') @@ -384,247 +385,257 @@ const dislikeDiscussion = async (req, res, next) => { } } -// /** -// * @desc rquest for add commment on a discussions -// * @route POST /api/community/discussions/comment -// * @access private -// */ -// export const addComment: RequestHandler = asyncHandler( -// async (req: Request, res: Response, next: NextFunction): Promise => { -// const { discussion_id } = req.body; -// if (!discussion_id) { -// res.status(400) -// return next(new Error("Invalid discussion")); -// } -// const newComment = await new Comment({ -// user_id: req.user?._id, -// post_id: req.body.discussion_id, -// content: req.body.content -// }) -// if (req.body.reply) { -// newComment.reply = req.body.reply -// } -// const comment = await newComment.save() +/** + * @desc rquest for add commment on a discussions + * @route POST /api/community/discussions/comment + * @access private + */ +const addComment = async (req, res, next) => { + try { + const { discussion_id } = req.body; + if (!discussion_id) { + res.status(400) + throw new Error('discussion not found') + } + const newComment = new Comment({ + user_id: req.user?._id, + discussion_id: req.body.discussion_id, + content: req.body.content + }) + if (req.body.reply) { + newComment.reply = req.body.reply + } + const comment = await newComment.save() -// if (comment) { -// const resComment = await Comment.aggregate([ -// { -// $match: { -// _id: comment._id -// } -// }, -// { -// $lookup: { -// from: 'userprofiles', -// localField: 'user_id', -// foreignField: 'user_id', -// as: 'user_details', -// pipeline: [ -// { -// $lookup: { -// from: 'users', -// localField: 'user_id', -// foreignField: '_id', -// as: "email", -// pipeline: [ -// { -// $project: { -// _id: 0, -// email: 1 -// } -// } -// ] - -// } -// }, -// { -// $unwind: { -// path: "$email" -// } -// }, -// { -// $project: { -// username: 1, -// profile_img: 1, -// email: '$email.email' -// } -// }, - -// ] -// } -// }, { -// $unwind: { -// path: '$user_details' -// } -// }, - -// ]) -// res.status(200).json({ -// status: 'ok', -// message: 'new Comment added', -// comment: resComment[0] -// }) -// } else { -// next(new Error('Internal server error')) -// } -// } -// ) + if (comment) { + const resComment = await Comment.aggregate([ + { + $match: { + _id: comment._id + } + }, + { + $lookup: { + from: 'profiles', + localField: 'user_id', + foreignField: 'user_id', + as: 'user_details', + pipeline: [ + { + $lookup: { + from: 'users', + localField: 'user_id', + foreignField: '_id', + as: "email", + pipeline: [ + { + $project: { + _id: 0, + email: 1 + } + } + ] -// /** -// * @desc rquest for fetching comments of a discussion -// * @route GET /api/community/discussions/comment/:id -// * @access private -// */ -// export const getComments: RequestHandler = asyncHandler( -// async (req: Request, res: Response, next: NextFunction): Promise => { -// const { id } = req.params; -// if (!id) { -// res.status(400) -// return next(new Error("Invalid discussion")); -// } + } + }, + { + $unwind: { + path: "$email" + } + }, + { + $project: { + username: 1, + profile_img: 1, + fullname: 1, + email: '$email.email' + } + }, + ] + } + }, { + $unwind: { + path: '$user_details' + } + }, -// const comments = await Comment.aggregate([ -// { -// $match: { -// post_id: new ObjectId(id) -// } -// }, -// { -// $lookup: { -// from: 'userprofiles', -// localField: 'user_id', -// foreignField: 'user_id', -// as: 'user_details', -// pipeline: [ -// { -// $lookup: { -// from: 'users', -// localField: 'user_id', -// foreignField: '_id', -// as: "email", -// pipeline: [ -// { -// $project: { -// _id: 0, -// email: 1 -// } -// } -// ] - -// } -// }, -// { -// $unwind: { -// path: "$email" -// } -// }, -// { -// $project: { -// username: 1, -// profile_img: 1, -// email: '$email.email' -// } -// }, - -// ] -// } -// }, { -// $unwind: { -// path: '$user_details' -// } -// }, - -// ]) -// if (comments) { -// res.status(200).json({ -// status: 'ok', -// message: 'comments fetched', -// comment: comments -// }) -// } else { -// next(new Error('Internal server error')) -// } -// } -// ) + ]) + res.status(200).json({ + success: true, + message: 'new Comment added', + comment: resComment[0] + }) + } else { + throw new Error('internal server error') + } + } catch (error) { + next(error.message) + } +} -// /** -// * @desc rquest for fetching replyies of comment -// * @route GET /api/community/discussions/comment/reply/:id -// * @access private -// */ -// export const getReplyCommemts: RequestHandler = asyncHandler( -// async (req: Request, res: Response, next: NextFunction): Promise => { -// const { id } = req.params; -// if (!id) { -// res.status(400) -// return next(new Error("Invalid comment")); -// } +/** + * @desc rquest for fetching comments of a discussion + * @route GET /api/community/discussions/comment/:id + * @access private + */ +const getComments = async (req, res, next) => { + try { + const { id } = req.params; + if (!id) { + res.status(400) + throw new Error('discussion not found') + } -// const comments = await Comment.aggregate([ -// { -// $match: { -// reply: new ObjectId(id) -// } -// }, -// { -// $lookup: { -// from: 'userprofiles', -// localField: 'user_id', -// foreignField: 'user_id', -// as: 'user_details', -// pipeline: [ -// { -// $lookup: { -// from: 'users', -// localField: 'user_id', -// foreignField: '_id', -// as: "email", -// pipeline: [ -// { -// $project: { -// _id: 0, -// email: 1 -// } -// } -// ] - -// } -// }, -// { -// $unwind: { -// path: "$email" -// } -// }, -// { -// $project: { -// username: 1, -// profile_img: 1, -// email: '$email.email' -// } -// }, - -// ] -// } -// }, { -// $unwind: { -// path: '$user_details' -// } -// }, - -// ]) -// if (comments) { -// res.status(200).json({ -// status: 'ok', -// message: 'reply comment fetched', -// comment: comments -// }) -// } else { -// next(new Error('Internal server error')) -// } -// } -// ) + const comments = await Comment.aggregate([ + { + $match: { + discussion_id: new mongoose.Types.ObjectId(id) + } + }, + { + $lookup: { + from: 'profiles', + localField: 'user_id', + foreignField: 'user_id', + as: 'user_details', + pipeline: [ + { + $lookup: { + from: 'users', + localField: 'user_id', + foreignField: '_id', + as: "email", + pipeline: [ + { + $project: { + _id: 0, + email: 1 + } + } + ] + + } + }, + { + $unwind: { + path: "$email" + } + }, + { + $project: { + username: 1, + profile_img: 1, + fullname: 1, + email: '$email.email' + } + }, + + ] + } + }, { + $unwind: { + path: '$user_details' + } + }, + + ]) + if (comments) { + res.status(200).json({ + success: true, + message: 'comments fetched', + comment: comments + }) + } else { + throw new Error('Internal server error') + } + } catch (error) { + next(error.message) + } +} + + +/** + * @desc rquest for fetching replyies of comment + * @route GET /api/community/discussions/comment/reply/:id + * @access private + */ +const getReplyCommemts = async (req, res, next) => { + try { + const { id } = req.params; + if (!id) { + res.status(400) + throw new Error('comment not found') + } + + + const comments = await Comment.aggregate([ + { + $match: { + reply: new mongoose.Types.ObjectId(id) + } + }, + { + $lookup: { + from: 'profiles', + localField: 'user_id', + foreignField: 'user_id', + as: 'user_details', + pipeline: [ + { + $lookup: { + from: 'users', + localField: 'user_id', + foreignField: '_id', + as: "email", + pipeline: [ + { + $project: { + _id: 0, + email: 1 + } + } + ] + + } + }, + { + $unwind: { + path: "$email" + } + }, + { + $project: { + username: 1, + fullname: 1, + profile_img: 1, + email: '$email.email' + } + }, + + ] + } + }, { + $unwind: { + path: '$user_details' + } + }, + + ]) + if (comments) { + res.status(200).json({ + success: true, + message: 'reply comment fetched', + comment: comments + }) + } else { + throw new Error('Internal server error') + } + } catch (error) { + next(error.message) + } +} // /** @@ -708,5 +719,9 @@ module.exports = { getDiscussions, getRecentDiscussion, deleteDiscussion, - likeDiscussion + likeDiscussion, + dislikeDiscussion, + addComment, + getComments, + getReplyCommemts } \ No newline at end of file diff --git a/server/models/commentModel.js b/server/models/commentModel.js index e69de29..aa17d76 100644 --- a/server/models/commentModel.js +++ b/server/models/commentModel.js @@ -0,0 +1,27 @@ +const mongoose = require("mongoose"); + +const commentSchema = new mongoose.Schema({ + discussion_id: { + type: mongoose.Types.ObjectId, + ref: 'Discussion' + }, + user_id: { + type: mongoose.Types.ObjectId, + ref: 'User' + }, + content: { + type: String, + required: true + }, + reply: { + type: mongoose.Types.ObjectId + }, + likes: [ + { + type: mongoose.Types.ObjectId, + ref: 'User' + } + ] +}, { timestamps: true }); + +module.exports = mongoose.model("Comment", commentSchema); \ No newline at end of file diff --git a/server/routes/communityRoute.js b/server/routes/communityRoute.js index e0a5058..790e64a 100644 --- a/server/routes/communityRoute.js +++ b/server/routes/communityRoute.js @@ -2,8 +2,7 @@ const express = require('express'); const { isLogedIn } = require('../middlewares/authMiddleware'); const router = express.Router(); const { createCommunity, getSuggestions, joinCommunity, acceptJoin, getmyCommunities } = require('../controller/community') -const { createDiscussion, getDiscussions, getRecentDiscussion, deleteDiscussion, likeDiscussion } = require('../controller/discussion') -// const { getDiscussions, createDiscussion, getRecentDiscussion, deleteDiscussion, likeDiscussion, dislikeDiscussion } = require('../controller/discussion') +const { createDiscussion, getDiscussions, getRecentDiscussion, deleteDiscussion, likeDiscussion, dislikeDiscussion, addComment, getComments, getReplyCommemts } = require('../controller/discussion') router.get('/get-suggestions', isLogedIn, getSuggestions) router.post('/', isLogedIn, createCommunity) @@ -18,5 +17,10 @@ router.get('/discussions/:id', isLogedIn, getDiscussions) router.post('/discussions', isLogedIn, createDiscussion) router.delete('/discussions/:id', isLogedIn, deleteDiscussion) router.put('/discussions/like/:id', isLogedIn, likeDiscussion) -// router.put('/discussions/dislike/:id', isLogedIn, dislikeDiscussion) +router.put('/discussions/dislike/:id', isLogedIn, dislikeDiscussion) + +router.post('/discussions/comment', isLogedIn, addComment) +router.get('/discussions/comment/:id', isLogedIn, getComments) +router.get('/discussions/comment/reply/:id', isLogedIn, getReplyCommemts) + module.exports = router; \ No newline at end of file From 311631a8ea405ed86a8bd8c5027cadf2d6452de5 Mon Sep 17 00:00:00 2001 From: Sreesanjay Date: Sun, 10 Mar 2024 02:49:03 +0530 Subject: [PATCH 2/2] feat: delete comment api added --- server/controller/discussion.js | 53 +++++++++++++++++---------------- server/routes/communityRoute.js | 3 +- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/server/controller/discussion.js b/server/controller/discussion.js index 25ec014..d20137c 100644 --- a/server/controller/discussion.js +++ b/server/controller/discussion.js @@ -638,30 +638,32 @@ const getReplyCommemts = async (req, res, next) => { } -// /** -// * @desc rquest for add commment on a discussions -// * @route DELETE /api/community/discussions/comment -// * @access private -// */ -// export const deleteComment: RequestHandler = asyncHandler( -// async (req: Request, res: Response, next: NextFunction): Promise => { -// const { id } = req.params; -// if (!id) { -// res.status(400) -// return next(new Error("Invalid comment")); -// } -// const deletedComment = await Comment.findOneAndDelete({ _id: id }) -// if (deletedComment) { -// res.status(200).json({ -// status: 'ok', -// message: 'comment deleted', -// deletedComment -// }) -// } else { -// next(new Error('Internal server error')) -// } -// } -// ) +/** + * @desc rquest for add commment on a discussions + * @route DELETE /api/community/discussions/comment + * @access private + */ +const deleteComment = async (req, res, next) => { + try { + const { id } = req.params; + if (!id) { + res.status(400) + throw new Error("Invalid comment"); + } + const deletedComment = await Comment.findOneAndDelete({ _id: id }) + if (deletedComment) { + res.status(200).json({ + status: 'ok', + message: 'comment deleted', + deletedComment + }) + } else { + throw new Error('internal server error') + } + } catch (error) { + next(error.message) + } +} // /** @@ -723,5 +725,6 @@ module.exports = { dislikeDiscussion, addComment, getComments, - getReplyCommemts + getReplyCommemts, + deleteComment } \ No newline at end of file diff --git a/server/routes/communityRoute.js b/server/routes/communityRoute.js index 790e64a..0f6fe64 100644 --- a/server/routes/communityRoute.js +++ b/server/routes/communityRoute.js @@ -2,7 +2,7 @@ const express = require('express'); const { isLogedIn } = require('../middlewares/authMiddleware'); const router = express.Router(); const { createCommunity, getSuggestions, joinCommunity, acceptJoin, getmyCommunities } = require('../controller/community') -const { createDiscussion, getDiscussions, getRecentDiscussion, deleteDiscussion, likeDiscussion, dislikeDiscussion, addComment, getComments, getReplyCommemts } = require('../controller/discussion') +const { createDiscussion, getDiscussions, getRecentDiscussion, deleteDiscussion, likeDiscussion, dislikeDiscussion, addComment, getComments, getReplyCommemts,deleteComment } = require('../controller/discussion') router.get('/get-suggestions', isLogedIn, getSuggestions) router.post('/', isLogedIn, createCommunity) @@ -22,5 +22,6 @@ router.put('/discussions/dislike/:id', isLogedIn, dislikeDiscussion) router.post('/discussions/comment', isLogedIn, addComment) router.get('/discussions/comment/:id', isLogedIn, getComments) router.get('/discussions/comment/reply/:id', isLogedIn, getReplyCommemts) +router.delete('/discussions/comment/:id', isLogedIn, deleteComment) module.exports = router; \ No newline at end of file