diff --git a/apps/portal/prisma/migrations/20221024123252_add_upvotes_to_schema/migration.sql b/apps/portal/prisma/migrations/20221024123252_add_upvotes_to_schema/migration.sql new file mode 100644 index 00000000..81d336ec --- /dev/null +++ b/apps/portal/prisma/migrations/20221024123252_add_upvotes_to_schema/migration.sql @@ -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; diff --git a/apps/portal/prisma/schema.prisma b/apps/portal/prisma/schema.prisma index 887475b6..7c342eb3 100644 --- a/apps/portal/prisma/schema.prisma +++ b/apps/portal/prisma/schema.prisma @@ -454,7 +454,7 @@ model QuestionsQuestionComment { id String @id @default(cuid()) questionId String userId String? - upvotes Int + upvotes Int @default(0) content String @db.Text createdAt DateTime @default(now()) updatedAt DateTime @updatedAt @@ -483,7 +483,7 @@ model QuestionsAnswer { questionId String userId String? content String @db.Text - upvotes Int + upvotes Int @default(0) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt diff --git a/apps/portal/src/server/router/questions-answer-comment-router.ts b/apps/portal/src/server/router/questions-answer-comment-router.ts index 75977b41..63a9ede4 100644 --- a/apps/portal/src/server/router/questions-answer-comment-router.ts +++ b/apps/portal/src/server/router/questions-answer-comment-router.ts @@ -166,13 +166,29 @@ export const questionsAnswerCommentRouter = createProtectedRouter() const { answerCommentId, vote } = input; - return await ctx.prisma.questionsAnswerCommentVote.create({ - data: { - answerCommentId, - userId, - vote, - }, - }); + const incrementValue = vote === Vote.UPVOTE ? 1 : -1; + + const [answerCommentVote] = await ctx.prisma.$transaction([ + ctx.prisma.questionsAnswerCommentVote.create({ + data: { + answerCommentId, + userId, + vote, + }, + }), + ctx.prisma.questionsAnswerComment.update({ + data: { + upvotes: { + increment: incrementValue, + }, + }, + where: { + id: answerCommentId, + }, + }), + ]); + + return answerCommentVote; }, }) .mutation('updateVote', { @@ -198,14 +214,30 @@ export const questionsAnswerCommentRouter = createProtectedRouter() }); } - return await ctx.prisma.questionsAnswerCommentVote.update({ - data: { - vote, - }, - where: { - id, - }, - }); + const incrementValue = vote === Vote.UPVOTE ? 2 : -2; + + const [answerCommentVote] = await ctx.prisma.$transaction([ + ctx.prisma.questionsAnswerCommentVote.update({ + data: { + vote, + }, + where: { + id, + }, + }), + ctx.prisma.questionsAnswerComment.update({ + data: { + upvotes: { + increment: incrementValue, + }, + }, + where: { + id: voteToUpdate.answerCommentId, + }, + }), + ]); + + return answerCommentVote; }, }) .mutation('deleteVote', { @@ -229,10 +261,26 @@ export const questionsAnswerCommentRouter = createProtectedRouter() }); } - return await ctx.prisma.questionsAnswerCommentVote.delete({ - where: { - id: input.id, - }, - }); + const incrementValue = voteToDelete.vote === Vote.UPVOTE ? -1 : 1; + + 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; + }, }); diff --git a/apps/portal/src/server/router/questions-answer-router.ts b/apps/portal/src/server/router/questions-answer-router.ts index 5d386854..e2318ba7 100644 --- a/apps/portal/src/server/router/questions-answer-router.ts +++ b/apps/portal/src/server/router/questions-answer-router.ts @@ -229,13 +229,28 @@ export const questionsAnswerRouter = createProtectedRouter() const { answerId, vote } = input; - return await ctx.prisma.questionsAnswerVote.create({ - data: { - answerId, - userId, - vote, - }, - }); + const incrementValue = vote === Vote.UPVOTE ? 1 : -1; + + const [answerVote] = await ctx.prisma.$transaction([ + ctx.prisma.questionsAnswerVote.create({ + data: { + answerId, + userId, + vote, + }, + }), + ctx.prisma.questionsAnswer.update({ + data: { + upvotes: { + increment: incrementValue, + }, + }, + where: { + id: answerId, + }, + }), + ]); + return answerVote; }, }) .mutation('updateVote', { @@ -260,14 +275,30 @@ export const questionsAnswerRouter = createProtectedRouter() }); } - return await ctx.prisma.questionsAnswerVote.update({ - data: { - vote, - }, - where: { - id, - }, - }); + const incrementValue = vote === Vote.UPVOTE ? 2 : -2; + + const [questionsAnswerVote] = await ctx.prisma.$transaction([ + ctx.prisma.questionsAnswerVote.update({ + data: { + vote, + }, + where: { + id, + }, + }), + ctx.prisma.questionsAnswer.update({ + data: { + upvotes: { + increment: incrementValue, + }, + }, + where: { + id: voteToUpdate.answerId, + }, + }), + ]); + + return questionsAnswerVote; }, }) .mutation('deleteVote', { @@ -290,10 +321,26 @@ export const questionsAnswerRouter = createProtectedRouter() }); } - return await ctx.prisma.questionsAnswerVote.delete({ - where: { - id: input.id, - }, - }); + const incrementValue = voteToDelete.vote === Vote.UPVOTE ? -1 : 1; + + 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; + }, }); diff --git a/apps/portal/src/server/router/questions-question-comment-router.ts b/apps/portal/src/server/router/questions-question-comment-router.ts index e2f786f9..28cf3b9d 100644 --- a/apps/portal/src/server/router/questions-question-comment-router.ts +++ b/apps/portal/src/server/router/questions-question-comment-router.ts @@ -166,13 +166,28 @@ export const questionsQuestionCommentRouter = createProtectedRouter() const userId = ctx.session?.user?.id; const { questionCommentId, vote } = input; - return await ctx.prisma.questionsQuestionCommentVote.create({ - data: { - questionCommentId, - userId, - vote, - }, - }); + const incrementValue: number = vote === Vote.UPVOTE ? 1 : -1; + + const [ questionCommentVote ] = await ctx.prisma.$transaction([ + ctx.prisma.questionsQuestionCommentVote.create({ + data: { + questionCommentId, + userId, + vote, + }, + }), + ctx.prisma.questionsQuestionComment.update({ + data: { + upvotes: { + increment: incrementValue, + }, + }, + where: { + id: questionCommentId, + }, + }), + ]); + return questionCommentVote; }, }) .mutation('updateVote', { @@ -198,14 +213,30 @@ export const questionsQuestionCommentRouter = createProtectedRouter() }); } - return await ctx.prisma.questionsQuestionCommentVote.update({ - data: { - vote, - }, - where: { - id, - }, - }); + const incrementValue = vote === Vote.UPVOTE ? 2 : -2; + + const [questionCommentVote] = await ctx.prisma.$transaction([ + ctx.prisma.questionsQuestionCommentVote.update({ + data: { + vote, + }, + where: { + id, + }, + }), + ctx.prisma.questionsQuestionComment.update({ + data: { + upvotes: { + increment: incrementValue, + }, + }, + where: { + id: voteToUpdate.questionCommentId, + }, + }), + ]); + + return questionCommentVote; }, }) .mutation('deleteVote', { @@ -229,10 +260,25 @@ export const questionsQuestionCommentRouter = createProtectedRouter() }); } - return await ctx.prisma.questionsQuestionCommentVote.delete({ - where: { - id: input.id, - }, - }); + const incrementValue = voteToDelete.vote === Vote.UPVOTE ? -1 : 1; + + 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; }, });