From 996d1a1206ca77de953bd5efa4486d8170331781 Mon Sep 17 00:00:00 2001 From: James Date: Thu, 17 Aug 2023 18:55:53 -0700 Subject: [PATCH] feat: added ability to vote on questions and answers --- apps/backend/src/app/app.controller.ts | 55 ++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/apps/backend/src/app/app.controller.ts b/apps/backend/src/app/app.controller.ts index 279b0b45..56ca389a 100644 --- a/apps/backend/src/app/app.controller.ts +++ b/apps/backend/src/app/app.controller.ts @@ -151,4 +151,59 @@ export class AppController { return answers.map((a) => ({ ...a, voteCount: a.votes.length })); } + + @ApiResponse({ type: Question, status: 200 }) + @Post('question/:id/vote') + async questionVote(@Param('id') id: number): Promise { + console.log('id', id); + const vote = await this.lovDb.questionVote.create({ + data: { + question: { + connect: { + id, + }, + }, + }, + include: { + question: { + include: { + votes: true, + answers: true, + }, + }, + }, + }); + + return { + ...vote.question, + voteCount: vote.question.votes.length, + answerCount: vote.question.answers.length, + }; + } + + @ApiResponse({ type: Answer, status: 200 }) + @Post('answer/:id/vote') + async answerVote(@Param('id') id: number): Promise { + const vote = await this.lovDb.answerVote.create({ + data: { + answer: { + connect: { + id, + }, + }, + }, + include: { + answer: { + include: { + votes: true, + }, + }, + }, + }); + + return { + ...vote.answer, + voteCount: vote.answer.votes.length, + }; + } }