[questions][chore] update to use vote enum

pull/330/head
hpkoh 3 years ago
parent 395bd2916f
commit cca2466edc

@ -1,10 +1,10 @@
import { z } from 'zod'; import { z } from 'zod';
import {QuestionsVote } from '@prisma/client'; import { Vote } from '@prisma/client';
import { TRPCError } from '@trpc/server'; import { TRPCError } from '@trpc/server';
import { createProtectedRouter } from './context'; import { createProtectedRouter } from './context';
import type { QuestionComment } from '~/types/questions-question'; import type { QuestionComment } from '~/types/questions';
export const questionsQuestionCommentRouter = createProtectedRouter() export const questionsQuestionCommentRouter = createProtectedRouter()
.query('getQuestionComments', { .query('getQuestionComments', {
@ -14,11 +14,6 @@ export const questionsQuestionCommentRouter = createProtectedRouter()
async resolve({ ctx, input }) { async resolve({ ctx, input }) {
const questionCommentsData = await ctx.prisma.questionsQuestionComment.findMany({ const questionCommentsData = await ctx.prisma.questionsQuestionComment.findMany({
include: { include: {
_count: {
select: {
votes: true,
},
},
user: { user: {
select: { select: {
name: true, name: true,
@ -39,12 +34,10 @@ export const questionsQuestionCommentRouter = createProtectedRouter()
let result:number = previousValue; let result:number = previousValue;
switch(currentValue.vote) { switch(currentValue.vote) {
case QuestionsVote.NO_VOTE: case Vote.UPVOTE:
break;
case QuestionsVote.UPVOTE:
result += 1 result += 1
break; break;
case QuestionsVote.DOWNVOTE: case Vote.DOWNVOTE:
result -= 1 result -= 1
break; break;
} }
@ -59,15 +52,14 @@ export const questionsQuestionCommentRouter = createProtectedRouter()
userName = data.user.name!; userName = data.user.name!;
} }
const questionComment: QuestionComment = {
const question: QuestionComment = {
content: data.content, content: data.content,
createdAt: data.createdAt, createdAt: data.createdAt,
id: data.id, id: data.id,
numVotes: votes, numVotes: votes,
user: userName, user: userName,
}; };
return question; return questionComment;
}); });
} }
}) })
@ -105,14 +97,12 @@ export const questionsQuestionCommentRouter = createProtectedRouter()
throw new TRPCError({ throw new TRPCError({
code: 'UNAUTHORIZED', code: 'UNAUTHORIZED',
message: 'User have no authorization to record.', message: 'User have no authorization to record.',
// Optional: pass the original error to retain stack trace
}); });
} }
return await ctx.prisma.questionsQuestionComment.update({ return await ctx.prisma.questionsQuestionComment.update({
data: { data: {
...input, ...input,
userId,
}, },
where: { where: {
id: input.id, id: input.id,
@ -130,13 +120,13 @@ export const questionsQuestionCommentRouter = createProtectedRouter()
const questionCommentToUpdate = await ctx.prisma.questionsQuestionComment.findUnique({ const questionCommentToUpdate = await ctx.prisma.questionsQuestionComment.findUnique({
where: { where: {
id: input.id, id: input.id,
},}); },
});
if (questionCommentToUpdate?.id !== userId) { if (questionCommentToUpdate?.id !== userId) {
throw new TRPCError({ throw new TRPCError({
code: 'UNAUTHORIZED', code: 'UNAUTHORIZED',
message: 'User have no authorization to record.', message: 'User have no authorization to record.',
// Optional: pass the original error to retain stack trace
}); });
} }

Loading…
Cancel
Save