Skip to content

Commit

Permalink
Refactor leaderboard responses
Browse files Browse the repository at this point in the history
  • Loading branch information
Alputer committed Jan 27, 2024
1 parent 92c7c80 commit 3d30874
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/controllers/leaderboard.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ApiBearerAuth, ApiResponse, ApiTags } from '@nestjs/swagger';
import { AuthGuard } from '../services/guards';
import { IAuthorizedRequest } from '../interfaces';
import { LeaderboardService } from '../services';
import { GlobalLeaderboardResponseDto } from '../dtos/leaderboard/responses';

@ApiBearerAuth()
@Controller('/api/leaderboard')
Expand All @@ -24,7 +25,7 @@ export class LeaderboardController {
description: 'Internal server error, contact with backend team.',
})
@Get('/global')
public async getGlobalLeaderboard(): Promise<any> {
public async getGlobalLeaderboard(): Promise<GlobalLeaderboardResponseDto[]> {
return await this.leaderboardService.getGlobalLeaderboard();
}

Expand Down
11 changes: 11 additions & 0 deletions src/dtos/leaderboard/responses/country-leaderboard-response.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ApiProperty } from '@nestjs/swagger';
import { Expose } from 'class-transformer';

export class CountryLeaderboardResponseDto {
@ApiProperty()
@Expose()
username: string;
@ApiProperty()
@Expose()
levelNum: string;
}
11 changes: 11 additions & 0 deletions src/dtos/leaderboard/responses/global-leaderboard-response.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ApiProperty } from '@nestjs/swagger';
import { Expose } from 'class-transformer';

export class GlobalLeaderboardResponseDto {
@ApiProperty()
@Expose()
username: string;
@ApiProperty()
@Expose()
levelNum: string;
}
2 changes: 2 additions & 0 deletions src/dtos/leaderboard/responses/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './global-leaderboard-response.dto';
export * from './country-leaderboard-response.dto';
17 changes: 13 additions & 4 deletions src/repositories/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import { ConfigService } from '@nestjs/config';
import { User } from '../entities';
import { TournamentGroupRepository } from '.';
import { GlobalLeaderboardResponseDto, CountryLeaderboardResponseDto } from '../dtos/leaderboard/responses';

@Injectable()
export class UserRepository {
Expand Down Expand Up @@ -141,21 +142,25 @@ export class UserRepository {
}
}

public async getGlobalLeaderboard(): Promise<any> {
public async getGlobalLeaderboard(): Promise<GlobalLeaderboardResponseDto[]> {
const queryCommand = new QueryCommand({
TableName: 'Users',
IndexName: 'LevelGSI',
KeyConditionExpression: 'dummyPartitionKey = :dpk',
ExpressionAttributeValues: {
':dpk': { S: '_' },
},
ProjectionExpression: 'username, levelNum',
Limit: 1000,
ScanIndexForward: false,
});

try {
const response = await this.client.send(queryCommand);
return response.Items;
return response.Items.map((item) => ({
levelNum: item.levelNum.N,
username: item.username.S,
}));
} catch (error) {
console.error('Error in get global leaderboard request:', error);
throw new InternalServerErrorException(
Expand All @@ -164,21 +169,25 @@ export class UserRepository {
}
}

public async getCountryLeaderboard(countryCode: string): Promise<any> {
public async getCountryLeaderboard(countryCode: string): Promise<CountryLeaderboardResponseDto[]> {
const queryCommand = new QueryCommand({
TableName: 'Users',
IndexName: 'CountryCodeGSI',
KeyConditionExpression: 'countryCode = :val',
ExpressionAttributeValues: {
':val': { S: countryCode },
},
ProjectionExpression: 'username, levelNum',
Limit: 1000,
ScanIndexForward: false,
});

try {
const response = await this.client.send(queryCommand);
return response.Items;
return response.Items.map((item) => ({
levelNum: item.levelNum.N,
username: item.username.S,
}));
} catch (error) {
console.error('Error in get global leaderboard request:', error);
throw new InternalServerErrorException(
Expand Down
3 changes: 2 additions & 1 deletion src/services/leaderboard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BadRequestException, Injectable } from '@nestjs/common';
import { TournamentGroupRepository, UserRepository } from '../repositories';
import { SortOption } from '../enums';
import { IUser } from '../interfaces';
import { GlobalLeaderboardResponseDto } from '../dtos/leaderboard/responses';

@Injectable()
export class LeaderboardService {
Expand All @@ -10,7 +11,7 @@ export class LeaderboardService {
public readonly userRepository: UserRepository,
) {}

public async getGlobalLeaderboard() {
public async getGlobalLeaderboard(): Promise<GlobalLeaderboardResponseDto[]>{
const users = await this.userRepository.getGlobalLeaderboard();

return users;
Expand Down

0 comments on commit 3d30874

Please sign in to comment.