Skip to content

Commit

Permalink
feat: added ability to vote on questions and answers
Browse files Browse the repository at this point in the history
  • Loading branch information
uptownhr committed Aug 18, 2023
1 parent 49f18b3 commit 996d1a1
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions apps/backend/src/app/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Question> {
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<Answer> {
const vote = await this.lovDb.answerVote.create({
data: {
answer: {
connect: {
id,
},
},
},
include: {
answer: {
include: {
votes: true,
},
},
},
});

return {
...vote.answer,
voteCount: vote.answer.votes.length,
};
}
}

0 comments on commit 996d1a1

Please sign in to comment.