[questions][feat] add upvotes tracking

pull/410/head
hpkoh 3 years ago
parent f8cfa4f4f9
commit 8fd1f6f413

@ -0,0 +1,14 @@
/*
Warnings:
- Added the required column `upvotes` to the `QuestionsAnswerComment` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "QuestionsAnswer" ADD COLUMN "upvotes" INTEGER NOT NULL DEFAULT 0;
-- AlterTable
ALTER TABLE "QuestionsAnswerComment" ADD COLUMN "upvotes" INTEGER NOT NULL;
-- AlterTable
ALTER TABLE "QuestionsQuestionComment" ADD COLUMN "upvotes" INTEGER NOT NULL DEFAULT 0;

@ -454,7 +454,7 @@ model QuestionsQuestionComment {
id String @id @default(cuid()) id String @id @default(cuid())
questionId String questionId String
userId String? userId String?
upvotes Int upvotes Int @default(0)
content String @db.Text content String @db.Text
createdAt DateTime @default(now()) createdAt DateTime @default(now())
updatedAt DateTime @updatedAt updatedAt DateTime @updatedAt
@ -483,7 +483,7 @@ model QuestionsAnswer {
questionId String questionId String
userId String? userId String?
content String @db.Text content String @db.Text
upvotes Int upvotes Int @default(0)
createdAt DateTime @default(now()) createdAt DateTime @default(now())
updatedAt DateTime @updatedAt updatedAt DateTime @updatedAt

@ -166,13 +166,29 @@ export const questionsAnswerCommentRouter = createProtectedRouter()
const { answerCommentId, vote } = input; const { answerCommentId, vote } = input;
return await ctx.prisma.questionsAnswerCommentVote.create({ const incrementValue = vote === Vote.UPVOTE ? 1 : -1;
data: {
answerCommentId, const [answerCommentVote] = await ctx.prisma.$transaction([
userId, ctx.prisma.questionsAnswerCommentVote.create({
vote, data: {
}, answerCommentId,
}); userId,
vote,
},
}),
ctx.prisma.questionsAnswerComment.update({
data: {
upvotes: {
increment: incrementValue,
},
},
where: {
id: answerCommentId,
},
}),
]);
return answerCommentVote;
}, },
}) })
.mutation('updateVote', { .mutation('updateVote', {
@ -198,14 +214,30 @@ export const questionsAnswerCommentRouter = createProtectedRouter()
}); });
} }
return await ctx.prisma.questionsAnswerCommentVote.update({ const incrementValue = vote === Vote.UPVOTE ? 2 : -2;
data: {
vote, const [answerCommentVote] = await ctx.prisma.$transaction([
}, ctx.prisma.questionsAnswerCommentVote.update({
where: { data: {
id, vote,
}, },
}); where: {
id,
},
}),
ctx.prisma.questionsAnswerComment.update({
data: {
upvotes: {
increment: incrementValue,
},
},
where: {
id: voteToUpdate.answerCommentId,
},
}),
]);
return answerCommentVote;
}, },
}) })
.mutation('deleteVote', { .mutation('deleteVote', {
@ -229,10 +261,26 @@ export const questionsAnswerCommentRouter = createProtectedRouter()
}); });
} }
return await ctx.prisma.questionsAnswerCommentVote.delete({ const incrementValue = voteToDelete.vote === Vote.UPVOTE ? -1 : 1;
where: {
id: input.id, const [answerCommentVote] = await ctx.prisma.$transaction([
}, ctx.prisma.questionsAnswerCommentVote.delete({
}); where: {
id: input.id,
},
}),
ctx.prisma.questionsAnswerComment.update({
data: {
upvotes: {
increment: incrementValue,
},
},
where: {
id: voteToDelete.answerCommentId,
},
}),
]);
return answerCommentVote;
}, },
}); });

@ -229,13 +229,28 @@ export const questionsAnswerRouter = createProtectedRouter()
const { answerId, vote } = input; const { answerId, vote } = input;
return await ctx.prisma.questionsAnswerVote.create({ const incrementValue = vote === Vote.UPVOTE ? 1 : -1;
data: {
answerId, const [answerVote] = await ctx.prisma.$transaction([
userId, ctx.prisma.questionsAnswerVote.create({
vote, data: {
}, answerId,
}); userId,
vote,
},
}),
ctx.prisma.questionsAnswer.update({
data: {
upvotes: {
increment: incrementValue,
},
},
where: {
id: answerId,
},
}),
]);
return answerVote;
}, },
}) })
.mutation('updateVote', { .mutation('updateVote', {
@ -260,14 +275,30 @@ export const questionsAnswerRouter = createProtectedRouter()
}); });
} }
return await ctx.prisma.questionsAnswerVote.update({ const incrementValue = vote === Vote.UPVOTE ? 2 : -2;
data: {
vote, const [questionsAnswerVote] = await ctx.prisma.$transaction([
}, ctx.prisma.questionsAnswerVote.update({
where: { data: {
id, vote,
}, },
}); where: {
id,
},
}),
ctx.prisma.questionsAnswer.update({
data: {
upvotes: {
increment: incrementValue,
},
},
where: {
id: voteToUpdate.answerId,
},
}),
]);
return questionsAnswerVote;
}, },
}) })
.mutation('deleteVote', { .mutation('deleteVote', {
@ -290,10 +321,26 @@ export const questionsAnswerRouter = createProtectedRouter()
}); });
} }
return await ctx.prisma.questionsAnswerVote.delete({ const incrementValue = voteToDelete.vote === Vote.UPVOTE ? -1 : 1;
where: {
id: input.id, const [questionsAnswerVote] = await ctx.prisma.$transaction([
}, ctx.prisma.questionsAnswerVote.delete({
}); where: {
id: input.id,
},
}),
ctx.prisma.questionsAnswer.update({
data: {
upvotes: {
increment: incrementValue,
},
},
where: {
id: voteToDelete.answerId,
},
}),
]);
return questionsAnswerVote;
}, },
}); });

@ -166,13 +166,28 @@ export const questionsQuestionCommentRouter = createProtectedRouter()
const userId = ctx.session?.user?.id; const userId = ctx.session?.user?.id;
const { questionCommentId, vote } = input; const { questionCommentId, vote } = input;
return await ctx.prisma.questionsQuestionCommentVote.create({ const incrementValue: number = vote === Vote.UPVOTE ? 1 : -1;
data: {
questionCommentId, const [ questionCommentVote ] = await ctx.prisma.$transaction([
userId, ctx.prisma.questionsQuestionCommentVote.create({
vote, data: {
}, questionCommentId,
}); userId,
vote,
},
}),
ctx.prisma.questionsQuestionComment.update({
data: {
upvotes: {
increment: incrementValue,
},
},
where: {
id: questionCommentId,
},
}),
]);
return questionCommentVote;
}, },
}) })
.mutation('updateVote', { .mutation('updateVote', {
@ -198,14 +213,30 @@ export const questionsQuestionCommentRouter = createProtectedRouter()
}); });
} }
return await ctx.prisma.questionsQuestionCommentVote.update({ const incrementValue = vote === Vote.UPVOTE ? 2 : -2;
data: {
vote, const [questionCommentVote] = await ctx.prisma.$transaction([
}, ctx.prisma.questionsQuestionCommentVote.update({
where: { data: {
id, vote,
}, },
}); where: {
id,
},
}),
ctx.prisma.questionsQuestionComment.update({
data: {
upvotes: {
increment: incrementValue,
},
},
where: {
id: voteToUpdate.questionCommentId,
},
}),
]);
return questionCommentVote;
}, },
}) })
.mutation('deleteVote', { .mutation('deleteVote', {
@ -229,10 +260,25 @@ export const questionsQuestionCommentRouter = createProtectedRouter()
}); });
} }
return await ctx.prisma.questionsQuestionCommentVote.delete({ const incrementValue = voteToDelete.vote === Vote.UPVOTE ? -1 : 1;
where: {
id: input.id, const [questionCommentVote] = await ctx.prisma.$transaction([
}, ctx.prisma.questionsQuestionCommentVote.delete({
}); where: {
id: input.id,
},
}),
ctx.prisma.questionsQuestionComment.update({
data: {
upvotes: {
increment: incrementValue,
},
},
where: {
id: voteToDelete.questionCommentId,
},
}),
]);
return questionCommentVote;
}, },
}); });

Loading…
Cancel
Save