[questions][feat] add answer comment crud (#332)

Co-authored-by: Jeff Sieu <jeffsy00@gmail.com>
pull/347/head
hpkoh 2 years ago committed by GitHub
parent f28332ab88
commit 0335616d57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -3,6 +3,7 @@ import superjson from 'superjson';
import { companiesRouter } from './companies-router';
import { createRouter } from './context';
import { protectedExampleRouter } from './protected-example-router';
import { questionsAnswerCommentRouter } from './questions-answer-comment-router';
import { questionsAnswerRouter } from './questions-answer-router';
import { questionsQuestionCommentRouter } from './questions-question-comment-router';
import { questionsQuestionRouter } from './questions-question-router';
@ -28,6 +29,7 @@ export const appRouter = createRouter()
.merge('resumes.star.user.', resumesStarUserRouter)
.merge('resumes.reviews.', resumeReviewsRouter)
.merge('resumes.reviews.user.', resumesReviewsUserRouter)
.merge('questions.answers.comments.', questionsAnswerCommentRouter)
.merge('questions.answers.', questionsAnswerRouter)
.merge('questions.questions.comments.', questionsQuestionCommentRouter)
.merge('questions.questions.', questionsQuestionRouter);

@ -0,0 +1,229 @@
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 answerCommentToUpdate = await ctx.prisma.questionsAnswerComment.findUnique({
where: {
id: input.id,
},
});
if (answerCommentToUpdate?.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 answerCommentToDelete = await ctx.prisma.questionsAnswerComment.findUnique({
where: {
id: input.id,
},
});
if (answerCommentToDelete?.id !== userId) {
throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'User have no authorization to record.',
});
}
return await ctx.prisma.questionsAnswerComment.delete({
where: {
id: input.id,
},
});
},
})
.query('getVote', {
input: z.object({
answerCommentId: z.string(),
}),
async resolve({ ctx, input }) {
const userId = ctx.session?.user?.id;
const {answerCommentId} = input
return await ctx.prisma.questionsAnswerCommentVote.findUnique({
where: {
answerCommentId_userId : {answerCommentId,userId },
},
});
},
})
.mutation('createVote', {
input: z.object({
answerCommentId: z.string(),
vote: z.nativeEnum(Vote),
}),
async resolve({ ctx, input }) {
const userId = ctx.session?.user?.id;
return await ctx.prisma.questionsAnswerCommentVote.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.questionsAnswerCommentVote.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.questionsAnswerCommentVote.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.questionsAnswerCommentVote.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.questionsAnswerCommentVote.delete({
where: {
id: input.id,
},
});
},
});

@ -12,6 +12,12 @@ export type Question = {
user: string;
};
export type AnswerComment = {
content: string;
id: string;
numVotes: number;
};
export type Answer = {
content: string;
createdAt: Date;

Loading…
Cancel
Save