-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from boostcampwm-2024/be/feature/rank
[BE/feature] 상위 5명 조회 api 작성
- Loading branch information
Showing
13 changed files
with
260 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { applyDecorators } from '@nestjs/common'; | ||
import { ApiResponse } from '@nestjs/swagger'; | ||
import { TokenDecorator } from 'src/global/utils/tokenSwagger'; | ||
import { rank5SuccessResponseDto } from '../dto/top5rank.dto'; | ||
|
||
export function top5rankResponseDecorator() { | ||
return applyDecorators( | ||
TokenDecorator(), | ||
ApiResponse({ | ||
status: 200, | ||
description: '5명 조회 성공', | ||
type: rank5SuccessResponseDto | ||
}) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
|
||
export class rank5DataDto { | ||
@ApiProperty({ | ||
description: '닉네임', | ||
example: '홍길동' | ||
}) | ||
nickname: string; | ||
|
||
@ApiProperty({ | ||
description: '점수', | ||
example: '20000' | ||
}) | ||
score: number; | ||
} | ||
|
||
export class rank5SuccessResponseDto { | ||
@ApiProperty({ | ||
description: '응답 코드', | ||
example: 200 | ||
}) | ||
code: number; | ||
|
||
@ApiProperty({ | ||
description: '응답 메세지', | ||
example: '상위 5명을 조회했습니다.' | ||
}) | ||
message: string; | ||
|
||
@ApiProperty({ | ||
description: '응답 데이터', | ||
type: [rank5DataDto], | ||
example: [ | ||
{ nickname: '파이썬', score: 50000 }, | ||
{ nickname: '자바', score: 40000 }, | ||
{ nickname: '자바스크립트', score: 30000 }, | ||
{ nickname: '타입스크립트', score: 20000 }, | ||
{ nickname: 'C 언어', score: 10000 } | ||
] | ||
}) | ||
data: rank5DataDto; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { RankController } from './rank.controller'; | ||
|
||
describe('RankController', () => { | ||
let controller: RankController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [RankController] | ||
}).compile(); | ||
|
||
controller = module.get<RankController>(RankController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Controller, Get, UseGuards } from '@nestjs/common'; | ||
import { RankService } from './rank.service'; | ||
import { successhandler, successMessage } from 'src/global/successhandler'; | ||
import { JwtAuthGuard } from 'src/global/utils/jwtAuthGuard'; | ||
import { ApiOperation } from '@nestjs/swagger'; | ||
import { top5rankResponseDecorator } from './decorator/top5rank.decorator'; | ||
|
||
@UseGuards(JwtAuthGuard) | ||
@Controller('api/rank') | ||
export class RankController { | ||
constructor(private readonly rankService: RankService) {} | ||
|
||
@ApiOperation({ summary: '상위 랭킹 5명 반환 api' }) | ||
@top5rankResponseDecorator() | ||
@Get('top5') | ||
async top5rank() { | ||
const data = await this.rankService.getTopRankings(); | ||
return successhandler(successMessage.TOP5_RANK_GET_SUCCESS, data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { RankController } from './rank.controller'; | ||
import { RankService } from './rank.service'; | ||
import { DatabaseModule } from 'src/database/database.module'; | ||
import { JwtModule } from '@nestjs/jwt'; | ||
import { ConfigModule, ConfigService } from '@nestjs/config'; | ||
|
||
@Module({ | ||
imports: [ | ||
DatabaseModule, | ||
JwtModule.registerAsync({ | ||
imports: [ConfigModule], | ||
inject: [ConfigService], | ||
useFactory: async (configService: ConfigService) => ({ | ||
secret: configService.get<string>('JWT_SECRET'), | ||
signOptions: { expiresIn: '1h' } | ||
}) | ||
}) | ||
], | ||
controllers: [RankController], | ||
providers: [RankService] | ||
}) | ||
export class RankModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export const rankQueries = { | ||
moneyDataQuery: `SELECT | ||
m.nickname, | ||
m.total_cash + COALESCE(SUM(mc.quantity * cp.price), 0) AS total_asset | ||
FROM | ||
members m | ||
LEFT JOIN | ||
member_crops mc ON m.member_id = mc.member_id | ||
LEFT JOIN | ||
crop_prices cp ON mc.crop_id = cp.crop_id | ||
AND cp.time = (SELECT MAX(time) FROM crop_prices WHERE crop_id = mc.crop_id) | ||
GROUP BY | ||
m.member_id, m.total_cash | ||
ORDER BY | ||
total_asset DESC;` | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { RankService } from './rank.service'; | ||
|
||
describe('RankService', () => { | ||
let service: RankService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [RankService] | ||
}).compile(); | ||
|
||
service = module.get<RankService>(RankService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { RedisClientType } from 'redis'; | ||
import { DatabaseService } from 'src/database/database.service'; | ||
import { rankQueries } from './rank.queries'; | ||
import { Cron, CronExpression } from '@nestjs/schedule'; | ||
|
||
@Injectable() | ||
export class RankService { | ||
constructor( | ||
private readonly databaseService: DatabaseService, | ||
@Inject('REDIS_CLIENT') private readonly redisClient: RedisClientType | ||
) {} | ||
|
||
async onApplicationBootstrap() { | ||
await this.storeMoneyRanking(); | ||
} | ||
|
||
@Cron(CronExpression.EVERY_2ND_HOUR) | ||
async handleCron() { | ||
await this.storeMoneyRanking(); | ||
} | ||
|
||
async storeMoneyRanking() { | ||
await this.redisClient.del('ranking'); | ||
const membersMoney = await this.databaseService.query(rankQueries.moneyDataQuery); | ||
for (const memberMoney of membersMoney.rows) { | ||
await this.redisClient.zAdd('ranking', { | ||
score: memberMoney.total_asset, | ||
value: memberMoney.nickname | ||
}); | ||
} | ||
} | ||
|
||
async getTopRankings() { | ||
const members = await this.redisClient.zRangeWithScores('ranking', -5, -1); | ||
return members.reverse(); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.