Skip to content

Commit

Permalink
Merge pull request #678 from bounswe/be-626/add_vote_distributions_to…
Browse files Browse the repository at this point in the history
…_pollid

be626: Add vote distribution to poll/id endpoint
  • Loading branch information
BatuhanIlhan authored Dec 23, 2023
2 parents 51e8651 + 0bcd8e2 commit 0555195
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
6 changes: 5 additions & 1 deletion app/backend/src/comment/comment.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { GoogleGenerativeAIEmbeddings } from '@langchain/google-genai';
import { RankingService } from '../ranking/ranking.service';
import { Ranking } from '../ranking/entities/ranking.entity';
import { Vote } from '../vote/entities/vote.entity';
import { VoteService } from '../vote/vote.service';
import { OptionService } from '../option/option.service';

@Module({
imports: [
Expand All @@ -41,7 +43,9 @@ import { Vote } from '../vote/entities/vote.entity';
BadgeService,
Pinecone,
GoogleGenerativeAIEmbeddings,
RankingService
RankingService,
VoteService,
OptionService
],
})
export class CommentModule {}
6 changes: 5 additions & 1 deletion app/backend/src/like/like.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { GoogleGenerativeAIEmbeddings } from '@langchain/google-genai';
import { RankingService } from '../ranking/ranking.service';
import { Ranking } from '../ranking/entities/ranking.entity';
import { Vote } from '../vote/entities/vote.entity';
import { VoteService } from '../vote/vote.service';
import { OptionService } from '../option/option.service';

@Module({
imports: [
Expand All @@ -46,7 +48,9 @@ import { Vote } from '../vote/entities/vote.entity';
BadgeService,
Pinecone,
GoogleGenerativeAIEmbeddings,
RankingService
RankingService,
VoteService,
OptionService
],
})
export class LikeModule {}
4 changes: 4 additions & 0 deletions app/backend/src/poll/poll.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import { GoogleGenerativeAIEmbeddings } from '@langchain/google-genai';
import { RankingService } from '../ranking/ranking.service';
import { Ranking } from '../ranking/entities/ranking.entity';
import { Vote } from '../vote/entities/vote.entity';
import { VoteService } from '../vote/vote.service';
import { OptionService } from '../option/option.service';

@Module({
imports: [
Expand Down Expand Up @@ -57,6 +59,8 @@ import { Vote } from '../vote/entities/vote.entity';
Pinecone,
GoogleGenerativeAIEmbeddings,
RankingService,
VoteService,
OptionService
],
exports: [PollService],
})
Expand Down
23 changes: 14 additions & 9 deletions app/backend/src/poll/poll.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { GoogleGenerativeAIEmbeddings } from '@langchain/google-genai';
import { TaskType } from '@google/generative-ai';
import { RankingService } from '../ranking/ranking.service';
import { UpdateTagsDto } from './dto/update-tags.dto';
import { VoteService } from '../vote/vote.service';

@Injectable()
export class PollService {
Expand All @@ -43,6 +44,7 @@ export class PollService {
private readonly tagService: TagService,
private readonly pinecone: Pinecone,
private readonly rankingService: RankingService,
private readonly voteService: VoteService,
) {
this.embeddings = new GoogleGenerativeAIEmbeddings({
modelName: 'embedding-001', // 768 dimensions
Expand Down Expand Up @@ -358,9 +360,6 @@ export class PollService {
'options',
'tags',
'creator',
'votes',
'votes.user',
'votes.option',
'likes',
'likes.user',
'comments',
Expand All @@ -370,15 +369,21 @@ export class PollService {
if (!poll) {
throw new NotFoundException('Poll not found');
}
const votedOption = await this.voteService.findOne(pollId,userId);

let voteDistribution = null;
if(votedOption){
voteDistribution = await this.voteService.getVoteRate(pollId)
}

const pollCount = await this.voteService.getVoteCount(pollId);


return {
...poll,
votedOption:
poll.votes
.filter((vote) => vote.user && vote.user.id == userId)
.map((vote) => vote.option.id)[0] || null,
didLike: poll.likes.some((like) => like.user && like.user.id == userId),
voteCount: poll.votes.length,
votedOption: votedOption,
voteDistribution: voteDistribution,
voteCount: pollCount,
likeCount: poll.likes.length,
commentCount: poll.comments.length,
};
Expand Down
10 changes: 8 additions & 2 deletions app/backend/src/vote/vote.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,14 @@ export class VoteService {

}

findOne(id: number) {
return `This action returns a #${id} vote`;
async findOne(pollID: string,userID: string) {
return await this.voteReposityory.findOne({where:{poll:{id:pollID},user:{id:userID}}})
}

async getVoteCount(pollID: string) {
return await this.voteReposityory.createQueryBuilder("vote")
.where("vote.poll.id = :pollID", { pollID: pollID })
.getCount()
}


Expand Down

0 comments on commit 0555195

Please sign in to comment.