Skip to content

Commit

Permalink
ADD: leaderboard (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
automerge-pingpong[bot] committed Oct 23, 2023
2 parents 44a0340 + 9fee980 commit 91ef55d
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
2 changes: 2 additions & 0 deletions backend/code/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { MessagesModule } from './messages/messages.module';
import { EventEmitterModule } from '@nestjs/event-emitter';
import { Gateways } from './gateways/gateways.gateway';
import { GameModule } from './game/game.module';
import { LeaderBoardModule } from './leaderboard/leaderboard.module';

@Module({
imports: [
Expand All @@ -25,6 +26,7 @@ import { GameModule } from './game/game.module';
MessagesModule,
EventEmitterModule.forRoot(),
GameModule,
LeaderBoardModule,
],
controllers: [AppController],
providers: [PrismaService, Gateways],
Expand Down
8 changes: 8 additions & 0 deletions backend/code/src/leaderboard/dto/leaderboard.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ApiProperty } from '@nestjs/swagger';

export class LeaderboardResponseDto {
@ApiProperty({ example: '60f1a7b0e1b3c2a4e8b4a1a0' })
userId: string;
@ApiProperty({ example: '3' })
wins: number;
}
22 changes: 22 additions & 0 deletions backend/code/src/leaderboard/leaderboard.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Controller, Get, Query, UseGuards, HttpStatus } from '@nestjs/common';
import { AtGuard } from 'src/auth/guards/at.guard';
import { LeaderBoardService } from './leaderboard.service';
import { QueryOffsetDto } from 'src/friends/dto/query-ofsset-dto';
import { ApiResponse, ApiTags } from '@nestjs/swagger';
import { LeaderboardResponseDto } from './dto/leaderboard.dto';

@ApiTags('leaderboard')
@Controller('leaderboard')
export class LeaderBoardController {
constructor(private readonly leaderBordService: LeaderBoardService) {}

@Get()
@ApiResponse({
type: LeaderboardResponseDto,
status: HttpStatus.OK,
})
@UseGuards(AtGuard)
async getLeaderBoard(@Query() { offset, limit }: QueryOffsetDto) {
return this.leaderBordService.getLeaderBoard(offset, limit);
}
}
9 changes: 9 additions & 0 deletions backend/code/src/leaderboard/leaderboard.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { LeaderBoardController } from './leaderboard.controller';
import { LeaderBoardService } from './leaderboard.service';

@Module({
controllers: [LeaderBoardController],
providers: [LeaderBoardService],
})
export class LeaderBoardModule {}
24 changes: 24 additions & 0 deletions backend/code/src/leaderboard/leaderboard.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Injectable } from '@nestjs/common';
import { PrismaService } from 'src/prisma/prisma.service';

@Injectable()
export class LeaderBoardService {
constructor(private prisma: PrismaService) {}
async getLeaderBoard(offset: number, limit: number) {
const leaderboard = await this.prisma.match.groupBy({
skip: offset,
take: limit,
by: ['winner_id'],
_count: { id: true },
orderBy: {
_count: {
id: 'desc',
},
},
});
return leaderboard.map((user) => ({
userId: user.winner_id,
wins: user._count.id,
}));
}
}

0 comments on commit 91ef55d

Please sign in to comment.