From bd3d9ee8cb49d7a77e9f70690766106b5a099b82 Mon Sep 17 00:00:00 2001 From: hpkoh Date: Sun, 9 Oct 2022 10:36:31 +0800 Subject: [PATCH] [questions][feat] add answer comment crud --- .../router/questions-answer-comment-router.ts | 140 ++++++++++++++++++ apps/portal/src/types/questions.d.ts | 7 + 2 files changed, 147 insertions(+) create mode 100644 apps/portal/src/server/router/questions-answer-comment-router.ts create mode 100644 apps/portal/src/types/questions.d.ts diff --git a/apps/portal/src/server/router/questions-answer-comment-router.ts b/apps/portal/src/server/router/questions-answer-comment-router.ts new file mode 100644 index 00000000..e86597bc --- /dev/null +++ b/apps/portal/src/server/router/questions-answer-comment-router.ts @@ -0,0 +1,140 @@ +import { z } from 'zod'; +import { Vote } from '@prisma/client'; +import { TRPCError } from '@trpc/server'; + +import { createProtectedRouter } from './context'; + +import type { AnswerComment } from '~/types/questions'; + +export const questionsAnswerCommentRouter = createProtectedRouter() + .query('getAnswerComments', { + input: z.object({ + answerId: z.string(), + }), + async resolve({ ctx, input }) { + const questionAnswerCommentsData = await ctx.prisma.questionsAnswerComment.findMany({ + include: { + user: { + select: { + name: true, + }, + }, + votes: true, + }, + orderBy: { + createdAt: 'desc', + }, + where: { + ...input, + }, + }); + return questionAnswerCommentsData.map((data) => { + const votes:number = data.votes.reduce( + (previousValue:number, currentValue) => { + let result:number = previousValue; + + switch(currentValue.vote) { + case Vote.UPVOTE: + result += 1 + break; + case Vote.DOWNVOTE: + result -= 1 + break; + } + return result; + }, + 0 + ); + + let userName = ""; + + if (data.user) { + userName = data.user.name!; + } + + + const answerComment: AnswerComment = { + content: data.content, + id: data.id, + numVotes: votes, + updatedAt: data.updatedAt, + user: userName, + }; + return answerComment; + }); + } + }) + .mutation('create', { + input: z.object({ + answerId: z.string(), + content: z.string(), + }), + async resolve({ ctx, input }) { + const userId = ctx.session?.user?.id; + + return await ctx.prisma.questionsAnswerComment.create({ + data: { + ...input, + userId, + }, + }); + }, + }) + .mutation('update', { + input: z.object({ + content: z.string().optional(), + id: z.string(), + }), + async resolve({ ctx, input }) { + const userId = ctx.session?.user?.id; + + const questionCommentToUpdate = await ctx.prisma.questionsAnswerComment.findUnique({ + where: { + id: input.id, + }, + }); + + if (questionCommentToUpdate?.id !== userId) { + throw new TRPCError({ + code: 'UNAUTHORIZED', + message: 'User have no authorization to record.', + }); + } + + return await ctx.prisma.questionsAnswerComment.update({ + data: { + ...input, + }, + where: { + id: input.id, + }, + }); + }, + }) + .mutation('delete', { + input: z.object({ + id: z.string(), + }), + async resolve({ ctx, input }) { + const userId = ctx.session?.user?.id; + + const questionCommentToUpdate = await ctx.prisma.questionsAnswerComment.findUnique({ + where: { + id: input.id, + }, + }); + + if (questionCommentToUpdate?.id !== userId) { + throw new TRPCError({ + code: 'UNAUTHORIZED', + message: 'User have no authorization to record.', + }); + } + + return await ctx.prisma.questionsAnswerComment.delete({ + where: { + id: input.id, + }, + }); + }, + }); \ No newline at end of file diff --git a/apps/portal/src/types/questions.d.ts b/apps/portal/src/types/questions.d.ts new file mode 100644 index 00000000..8fba30b9 --- /dev/null +++ b/apps/portal/src/types/questions.d.ts @@ -0,0 +1,7 @@ +export type AnswerComment = { + content: string; + id: string; + numVotes: number; + updatedAt: Date; + user: string; +}; \ No newline at end of file