mirror of
https://github.com/yangshun/tech-interview-handbook.git
synced 2025-07-30 05:34:33 +08:00
[questions][feat] add question answer crud (#331)
Co-authored-by: Jeff Sieu <jeffsy00@gmail.com>
This commit is contained in:
@ -3,6 +3,7 @@ import superjson from 'superjson';
|
|||||||
import { companiesRouter } from './companies-router';
|
import { companiesRouter } from './companies-router';
|
||||||
import { createRouter } from './context';
|
import { createRouter } from './context';
|
||||||
import { protectedExampleRouter } from './protected-example-router';
|
import { protectedExampleRouter } from './protected-example-router';
|
||||||
|
import { questionsAnswerRouter } from './questions-answer-router';
|
||||||
import { questionsQuestionCommentRouter } from './questions-question-comment-router';
|
import { questionsQuestionCommentRouter } from './questions-question-comment-router';
|
||||||
import { questionsQuestionRouter } from './questions-question-router';
|
import { questionsQuestionRouter } from './questions-question-router';
|
||||||
import { resumesRouter } from './resumes/resumes-resume-router';
|
import { resumesRouter } from './resumes/resumes-resume-router';
|
||||||
@ -27,6 +28,7 @@ export const appRouter = createRouter()
|
|||||||
.merge('resumes.star.user.', resumesStarUserRouter)
|
.merge('resumes.star.user.', resumesStarUserRouter)
|
||||||
.merge('resumes.reviews.', resumeReviewsRouter)
|
.merge('resumes.reviews.', resumeReviewsRouter)
|
||||||
.merge('resumes.reviews.user.', resumesReviewsUserRouter)
|
.merge('resumes.reviews.user.', resumesReviewsUserRouter)
|
||||||
|
.merge('questions.answers.', questionsAnswerRouter)
|
||||||
.merge('questions.questions.comments.', questionsQuestionCommentRouter)
|
.merge('questions.questions.comments.', questionsQuestionCommentRouter)
|
||||||
.merge('questions.questions.', questionsQuestionRouter);
|
.merge('questions.questions.', questionsQuestionRouter);
|
||||||
|
|
||||||
|
235
apps/portal/src/server/router/questions-answer-router.ts
Normal file
235
apps/portal/src/server/router/questions-answer-router.ts
Normal file
@ -0,0 +1,235 @@
|
|||||||
|
import { z } from 'zod';
|
||||||
|
import { Vote } from '@prisma/client';
|
||||||
|
import { TRPCError } from '@trpc/server';
|
||||||
|
|
||||||
|
import { createProtectedRouter } from './context';
|
||||||
|
|
||||||
|
import type { Answer } from '~/types/questions';
|
||||||
|
|
||||||
|
export const questionsAnswerRouter = createProtectedRouter()
|
||||||
|
.query('getAnswers', {
|
||||||
|
input: z.object({
|
||||||
|
questionId: z.string(),
|
||||||
|
}),
|
||||||
|
async resolve({ ctx, input }) {
|
||||||
|
const answersData = await ctx.prisma.questionsAnswer.findMany({
|
||||||
|
include: {
|
||||||
|
_count: {
|
||||||
|
select: {
|
||||||
|
comments: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
user: {
|
||||||
|
select: {
|
||||||
|
name: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
votes: true,
|
||||||
|
},
|
||||||
|
orderBy: {
|
||||||
|
createdAt: 'desc',
|
||||||
|
},
|
||||||
|
where: {
|
||||||
|
...input,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return answersData.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 answer: Answer = {
|
||||||
|
content: data.content,
|
||||||
|
createdAt: data.createdAt,
|
||||||
|
id: data.id,
|
||||||
|
numComments: data._count.comments,
|
||||||
|
numVotes: votes,
|
||||||
|
user: userName,
|
||||||
|
};
|
||||||
|
return answer;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.mutation('create', {
|
||||||
|
input: z.object({
|
||||||
|
content: z.string(),
|
||||||
|
questionId: z.string(),
|
||||||
|
}),
|
||||||
|
async resolve({ ctx, input }) {
|
||||||
|
const userId = ctx.session?.user?.id;
|
||||||
|
|
||||||
|
return await ctx.prisma.questionsAnswer.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 {content, id} = input
|
||||||
|
|
||||||
|
const answerToUpdate = await ctx.prisma.questionsAnswer.findUnique({
|
||||||
|
where: {
|
||||||
|
id: input.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (answerToUpdate?.id !== userId) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: 'UNAUTHORIZED',
|
||||||
|
message: 'User have no authorization to record.',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return await ctx.prisma.questionsAnswer.update({
|
||||||
|
data: {
|
||||||
|
content,
|
||||||
|
},
|
||||||
|
where: {
|
||||||
|
id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.mutation('delete', {
|
||||||
|
input: z.object({
|
||||||
|
id: z.string(),
|
||||||
|
}),
|
||||||
|
async resolve({ ctx, input }) {
|
||||||
|
const userId = ctx.session?.user?.id;
|
||||||
|
|
||||||
|
const answerToDelete = await ctx.prisma.questionsAnswer.findUnique({
|
||||||
|
where: {
|
||||||
|
id: input.id,
|
||||||
|
},});
|
||||||
|
|
||||||
|
if (answerToDelete?.id !== userId) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: 'UNAUTHORIZED',
|
||||||
|
message: 'User have no authorization to record.',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return await ctx.prisma.questionsAnswer.delete({
|
||||||
|
where: {
|
||||||
|
id: input.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.query('getVote', {
|
||||||
|
input: z.object({
|
||||||
|
answerId: z.string(),
|
||||||
|
}),
|
||||||
|
async resolve({ ctx, input }) {
|
||||||
|
const userId = ctx.session?.user?.id;
|
||||||
|
const {answerId} = input
|
||||||
|
|
||||||
|
return await ctx.prisma.questionsAnswerVote.findUnique({
|
||||||
|
where: {
|
||||||
|
answerId_userId : { answerId, userId },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.mutation('createVote', {
|
||||||
|
input: z.object({
|
||||||
|
answerId: z.string(),
|
||||||
|
vote: z.nativeEnum(Vote),
|
||||||
|
}),
|
||||||
|
async resolve({ ctx, input }) {
|
||||||
|
const userId = ctx.session?.user?.id;
|
||||||
|
|
||||||
|
return await ctx.prisma.questionsAnswerVote.create({
|
||||||
|
data: {
|
||||||
|
...input,
|
||||||
|
userId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.mutation('updateVote', {
|
||||||
|
input: z.object({
|
||||||
|
id: z.string(),
|
||||||
|
vote: z.nativeEnum(Vote),
|
||||||
|
}),
|
||||||
|
async resolve({ ctx, input }) {
|
||||||
|
const userId = ctx.session?.user?.id;
|
||||||
|
const {id, vote} = input
|
||||||
|
|
||||||
|
const voteToUpdate = await ctx.prisma.questionsAnswerVote.findUnique({
|
||||||
|
where: {
|
||||||
|
id: input.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (voteToUpdate?.id !== userId) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: 'UNAUTHORIZED',
|
||||||
|
message: 'User have no authorization to record.',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return await ctx.prisma.questionsAnswerVote.update({
|
||||||
|
data: {
|
||||||
|
vote,
|
||||||
|
},
|
||||||
|
where: {
|
||||||
|
id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.mutation('deleteVote', {
|
||||||
|
input: z.object({
|
||||||
|
id: z.string(),
|
||||||
|
}),
|
||||||
|
async resolve({ ctx, input }) {
|
||||||
|
const userId = ctx.session?.user?.id;
|
||||||
|
|
||||||
|
const voteToDelete = await ctx.prisma.questionsAnswerVote.findUnique({
|
||||||
|
where: {
|
||||||
|
id: input.id,
|
||||||
|
},});
|
||||||
|
|
||||||
|
if (voteToDelete?.id !== userId) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: 'UNAUTHORIZED',
|
||||||
|
message: 'User have no authorization to record.',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return await ctx.prisma.questionsAnswerVote.delete({
|
||||||
|
where: {
|
||||||
|
id: input.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
11
apps/portal/src/types/questions.d.ts
vendored
11
apps/portal/src/types/questions.d.ts
vendored
@ -12,9 +12,18 @@ export type Question = {
|
|||||||
user: string;
|
user: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type Answer = {
|
||||||
|
content: string;
|
||||||
|
createdAt: Date;
|
||||||
|
id: string;
|
||||||
|
numComments: number;
|
||||||
|
numVotes: number;
|
||||||
|
};
|
||||||
|
|
||||||
export type QuestionComment = {
|
export type QuestionComment = {
|
||||||
content: string;
|
content: string;
|
||||||
createdAt: Date;
|
createdAt: Date;
|
||||||
id: string;
|
id: string;
|
||||||
numVotes: number;
|
numVotes: number;
|
||||||
};
|
user: string;
|
||||||
|
};
|
||||||
|
Reference in New Issue
Block a user