Skip to content

Commit

Permalink
Implement country leaderboard endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Alputer committed Jan 27, 2024
1 parent 71c7af4 commit 90969e8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
36 changes: 24 additions & 12 deletions src/controllers/leaderboard.controller.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import {
Controller,
Get,
Req,
UseGuards,
} from '@nestjs/common';
import { ApiBearerAuth, ApiQuery, ApiResponse, ApiTags } from '@nestjs/swagger';
import { Controller, Get, Param, Req, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiResponse, ApiTags } from '@nestjs/swagger';
import { AuthGuard } from '../services/guards';
import { IAuthorizedRequest } from '../interfaces';
import { LeaderboardService } from '../services';
Expand All @@ -29,13 +24,30 @@ export class LeaderboardController {
description: 'Internal server error, contact with backend team.',
})
@Get('/global')
public async getGlobalLeaderboard(
@Req() req: IAuthorizedRequest,
): Promise<number> {
const user = req.user;
public async getGlobalLeaderboard(): Promise<any> {
return await this.leaderboardService.getGlobalLeaderboard();
}

@UseGuards(AuthGuard)
@ApiResponse({
status: 200,
description: 'Country leaderboard is returned successfully.',
})
@ApiResponse({
status: 401,
description: 'You are not authenticated.',
})
@ApiResponse({
status: 500,
description: 'Internal server error, contact with backend team.',
})
@Get('/country/:countryCode')
public async getCountryLeaderboard(
@Param('countryCode') countryCode: string,
): Promise<any> {
return await this.leaderboardService.getCountryLeaderboard(countryCode);
}

@UseGuards(AuthGuard)
@ApiResponse({
status: 200,
Expand All @@ -52,7 +64,7 @@ export class LeaderboardController {
@Get('/tournament')
public async getTournamentLeaderboard(
@Req() req: IAuthorizedRequest,
): Promise<number> {
): Promise<any> {
const user = req.user;
return await this.leaderboardService.getTournamentLeaderboard(
user.username,
Expand Down
23 changes: 23 additions & 0 deletions src/repositories/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,29 @@ export class UserRepository {
}
}

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

try {
const response = await this.client.send(queryCommand);
return response.Items;
} catch (error) {
console.error('Error in get global leaderboard request:', error);
throw new InternalServerErrorException(
'Internal Server Error in getGlobalLeaderboard query',
);
}
}

public async completeLevel(user: User): Promise<void> {
const updateCommand = new UpdateItemCommand({
TableName: this.tableName,
Expand Down
6 changes: 6 additions & 0 deletions src/services/leaderboard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ export class LeaderboardService {
return users;
}

public async getCountryLeaderboard(countryCode: string) {
const users = await this.userRepository.getCountryLeaderboard(countryCode);

return users;
}

public async getTournamentLeaderboard(username: string) {
const user = await this.userRepository.findUserByUsername(username);

Expand Down

0 comments on commit 90969e8

Please sign in to comment.