Skip to content

Commit

Permalink
Add userVoteComment to comment
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewquang512 committed Nov 5, 2023
1 parent 3e8467a commit 1e6c9af
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/Type_Definitions/Comment_Comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const commentDefs = gql`
input VoteCommentInput {
commentId: ID!
action: voteCommentAction!
userId: ID!
}
input CreateCommentInput {
Expand Down Expand Up @@ -77,7 +78,7 @@ const commentDefs = gql`
votes: Int!
children: [Comment]
userVotePost: [String]!
userVoteComment: [String]!
}
`;

Expand Down
2 changes: 1 addition & 1 deletion src/prisma/ERD.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ model Comment {
children Comment[] @relation(name: "Comment_Children")
votes Int @default(0)
userVoteComment String[]
}

model Contest {
Expand Down
14 changes: 12 additions & 2 deletions src/resolvers/Mutation/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ const commentMutation = {
/**
*
* @param {*} parent
* @param {{data: {commentId: string, action: UPVOTE | DOWNVOTE}}} args
* @param {{data: {commentId: string, userId: string, action: UPVOTE | DOWNVOTE}}} args
* @param {*} info
* @returns
*/
voteComment: async (parent, args, info) => {
const { action, commentId } = args.data;
const { action, commentId, userId } = args.data;

const existedComment = await prisma.comment.findUnique({
where: {
Expand All @@ -104,6 +104,10 @@ const commentMutation = {
throw Error('Comment Not Exsited');
}

if (existedComment.userVoteComment.length > 0 && existedComment.userVoteComment.includes(userId)) {
throw Error('User has voted this comment already');
}

switch (action) {
case VOTE_COMMENT_ACTION.DOWNVOTE:
return await prisma.comment.update({
Expand All @@ -112,6 +116,9 @@ const commentMutation = {
},
data: {
votes: existedComment.votes - 1,
userVoteComment: {
push: userId
}
},
});

Expand All @@ -122,6 +129,9 @@ const commentMutation = {
},
data: {
votes: existedComment.votes + 1,
userVoteComment: {
push: userId
}
},
});
default:
Expand Down

0 comments on commit 1e6c9af

Please sign in to comment.