[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;
return await ctx.prisma.$transaction(async (tx) => {
const vote = await tx.questionsAnswerCommentVote.findUnique({
where: {
answerCommentId_userId: { answerCommentId, userId },
},
})
if (vote === null) {
const createdVote = await tx.questionsAnswerCommentVote.create({
data: {
answerCommentId,
userId,
vote: Vote.UPVOTE,
},
});
await tx.questionsAnswerComment.update({
data: {
upvotes: {
increment: 1,
},
},
where: {
id: answerCommentId,
},
});
return createdVote
}
if (vote!.userId !== userId) {
throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'User have no authorization to record.',
});
}
const { answerCommentId, vote } = input; if (vote!.vote === Vote.UPVOTE) {
return vote;
}
const incrementValue = vote === Vote.UPVOTE ? 1 : -1; if (vote.vote === Vote.DOWNVOTE) {
tx.questionsAnswerCommentVote.delete({
where: {
id: vote.id,
},
});
const [answerCommentVote] = await ctx.prisma.$transaction([ const createdVote = await tx.questionsAnswerCommentVote.create({
ctx.prisma.questionsAnswerCommentVote.create({
data: { data: {
answerCommentId, answerCommentId,
userId, userId,
vote, vote: Vote.UPVOTE,
}, },
}), });
ctx.prisma.questionsAnswerComment.update({
await tx.questionsAnswerComment.update({
data: { data: {
upvotes: { upvotes: {
increment: incrementValue, increment: 2,
}, },
}, },
where: { where: {
id: answerCommentId, id: answerCommentId,
}, },
}), });
]);
return answerCommentVote; 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 (vote === null) {
const createdVote = await tx.questionsAnswerCommentVote.create({
data: {
answerCommentId,
userId,
vote: Vote.DOWNVOTE,
}, },
}); });
if (voteToUpdate?.userId !== userId) { await tx.questionsAnswerComment.update({
data: {
upvotes: {
increment: -1,
},
},
where: {
id: answerCommentId,
},
});
return createdVote
}
if (vote!.userId !== 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.',
}); });
} }
const incrementValue = vote === Vote.UPVOTE ? 2 : -2; if (vote!.vote === Vote.DOWNVOTE) {
return vote;
}
const [answerCommentVote] = await ctx.prisma.$transaction([ if (vote.vote === Vote.UPVOTE) {
ctx.prisma.questionsAnswerCommentVote.update({ tx.questionsAnswerCommentVote.delete({
data: {
vote,
},
where: { where: {
id, id: vote.id,
}, },
}), });
ctx.prisma.questionsAnswerComment.update({
const createdVote = await tx.questionsAnswerCommentVote.create({
data: {
answerCommentId,
userId,
vote: Vote.DOWNVOTE,
},
});
await tx.questionsAnswerComment.update({
data: { data: {
upvotes: { upvotes: {
increment: incrementValue, increment: -2,
}, },
}, },
where: { where: {
id: voteToUpdate.answerCommentId, id: answerCommentId,
}, },
}), });
]);
return answerCommentVote; 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 === null) {
return null;
}
if (voteToDelete?.userId !== userId) { if (voteToDelete!.userId !== 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.',
}); });
} }
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: {
upvotes: {
increment: 1,
},
},
where: {
id: answerId,
},
});
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.questionsAnswerVote.delete({
where: {
id: vote.id,
},
});
const createdVote = await tx.questionsAnswerVote.create({
data: { data: {
answerId, answerId,
userId, userId,
vote, vote: Vote.UPVOTE,
}, },
}), });
ctx.prisma.questionsAnswer.update({
await tx.questionsAnswer.update({
data: { data: {
upvotes: { upvotes: {
increment: incrementValue, increment: 2,
}, },
}, },
where: { where: {
id: answerId, id: answerId,
}, },
}), });
]);
return answerVote; 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) => {
const vote = await tx.questionsAnswerVote.findUnique({
where: { where: {
id: input.id, answerId_userId: { answerId, userId },
},
})
if (vote === null) {
const createdVote = await tx.questionsAnswerVote.create({
data: {
answerId,
userId,
vote: Vote.DOWNVOTE,
}, },
}); });
if (voteToUpdate?.userId !== userId) { await tx.questionsAnswer.update({
data: {
upvotes: {
increment: -1,
},
},
where: {
id: answerId,
},
});
return createdVote
}
if (vote!.userId !== 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.',
}); });
} }
const incrementValue = vote === Vote.UPVOTE ? 2 : -2; if (vote!.vote === Vote.DOWNVOTE) {
return vote;
}
const [questionsAnswerVote] = await ctx.prisma.$transaction([ if (vote.vote === Vote.UPVOTE) {
ctx.prisma.questionsAnswerVote.update({ tx.questionsAnswerVote.delete({
data: {
vote,
},
where: { where: {
id, id: vote.id,
}, },
}), });
ctx.prisma.questionsAnswer.update({
const createdVote = await tx.questionsAnswerVote.create({
data: {
answerId,
userId,
vote: Vote.DOWNVOTE,
},
});
await tx.questionsAnswer.update({
data: { data: {
upvotes: { upvotes: {
increment: incrementValue, increment: -2,
}, },
}, },
where: { where: {
id: voteToUpdate.answerId, id: answerId,
}, },
}), });
]);
return questionsAnswerVote; 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) => {
const voteToDelete = await tx.questionsAnswerVote.findUnique({
where: { where: {
id: input.id, answerId_userId: { answerId, userId },
}, },
}); })
if (voteToDelete === null) {
return null;
}
if (voteToDelete?.userId !== userId) { if (voteToDelete!.userId !== 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.',
}); });
} }
const incrementValue = voteToDelete.vote === Vote.UPVOTE ? -1 : 1; const incrementValue = voteToDelete!.vote === Vote.UPVOTE ? -1 : 1;
const [questionsAnswerVote] = await ctx.prisma.$transaction([ tx.questionsAnswerVote.delete({
ctx.prisma.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; return await ctx.prisma.$transaction(async (tx) => {
const vote = await tx.questionsQuestionCommentVote.findUnique({
where: {
questionCommentId_userId: { questionCommentId, userId },
},
})
const [questionCommentVote] = await ctx.prisma.$transaction([ if (vote === null) {
ctx.prisma.questionsQuestionCommentVote.create({ const createdVote = await tx.questionsQuestionCommentVote.create({
data: { data: {
questionCommentId, questionCommentId,
userId, userId,
vote, vote: Vote.UPVOTE,
}, },
}), });
ctx.prisma.questionsQuestionComment.update({
await tx.questionsQuestionComment.update({
data: { data: {
upvotes: { upvotes: {
increment: incrementValue, increment: 1,
}, },
}, },
where: { where: {
id: questionCommentId, id: questionCommentId,
}, },
}), });
]);
return questionCommentVote; 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 (vote === null) {
const createdVote = await tx.questionsQuestionCommentVote.create({
data: {
questionCommentId,
userId,
vote: Vote.DOWNVOTE,
}, },
}); });
if (voteToUpdate?.userId !== userId) { await tx.questionsQuestionComment.update({
data: {
upvotes: {
increment: -1,
},
},
where: {
id: questionCommentId,
},
});
return createdVote
}
if (vote!.userId !== 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.',
}); });
} }
const incrementValue = vote === Vote.UPVOTE ? 2 : -2; if (vote!.vote === Vote.DOWNVOTE) {
return vote;
}
const [questionCommentVote] = await ctx.prisma.$transaction([ if (vote.vote === Vote.UPVOTE) {
ctx.prisma.questionsQuestionCommentVote.update({ tx.questionsQuestionCommentVote.delete({
data: {
vote,
},
where: { where: {
id, id: vote.id,
}, },
}), });
ctx.prisma.questionsQuestionComment.update({
const createdVote = await tx.questionsQuestionCommentVote.create({
data: {
questionCommentId,
userId,
vote: Vote.DOWNVOTE,
},
});
await tx.questionsQuestionComment.update({
data: { data: {
upvotes: { upvotes: {
increment: incrementValue, increment: -2,
}, },
}, },
where: { where: {
id: voteToUpdate.questionCommentId, 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 === null) {
return null;
}
if (voteToDelete?.userId !== userId) { if (voteToDelete!.userId !== 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.',
}); });
} }
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; return await ctx.prisma.$transaction(async (tx) => {
const vote = await tx.questionsQuestionVote.findUnique({
where: {
questionId_userId: { questionId, userId },
},
})
const [questionVote] = await ctx.prisma.$transaction([ if (vote === null) {
ctx.prisma.questionsQuestionVote.create({ const createdVote = await tx.questionsQuestionVote.create({
data: { data: {
questionId, questionId,
userId, userId,
vote, vote: Vote.UPVOTE,
}, },
}), });
ctx.prisma.questionsQuestion.update({
await tx.questionsQuestion.update({
data: { data: {
upvotes: { upvotes: {
increment: incrementValue, increment: 1,
}, },
}, },
where: { where: {
id: questionId, id: questionId,
}, },
}), });
]);
return questionVote; 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) => {
const vote = await tx.questionsQuestionVote.findUnique({
where: { where: {
id: input.id, questionId_userId: { questionId, userId },
},
})
if (vote === null) {
const createdVote = await tx.questionsQuestionVote.create({
data: {
questionId,
userId,
vote: Vote.DOWNVOTE,
}, },
}); });
if (voteToUpdate?.userId !== userId) { await tx.questionsQuestion.update({
data: {
upvotes: {
increment: -1,
},
},
where: {
id: questionId,
},
});
return createdVote
}
if (vote!.userId !== 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.',
}); });
} }
const incrementValue = vote === Vote.UPVOTE ? 2 : -2; if (vote!.vote === Vote.DOWNVOTE) {
return vote;
}
const [questionVote] = await ctx.prisma.$transaction([ if (vote.vote === Vote.UPVOTE) {
ctx.prisma.questionsQuestionVote.update({ tx.questionsQuestionVote.delete({
data: {
vote,
},
where: { where: {
id, id: vote.id,
}, },
}), });
ctx.prisma.questionsQuestion.update({
const createdVote = await tx.questionsQuestionVote.create({
data: {
questionId,
userId,
vote: Vote.DOWNVOTE,
},
});
await tx.questionsQuestion.update({
data: { data: {
upvotes: { upvotes: {
increment: incrementValue, increment: -2,
}, },
}, },
where: { where: {
id: voteToUpdate.questionId, 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) => {
const voteToDelete = await tx.questionsQuestionVote.findUnique({
where: { where: {
id: input.id, questionId_userId: { questionId, userId },
}, },
}); })
if (voteToDelete === null) {
return null;
}
if (voteToDelete?.userId !== userId) { if (voteToDelete!.userId !== 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.',
}); });
} }
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