From 502513ce72a889f64fb2ed96318102a0811dead6 Mon Sep 17 00:00:00 2001 From: hpkoh Date: Sat, 8 Oct 2022 20:12:22 +0800 Subject: [PATCH] [questions][feat] add crud for questions --- .../router/questions-question-router.ts | 140 ++++++++++++++++++ apps/portal/src/types/questions-question.d.ts | 13 ++ 2 files changed, 153 insertions(+) diff --git a/apps/portal/src/server/router/questions-question-router.ts b/apps/portal/src/server/router/questions-question-router.ts index e69de29b..6f612b58 100644 --- a/apps/portal/src/server/router/questions-question-router.ts +++ b/apps/portal/src/server/router/questions-question-router.ts @@ -0,0 +1,140 @@ +import { z } from 'zod'; +import {QuestionsQuestionType, QuestionsVote } from '@prisma/client'; +import { TRPCError } from '@trpc/server'; + +import { createProtectedRouter } from './context'; + +import type { Question } from '~/types/questions-question'; + +export const questionsQuestionsRouter = createProtectedRouter() + .query('getQuestionsByFilter', { + input: z.object({ + content: z.string(), + questionType: z.nativeEnum(QuestionsQuestionType), + }), + async resolve({ ctx, input }) { + const questionsData = await ctx.prisma.questionsQuestion.findMany({ + include: { + _count: { + select: { + answers: true, + comments: true, + }, + }, + user: { + select: { + name: true, + }, + }, + votes: true, + }, + orderBy: { + createdAt: 'desc', + }, + }); + return questionsData.map((data) => { + const votes:number = data.votes.reduce( + (previousValue:number, currentValue) => { + let result:number = previousValue; + + switch(currentValue.vote) { + case QuestionsVote.NO_VOTE: + break; + case QuestionsVote.UPVOTE: + result += 1 + break; + case QuestionsVote.DOWNVOTE: + result -= 1 + break; + } + return result; + }, + 0 + ); + + let userName = ""; + + if (data.user) { + userName = data.user.name!; + } + + + const question: Question = { + company: "", + content: data.content, + id: data.id, + location: "", + numAnswers: data._count.answers, + numComments: data._count.comments, + numVotes: votes, + role: "", + updatedAt: data.updatedAt, + user: userName, + }; + return question; + }); + } + }) + .mutation('create', { + input: z.object({ + content: z.string(), + questionType: z.nativeEnum(QuestionsQuestionType), + }), + async resolve({ ctx, input }) { + const userId = ctx.session?.user?.id; + + return await ctx.prisma.questionsQuestion.create({ + data: { + ...input, + userId, + }, + }); + }, + }) + .mutation('update', { + input: z.object({ + content: z.string().optional(), + id: z.string(), + questionType: z.nativeEnum(QuestionsQuestionType).optional(), + + }), + async resolve({ ctx, input }) { + const userId = ctx.session?.user?.id; + + const questionToUpdate = await ctx.prisma.questionsQuestion.findUnique({ + where: { + id: input.id, + },}); + + if (questionToUpdate?.id !== userId) { + throw new TRPCError({ + code: 'UNAUTHORIZED', + message: 'User have no authorization to record.', + // Optional: pass the original error to retain stack trace + }); + } + // TODO: Check if session user owns this Question. + return await ctx.prisma.questionsQuestion.update({ + data: { + ...input, + userId, + }, + where: { + id: input.id, + }, + }); + }, + }) + .mutation('delete', { + input: z.object({ + id: z.string(), + }), + async resolve({ ctx, input }) { + // TODO: Check if session user owns this Todo. + return await ctx.prisma.questionsQuestion.delete({ + where: { + id: input.id, + }, + }); + }, + }); \ No newline at end of file diff --git a/apps/portal/src/types/questions-question.d.ts b/apps/portal/src/types/questions-question.d.ts index e69de29b..fa72f638 100644 --- a/apps/portal/src/types/questions-question.d.ts +++ b/apps/portal/src/types/questions-question.d.ts @@ -0,0 +1,13 @@ +export type Question = { + // TODO: company, location, role maps + company: string; + content: string; + id: string; + location: string; + numAnswers: number; + numComments: number; + numVotes: number; + role: string; + updatedAt: Date; + user: string; +}; \ No newline at end of file