[questions][feat] refactor vote mutations

pull/457/head
hpkoh 3 years ago
parent 1c16930bf5
commit 10b081dc95

@ -100,130 +100,212 @@ export const questionsAnswerCommentUserRouter = createProtectedRouter()
}); });
}, },
}) })
.mutation('createVote', { .mutation('setUpVote', {
input: z.object({ input: z.object({
answerCommentId: z.string(), answerCommentId: z.string(),
vote: z.nativeEnum(Vote),
}), }),
async resolve({ ctx, input }) { async resolve({ ctx, input }) {
const userId = ctx.session?.user?.id; const userId = ctx.session?.user?.id;
const { answerCommentId } = input;
const { answerCommentId, vote } = input; return await ctx.prisma.$transaction(async (tx) => {
const vote = await tx.questionsAnswerCommentVote.findUnique({
where: {
answerCommentId_userId: { answerCommentId, userId },
},
})
const incrementValue = vote === Vote.UPVOTE ? 1 : -1; if (vote === null) {
const createdVote = await tx.questionsAnswerCommentVote.create({
data: {
answerCommentId,
userId,
vote: Vote.UPVOTE,
},
});
const [answerCommentVote] = await ctx.prisma.$transaction([ await tx.questionsAnswerComment.update({
ctx.prisma.questionsAnswerCommentVote.create({ data: {
data: { upvotes: {
answerCommentId, increment: 1,
userId, },
vote,
},
}),
ctx.prisma.questionsAnswerComment.update({
data: {
upvotes: {
increment: incrementValue,
}, },
}, where: {
where: { id: answerCommentId,
id: answerCommentId, },
}, });
}),
]); return createdVote
}
if (vote!.userId !== userId) {
throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'User have no authorization to record.',
});
}
if (vote!.vote === Vote.UPVOTE) {
return vote;
}
if (vote.vote === Vote.DOWNVOTE) {
tx.questionsAnswerCommentVote.delete({
where: {
id: vote.id,
},
});
return answerCommentVote; const createdVote = await tx.questionsAnswerCommentVote.create({
data: {
answerCommentId,
userId,
vote: Vote.UPVOTE,
},
});
await tx.questionsAnswerComment.update({
data: {
upvotes: {
increment: 2,
},
},
where: {
id: answerCommentId,
},
});
return createdVote
}
});
}, },
}) })
.mutation('updateVote', { .mutation('setDownVote', {
input: z.object({ input: z.object({
id: z.string(), answerCommentId: z.string(),
vote: z.nativeEnum(Vote),
}), }),
async resolve({ ctx, input }) { async resolve({ ctx, input }) {
const userId = ctx.session?.user?.id; const userId = ctx.session?.user?.id;
const { id, vote } = input; const { answerCommentId } = input;
const voteToUpdate = return await ctx.prisma.$transaction(async (tx) => {
await ctx.prisma.questionsAnswerCommentVote.findUnique({ const vote = await tx.questionsAnswerCommentVote.findUnique({
where: { where: {
id: input.id, answerCommentId_userId: { answerCommentId, userId },
}, },
}); })
if (voteToUpdate?.userId !== userId) { if (vote === null) {
throw new TRPCError({ const createdVote = await tx.questionsAnswerCommentVote.create({
code: 'UNAUTHORIZED', data: {
message: 'User have no authorization to record.', answerCommentId,
}); userId,
} vote: Vote.DOWNVOTE,
},
});
const incrementValue = vote === Vote.UPVOTE ? 2 : -2; await tx.questionsAnswerComment.update({
data: {
upvotes: {
increment: -1,
},
},
where: {
id: answerCommentId,
},
});
const [answerCommentVote] = await ctx.prisma.$transaction([ return createdVote
ctx.prisma.questionsAnswerCommentVote.update({ }
data: {
vote, if (vote!.userId !== userId) {
}, throw new TRPCError({
where: { code: 'UNAUTHORIZED',
id, message: 'User have no authorization to record.',
}, });
}), }
ctx.prisma.questionsAnswerComment.update({
data: { if (vote!.vote === Vote.DOWNVOTE) {
upvotes: { return vote;
increment: incrementValue, }
if (vote.vote === Vote.UPVOTE) {
tx.questionsAnswerCommentVote.delete({
where: {
id: vote.id,
}, },
}, });
where: {
id: voteToUpdate.answerCommentId, const createdVote = await tx.questionsAnswerCommentVote.create({
}, data: {
}), answerCommentId,
]); userId,
vote: Vote.DOWNVOTE,
},
});
return answerCommentVote; await tx.questionsAnswerComment.update({
data: {
upvotes: {
increment: -2,
},
},
where: {
id: answerCommentId,
},
});
return createdVote
}
});
}, },
}) })
.mutation('deleteVote', { .mutation('setNoVote', {
input: z.object({ input: z.object({
id: z.string(), answerCommentId: z.string(),
}), }),
async resolve({ ctx, input }) { async resolve({ ctx, input }) {
const userId = ctx.session?.user?.id; const userId = ctx.session?.user?.id;
const { answerCommentId } = input;
const voteToDelete = return await ctx.prisma.$transaction(async (tx) => {
await ctx.prisma.questionsAnswerCommentVote.findUnique({ const voteToDelete = await tx.questionsAnswerCommentVote.findUnique({
where: { where: {
id: input.id, answerCommentId_userId: { answerCommentId, userId },
}, },
}); })
if (voteToDelete?.userId !== userId) { if (voteToDelete === null) {
throw new TRPCError({ return null;
code: 'UNAUTHORIZED', }
message: 'User have no authorization to record.',
}); if (voteToDelete!.userId !== userId) {
} throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'User have no authorization to record.',
});
}
const incrementValue = voteToDelete.vote === Vote.UPVOTE ? -1 : 1; const incrementValue = voteToDelete!.vote === Vote.UPVOTE ? -1 : 1;
const [answerCommentVote] = await ctx.prisma.$transaction([ tx.questionsAnswerCommentVote.delete({
ctx.prisma.questionsAnswerCommentVote.delete({
where: { where: {
id: input.id, id: voteToDelete.id,
}, },
}), });
ctx.prisma.questionsAnswerComment.update({
await tx.questionsAnswerComment.update({
data: { data: {
upvotes: { upvotes: {
increment: incrementValue, increment: incrementValue,
}, },
}, },
where: { where: {
id: voteToDelete.answerCommentId, id: answerCommentId,
}, },
}), });
]);
return answerCommentVote; return voteToDelete;
});
}, },
}); });

@ -98,127 +98,212 @@ export const questionsAnswerUserRouter = createProtectedRouter()
}); });
}, },
}) })
.mutation('createVote', { .mutation('setUpVote', {
input: z.object({ input: z.object({
answerId: z.string(), answerId: z.string(),
vote: z.nativeEnum(Vote),
}), }),
async resolve({ ctx, input }) { async resolve({ ctx, input }) {
const userId = ctx.session?.user?.id; const userId = ctx.session?.user?.id;
const { answerId } = input;
const { answerId, vote } = input; return await ctx.prisma.$transaction(async (tx) => {
const vote = await tx.questionsAnswerVote.findUnique({
where: {
answerId_userId: { answerId, userId },
},
})
const incrementValue = vote === Vote.UPVOTE ? 1 : -1; if (vote === null) {
const createdVote = await tx.questionsAnswerVote.create({
data: {
answerId,
userId,
vote: Vote.UPVOTE,
},
});
const [answerVote] = await ctx.prisma.$transaction([ await tx.questionsAnswer.update({
ctx.prisma.questionsAnswerVote.create({ data: {
data: { upvotes: {
answerId, increment: 1,
userId, },
vote,
},
}),
ctx.prisma.questionsAnswer.update({
data: {
upvotes: {
increment: incrementValue,
}, },
}, where: {
where: { id: answerId,
id: answerId, },
}, });
}),
]); return createdVote
return answerVote; }
if (vote!.userId !== userId) {
throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'User have no authorization to record.',
});
}
if (vote!.vote === Vote.UPVOTE) {
return vote;
}
if (vote.vote === Vote.DOWNVOTE) {
tx.questionsAnswerVote.delete({
where: {
id: vote.id,
},
});
const createdVote = await tx.questionsAnswerVote.create({
data: {
answerId,
userId,
vote: Vote.UPVOTE,
},
});
await tx.questionsAnswer.update({
data: {
upvotes: {
increment: 2,
},
},
where: {
id: answerId,
},
});
return createdVote
}
});
}, },
}) })
.mutation('updateVote', { .mutation('setDownVote', {
input: z.object({ input: z.object({
id: z.string(), answerId: z.string(),
vote: z.nativeEnum(Vote),
}), }),
async resolve({ ctx, input }) { async resolve({ ctx, input }) {
const userId = ctx.session?.user?.id; const userId = ctx.session?.user?.id;
const { id, vote } = input; const { answerId } = input;
const voteToUpdate = await ctx.prisma.questionsAnswerVote.findUnique({ return await ctx.prisma.$transaction(async (tx) => {
where: { const vote = await tx.questionsAnswerVote.findUnique({
id: input.id, where: {
}, answerId_userId: { answerId, userId },
}); },
})
if (voteToUpdate?.userId !== userId) { if (vote === null) {
throw new TRPCError({ const createdVote = await tx.questionsAnswerVote.create({
code: 'UNAUTHORIZED', data: {
message: 'User have no authorization to record.', answerId,
}); userId,
} vote: Vote.DOWNVOTE,
},
});
const incrementValue = vote === Vote.UPVOTE ? 2 : -2; await tx.questionsAnswer.update({
data: {
upvotes: {
increment: -1,
},
},
where: {
id: answerId,
},
});
const [questionsAnswerVote] = await ctx.prisma.$transaction([ return createdVote
ctx.prisma.questionsAnswerVote.update({ }
data: {
vote, if (vote!.userId !== userId) {
}, throw new TRPCError({
where: { code: 'UNAUTHORIZED',
id, message: 'User have no authorization to record.',
}, });
}), }
ctx.prisma.questionsAnswer.update({
data: { if (vote!.vote === Vote.DOWNVOTE) {
upvotes: { return vote;
increment: incrementValue, }
if (vote.vote === Vote.UPVOTE) {
tx.questionsAnswerVote.delete({
where: {
id: vote.id,
}, },
}, });
where: {
id: voteToUpdate.answerId, const createdVote = await tx.questionsAnswerVote.create({
}, data: {
}), answerId,
]); userId,
vote: Vote.DOWNVOTE,
},
});
return questionsAnswerVote; await tx.questionsAnswer.update({
data: {
upvotes: {
increment: -2,
},
},
where: {
id: answerId,
},
});
return createdVote
}
});
}, },
}) })
.mutation('deleteVote', { .mutation('setNoVote', {
input: z.object({ input: z.object({
id: z.string(), answerId: z.string(),
}), }),
async resolve({ ctx, input }) { async resolve({ ctx, input }) {
const userId = ctx.session?.user?.id; const userId = ctx.session?.user?.id;
const { answerId } = input;
const voteToDelete = await ctx.prisma.questionsAnswerVote.findUnique({ return await ctx.prisma.$transaction(async (tx) => {
where: { const voteToDelete = await tx.questionsAnswerVote.findUnique({
id: input.id, where: {
}, answerId_userId: { answerId, userId },
}); },
})
if (voteToDelete?.userId !== userId) { if (voteToDelete === null) {
throw new TRPCError({ return null;
code: 'UNAUTHORIZED', }
message: 'User have no authorization to record.',
});
}
const incrementValue = voteToDelete.vote === Vote.UPVOTE ? -1 : 1; if (voteToDelete!.userId !== userId) {
throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'User have no authorization to record.',
});
}
const [questionsAnswerVote] = await ctx.prisma.$transaction([ const incrementValue = voteToDelete!.vote === Vote.UPVOTE ? -1 : 1;
ctx.prisma.questionsAnswerVote.delete({
tx.questionsAnswerVote.delete({
where: { where: {
id: input.id, id: voteToDelete.id,
}, },
}), });
ctx.prisma.questionsAnswer.update({
await tx.questionsAnswer.update({
data: { data: {
upvotes: { upvotes: {
increment: incrementValue, increment: incrementValue,
}, },
}, },
where: { where: {
id: voteToDelete.answerId, id: answerId,
}, },
}), });
]);
return questionsAnswerVote; return voteToDelete;
});
}, },
}); });

@ -101,128 +101,212 @@ export const questionsQuestionCommentUserRouter = createProtectedRouter()
}); });
}, },
}) })
.mutation('createVote', { .mutation('setUpVote', {
input: z.object({ input: z.object({
questionCommentId: z.string(), questionCommentId: z.string(),
vote: z.nativeEnum(Vote),
}), }),
async resolve({ ctx, input }) { async resolve({ ctx, input }) {
const userId = ctx.session?.user?.id; const userId = ctx.session?.user?.id;
const { questionCommentId, vote } = input; const { questionCommentId } = input;
const incrementValue: number = vote === Vote.UPVOTE ? 1 : -1;
const [questionCommentVote] = await ctx.prisma.$transaction([ return await ctx.prisma.$transaction(async (tx) => {
ctx.prisma.questionsQuestionCommentVote.create({ const vote = await tx.questionsQuestionCommentVote.findUnique({
data: {
questionCommentId,
userId,
vote,
},
}),
ctx.prisma.questionsQuestionComment.update({
data: {
upvotes: {
increment: incrementValue,
},
},
where: { where: {
id: questionCommentId, questionCommentId_userId: { questionCommentId, userId },
}, },
}), })
]);
return questionCommentVote; if (vote === null) {
const createdVote = await tx.questionsQuestionCommentVote.create({
data: {
questionCommentId,
userId,
vote: Vote.UPVOTE,
},
});
await tx.questionsQuestionComment.update({
data: {
upvotes: {
increment: 1,
},
},
where: {
id: questionCommentId,
},
});
return createdVote
}
if (vote!.userId !== userId) {
throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'User have no authorization to record.',
});
}
if (vote!.vote === Vote.UPVOTE) {
return vote;
}
if (vote.vote === Vote.DOWNVOTE) {
tx.questionsQuestionCommentVote.delete({
where: {
id: vote.id,
},
});
const createdVote = await tx.questionsQuestionCommentVote.create({
data: {
questionCommentId,
userId,
vote: Vote.UPVOTE,
},
});
await tx.questionsQuestionComment.update({
data: {
upvotes: {
increment: 2,
},
},
where: {
id: questionCommentId,
},
});
return createdVote
}
});
}, },
}) })
.mutation('updateVote', { .mutation('setDownVote', {
input: z.object({ input: z.object({
id: z.string(), questionCommentId: z.string(),
vote: z.nativeEnum(Vote),
}), }),
async resolve({ ctx, input }) { async resolve({ ctx, input }) {
const userId = ctx.session?.user?.id; const userId = ctx.session?.user?.id;
const { id, vote } = input; const { questionCommentId } = input;
const voteToUpdate = return await ctx.prisma.$transaction(async (tx) => {
await ctx.prisma.questionsQuestionCommentVote.findUnique({ const vote = await tx.questionsQuestionCommentVote.findUnique({
where: { where: {
id: input.id, questionCommentId_userId: { questionCommentId, userId },
}, },
}); })
if (voteToUpdate?.userId !== userId) { if (vote === null) {
throw new TRPCError({ const createdVote = await tx.questionsQuestionCommentVote.create({
code: 'UNAUTHORIZED', data: {
message: 'User have no authorization to record.', questionCommentId,
}); userId,
} vote: Vote.DOWNVOTE,
},
});
const incrementValue = vote === Vote.UPVOTE ? 2 : -2; await tx.questionsQuestionComment.update({
data: {
upvotes: {
increment: -1,
},
},
where: {
id: questionCommentId,
},
});
const [questionCommentVote] = await ctx.prisma.$transaction([ return createdVote
ctx.prisma.questionsQuestionCommentVote.update({ }
data: {
vote, if (vote!.userId !== userId) {
}, throw new TRPCError({
where: { code: 'UNAUTHORIZED',
id, message: 'User have no authorization to record.',
}, });
}), }
ctx.prisma.questionsQuestionComment.update({
data: { if (vote!.vote === Vote.DOWNVOTE) {
upvotes: { return vote;
increment: incrementValue, }
if (vote.vote === Vote.UPVOTE) {
tx.questionsQuestionCommentVote.delete({
where: {
id: vote.id,
}, },
}, });
where: {
id: voteToUpdate.questionCommentId, const createdVote = await tx.questionsQuestionCommentVote.create({
}, data: {
}), questionCommentId,
]); userId,
vote: Vote.DOWNVOTE,
},
});
await tx.questionsQuestionComment.update({
data: {
upvotes: {
increment: -2,
},
},
where: {
id: questionCommentId,
},
});
return questionCommentVote; return createdVote
}
});
}, },
}) })
.mutation('deleteVote', { .mutation('setNoVote', {
input: z.object({ input: z.object({
id: z.string(), questionCommentId: z.string(),
}), }),
async resolve({ ctx, input }) { async resolve({ ctx, input }) {
const userId = ctx.session?.user?.id; const userId = ctx.session?.user?.id;
const { questionCommentId } = input;
const voteToDelete = return await ctx.prisma.$transaction(async (tx) => {
await ctx.prisma.questionsQuestionCommentVote.findUnique({ const voteToDelete = await tx.questionsQuestionCommentVote.findUnique({
where: { where: {
id: input.id, questionCommentId_userId: { questionCommentId, userId },
}, },
}); })
if (voteToDelete?.userId !== userId) { if (voteToDelete === null) {
throw new TRPCError({ return null;
code: 'UNAUTHORIZED', }
message: 'User have no authorization to record.',
}); if (voteToDelete!.userId !== userId) {
} throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'User have no authorization to record.',
});
}
const incrementValue = voteToDelete.vote === Vote.UPVOTE ? -1 : 1; const incrementValue = voteToDelete!.vote === Vote.UPVOTE ? -1 : 1;
const [questionCommentVote] = await ctx.prisma.$transaction([ tx.questionsQuestionCommentVote.delete({
ctx.prisma.questionsQuestionCommentVote.delete({
where: { where: {
id: input.id, id: voteToDelete.id,
}, },
}), });
ctx.prisma.questionsQuestionComment.update({
await tx.questionsQuestionComment.update({
data: { data: {
upvotes: { upvotes: {
increment: incrementValue, increment: incrementValue,
}, },
}, },
where: { where: {
id: voteToDelete.questionCommentId, id: questionCommentId,
}, },
}), });
]);
return questionCommentVote; return voteToDelete;
});
}, },
}); });

@ -123,126 +123,212 @@ export const questionsQuestionUserRouter = createProtectedRouter()
}); });
}, },
}) })
.mutation('createVote', { .mutation('setUpVote', {
input: z.object({ input: z.object({
questionId: z.string(), questionId: z.string(),
vote: z.nativeEnum(Vote),
}), }),
async resolve({ ctx, input }) { async resolve({ ctx, input }) {
const userId = ctx.session?.user?.id; const userId = ctx.session?.user?.id;
const { questionId, vote } = input; const { questionId } = input;
const incrementValue = vote === Vote.UPVOTE ? 1 : -1;
const [questionVote] = await ctx.prisma.$transaction([ return await ctx.prisma.$transaction(async (tx) => {
ctx.prisma.questionsQuestionVote.create({ const vote = await tx.questionsQuestionVote.findUnique({
data: {
questionId,
userId,
vote,
},
}),
ctx.prisma.questionsQuestion.update({
data: {
upvotes: {
increment: incrementValue,
},
},
where: { where: {
id: questionId, questionId_userId: { questionId, userId },
}, },
}), })
]);
return questionVote; if (vote === null) {
const createdVote = await tx.questionsQuestionVote.create({
data: {
questionId,
userId,
vote: Vote.UPVOTE,
},
});
await tx.questionsQuestion.update({
data: {
upvotes: {
increment: 1,
},
},
where: {
id: questionId,
},
});
return createdVote
}
if (vote!.userId !== userId) {
throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'User have no authorization to record.',
});
}
if (vote!.vote === Vote.UPVOTE) {
return vote;
}
if (vote.vote === Vote.DOWNVOTE) {
tx.questionsQuestionVote.delete({
where: {
id: vote.id,
},
});
const createdVote = await tx.questionsQuestionVote.create({
data: {
questionId,
userId,
vote: Vote.UPVOTE,
},
});
await tx.questionsQuestion.update({
data: {
upvotes: {
increment: 2,
},
},
where: {
id: questionId,
},
});
return createdVote
}
});
}, },
}) })
.mutation('updateVote', { .mutation('setDownVote', {
input: z.object({ input: z.object({
id: z.string(), questionId: z.string(),
vote: z.nativeEnum(Vote),
}), }),
async resolve({ ctx, input }) { async resolve({ ctx, input }) {
const userId = ctx.session?.user?.id; const userId = ctx.session?.user?.id;
const { id, vote } = input; const { questionId } = input;
const voteToUpdate = await ctx.prisma.questionsQuestionVote.findUnique({ return await ctx.prisma.$transaction(async (tx) => {
where: { const vote = await tx.questionsQuestionVote.findUnique({
id: input.id, where: {
}, questionId_userId: { questionId, userId },
}); },
})
if (voteToUpdate?.userId !== userId) { if (vote === null) {
throw new TRPCError({ const createdVote = await tx.questionsQuestionVote.create({
code: 'UNAUTHORIZED', data: {
message: 'User have no authorization to record.', questionId,
}); userId,
} vote: Vote.DOWNVOTE,
},
});
const incrementValue = vote === Vote.UPVOTE ? 2 : -2; await tx.questionsQuestion.update({
data: {
upvotes: {
increment: -1,
},
},
where: {
id: questionId,
},
});
const [questionVote] = await ctx.prisma.$transaction([ return createdVote
ctx.prisma.questionsQuestionVote.update({ }
data: {
vote, if (vote!.userId !== userId) {
}, throw new TRPCError({
where: { code: 'UNAUTHORIZED',
id, message: 'User have no authorization to record.',
}, });
}), }
ctx.prisma.questionsQuestion.update({
data: { if (vote!.vote === Vote.DOWNVOTE) {
upvotes: { return vote;
increment: incrementValue, }
if (vote.vote === Vote.UPVOTE) {
tx.questionsQuestionVote.delete({
where: {
id: vote.id,
}, },
}, });
where: {
id: voteToUpdate.questionId, const createdVote = await tx.questionsQuestionVote.create({
}, data: {
}), questionId,
]); userId,
vote: Vote.DOWNVOTE,
},
});
await tx.questionsQuestion.update({
data: {
upvotes: {
increment: -2,
},
},
where: {
id: questionId,
},
});
return questionVote; return createdVote
}
});
}, },
}) })
.mutation('deleteVote', { .mutation('setNoVote', {
input: z.object({ input: z.object({
id: z.string(), questionId: z.string(),
}), }),
async resolve({ ctx, input }) { async resolve({ ctx, input }) {
const userId = ctx.session?.user?.id; const userId = ctx.session?.user?.id;
const { questionId } = input;
const voteToDelete = await ctx.prisma.questionsQuestionVote.findUnique({ return await ctx.prisma.$transaction(async (tx) => {
where: { const voteToDelete = await tx.questionsQuestionVote.findUnique({
id: input.id, where: {
}, questionId_userId: { questionId, userId },
}); },
})
if (voteToDelete?.userId !== userId) { if (voteToDelete === null) {
throw new TRPCError({ return null;
code: 'UNAUTHORIZED', }
message: 'User have no authorization to record.',
}); if (voteToDelete!.userId !== userId) {
} throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'User have no authorization to record.',
});
}
const incrementValue = voteToDelete.vote === Vote.UPVOTE ? -1 : 1; const incrementValue = voteToDelete!.vote === Vote.UPVOTE ? -1 : 1;
const [questionVote] = await ctx.prisma.$transaction([ tx.questionsQuestionVote.delete({
ctx.prisma.questionsQuestionVote.delete({
where: { where: {
id: input.id, id: voteToDelete.id,
}, },
}), });
ctx.prisma.questionsQuestion.update({
await tx.questionsQuestion.update({
data: { data: {
upvotes: { upvotes: {
increment: incrementValue, increment: incrementValue,
}, },
}, },
where: { where: {
id: voteToDelete.questionId, id: questionId,
}, },
}), });
]);
return questionVote; return voteToDelete;
});
}, },
}); });

Loading…
Cancel
Save