-
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.
- Loading branch information
Showing
12 changed files
with
757 additions
and
141 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,45 @@ | ||
const mongoose = require("mongoose"); | ||
const TrendingComment = require('../schema/trendingComment'); | ||
const Vote = require('../schema/votes'); | ||
|
||
const getRepliesForComment = async (req, res) => { | ||
const commentId = req.params.commentId; // This is the ID of the parent comment | ||
const userId = req.params.userId; // Assuming you are using some form of user auth to get user info | ||
|
||
try { | ||
const replies = await TrendingComment.find({ parentId: commentId }) | ||
.lean() | ||
.sort({ createdAt: -1 }); | ||
|
||
// Aggregate votes for replies similarly as you did for comments | ||
const replyIds = replies.map(reply => reply._id); | ||
const votes = await Vote.aggregate([ | ||
{ $match: { commentId: { $in: replyIds } } }, | ||
{ $group: { _id: "$commentId", totalVotes: { $sum: "$voteType" }, | ||
userVote: { | ||
$max: { | ||
$cond: { if: { $eq: ["$userId", userId] }, then: "$voteType", else: 0 } | ||
} | ||
}}} | ||
]); | ||
|
||
const votesMap = votes.reduce((acc, vote) => { | ||
acc[vote._id.toString()] = { totalVotes: vote.totalVotes, userVote: vote.userVote }; | ||
return acc; | ||
}, {}); | ||
|
||
// Attach vote data to replies | ||
const repliesWithVotes = replies.map(reply => ({ | ||
...reply, | ||
votes: votesMap[reply._id.toString()] ? votesMap[reply._id.toString()].totalVotes : 0, | ||
userVote: votesMap[reply._id.toString()] ? votesMap[reply._id.toString()].userVote : 0, | ||
})); | ||
|
||
res.json(repliesWithVotes); | ||
} catch (error) { | ||
console.error('Error fetching replies:', error); | ||
res.status(500).json({ message: error.message }); | ||
} | ||
}; | ||
|
||
module.exports = getRepliesForComment; |
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 |
---|---|---|
@@ -1,18 +1,62 @@ | ||
const mongoose = require("mongoose"); | ||
const TrendingComment = require('../schema/trendingComment'); | ||
const Vote = require('../schema/votes'); | ||
|
||
const getTrendingComments = async (req, res) => { | ||
const userId = req.params.userId; // Assuming you have user information from session or token | ||
const resumeId = req.params.resumeId; | ||
|
||
try { | ||
let comments = await TrendingComment.find({ resumeId: req.params.resumeId, parentId: null }) | ||
.sort({ createdAt: -1 }); | ||
comments = await Promise.all(comments.map(async (comment) => { | ||
const replies = await TrendingComment.find({ parentId: comment._id }).sort({ createdAt: 1 }); | ||
return { ...comment.toObject(), replies }; | ||
// const comments = await TrendingComment.find({ resumeId, parentId: null}) | ||
// .lean() | ||
// .sort({ createdAt: -1 }); | ||
|
||
const comments = await TrendingComment.aggregate([ | ||
// Match top-level comments | ||
{ $match: { resumeId: mongoose.Types.ObjectId(resumeId), parentId: null } }, | ||
// Lookup to find replies and count them | ||
{ $lookup: { | ||
from: "trendingcomments", // assuming collection name is trendingcomments | ||
localField: "_id", | ||
foreignField: "parentId", | ||
as: "replies" | ||
}}, | ||
{ $addFields: { | ||
repliesCount: { $size: "$replies" } | ||
}}, | ||
{ $project: { replies: 0 } }, // Exclude the replies field from output | ||
// Sort comments by creation date | ||
{ $sort: { createdAt: -1 } } | ||
]); | ||
// Get vote counts and user-specific votes in one go, if possible | ||
const commentIds = comments.map(comment => comment._id); | ||
const votes = await Vote.aggregate([ | ||
{ $match: { commentId: { $in: commentIds } } }, | ||
{ $group: { _id: "$commentId", totalVotes: { $sum: "$voteType" }, | ||
userVote: { | ||
$max: { | ||
$cond: { if: { $eq: ["$userId", userId] }, then: "$voteType", else: 0 } | ||
} | ||
}}} | ||
]); | ||
|
||
const votesMap = votes.reduce((acc, vote) => { | ||
acc[vote._id.toString()] = { totalVotes: vote.totalVotes, userVote: vote.userVote }; | ||
return acc; | ||
}, {}); | ||
|
||
// Attach vote data to comments | ||
const commentsWithVotes = comments.map(comment => ({ | ||
...comment, | ||
votes: votesMap[comment._id.toString()] ? votesMap[comment._id.toString()].totalVotes : 0, | ||
userVote: votesMap[comment._id.toString()] ? votesMap[comment._id.toString()].userVote : 0, | ||
})); | ||
res.json(comments); | ||
|
||
res.json(commentsWithVotes); | ||
} catch (error) { | ||
console.error('Error fetching trending comments:', error); | ||
res.status(500).json({ message: error.message }); | ||
} | ||
}; | ||
|
||
module.exports = getTrendingComments; | ||
module.exports = getTrendingComments; |
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 |
---|---|---|
@@ -1,23 +1,61 @@ | ||
const mongoose = require("mongoose"); | ||
const Vote = require('../schema/votes'); | ||
const TrendingComment = require('../schema/trendingComment'); | ||
|
||
const voteOnComment = async (req, res) => { | ||
const { commentId, delta } = req.body; | ||
const { userId, commentId, voteType } = req.body; | ||
|
||
try { | ||
const comment = await TrendingComment.findByIdAndUpdate( | ||
commentId, | ||
{ $inc: { votes: delta } }, // Increment or decrement the votes field | ||
{ new: true } // Return the updated document | ||
); | ||
|
||
if (!comment) { | ||
return res.status(404).send({ message: "Comment not found" }); | ||
const existingVote = await Vote.findOne({ commentId, userId }); | ||
|
||
if (existingVote) { | ||
if (voteType === 0) { | ||
await existingVote.remove(); | ||
} else if (existingVote.voteType !== voteType) { | ||
existingVote.voteType = voteType; | ||
await existingVote.save(); | ||
} | ||
} else { | ||
if (voteType !== 0) { // Only save if voteType is not 0 | ||
const newVote = new Vote({ commentId, userId, voteType }); | ||
await newVote.save(); | ||
} | ||
} | ||
|
||
res.json(comment); | ||
await updateCommentVotes(commentId); | ||
res.status(200).json({ message: 'Vote registered successfully.' }); | ||
} catch (error) { | ||
res.status(500).json({ message: 'Error processing vote', error: error.message }); | ||
} | ||
}; | ||
|
||
// Helper function to update the vote count on a comment | ||
async function updateCommentVotes(commentId) { | ||
const votes = await Vote.aggregate([ | ||
{ $match: { commentId: mongoose.Types.ObjectId(commentId) } }, | ||
{ $group: { _id: null, totalVotes: { $sum: '$voteType' } } } | ||
]); | ||
|
||
const totalVotes = votes.length > 0 ? votes[0].totalVotes : 0; | ||
await TrendingComment.findByIdAndUpdate(commentId, { votes: totalVotes }); | ||
} | ||
|
||
|
||
const getVoteStatus = async (req, res) => { | ||
const { userId, commentId } = req.query; // Assuming userId and commentId are passed as query parameters | ||
|
||
try { | ||
const existingVote = await Vote.findOne({ commentId, userId }); | ||
|
||
if (existingVote) { | ||
res.status(200).json({ voteType: existingVote.voteType }); | ||
} else { | ||
res.status(200).json({ voteType: 0 }); // No vote | ||
} | ||
} catch (error) { | ||
console.error('Failed to update comment votes:', error); | ||
res.status(500).send({ message: "Failed to update comment votes due to an internal error." }); | ||
res.status(500).json({ message: 'Error fetching vote status', error: error.message }); | ||
} | ||
}; | ||
|
||
module.exports = voteOnComment; | ||
module.exports = { voteOnComment, getVoteStatus }; | ||
// module.exports = voteOnComment; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const mongoose = require('mongoose'); | ||
const Schema = mongoose.Schema; | ||
|
||
const voteSchema = new Schema({ | ||
commentId: { type: Schema.Types.ObjectId, ref: 'TrendingComment', required: true }, | ||
userId: { type: Schema.Types.ObjectId, ref: 'User', required: true }, | ||
voteType: { type: Number, required: true, validate: { | ||
validator: function(v) { | ||
return v === 1 || v === -1 || v === 0; // Ensures vote is either an upvote (+1) or a downvote (-1) | ||
}, | ||
message: props => `${props.value} is not a valid vote type!` | ||
}} | ||
}); | ||
|
||
module.exports = mongoose.model('Vote', voteSchema); |
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
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.