Skip to content

Commit

Permalink
Merge pull request #93 from boostcampwm-2024/be/fix/rank_error_fix
Browse files Browse the repository at this point in the history
[BE/fix] 랭킹 로직 수정
  • Loading branch information
HBLEEEEE authored Nov 25, 2024
2 parents d317deb + 0a55a45 commit 28bf1f9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
26 changes: 25 additions & 1 deletion apps/backend/src/rank/decorator/getRank.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,31 @@ export function getRankResponseDecorator() {
ApiResponse({
status: 200,
description: '랭킹 조회 성공',
type: GetRankSuccessResponseDto
type: GetRankSuccessResponseDto,
examples: {
success: {
summary: '랭킹이 있는 경우',
value: {
code: 200,
message: '현재 랭킹을 조회했습니다.',
data: {
rank: 5,
percentage: 50
}
}
},
noRank: {
summary: '랭킹이 갱신되지 않은 경우',
value: {
code: 200,
message: '현재 랭킹을 조회했습니다.',
data: {
rank: -1,
percentage: null
}
}
}
}
})
);
}
8 changes: 7 additions & 1 deletion apps/backend/src/rank/dto/getRank.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ import { ApiProperty } from '@nestjs/swagger';
export class GetRankDataDto {
@ApiProperty({
description: '순위',
example: '5'
example: 33
})
rank: number;

@ApiProperty({
description: '백분율',
example: 50
})
percentage: number | null;
}

export class GetRankSuccessResponseDto {
Expand Down
16 changes: 7 additions & 9 deletions apps/backend/src/rank/rank.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ export class RankService {
async storeMoneyRanking() {
await this.redisClient.del('ranking');
const membersMoney = await this.databaseService.query(rankQueries.moneyDataQuery);
const pipeline = this.redisClient.multi();
for (const memberMoney of membersMoney.rows) {
await this.redisClient.zAdd('ranking', {
pipeline.zAdd('ranking', {
score: memberMoney.total_asset,
value: memberMoney.nickname
});
}
await pipeline.exec();
}

async getTopRankings() {
Expand All @@ -38,13 +40,9 @@ export class RankService {

async getRanking(nickname: string) {
const rank = await this.redisClient.zRevRank('ranking', nickname);
if (rank) {
return {
rank: rank + 1
};
}
return {
rank: '해당 유저의 랭킹이 존재 하지 않습니다.'
};
const totalmembers = await this.redisClient.zCard('ranking');
if (!rank) return { rank: -1, percentage: null };
const percentage = ((rank + 1) / totalmembers) * 100;
return { rank: rank + 1, percentage: percentage == 0 ? 1 : percentage.toFixed(0) };
}
}

0 comments on commit 28bf1f9

Please sign in to comment.