Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified createGame to work with login #65

Merged
merged 2 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions api/src/modules/game/game.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,23 @@ import { User } from '@prisma/client';

import { JwtTwoFaAuthGuard } from '../auth';
import { GetUser } from '../auth/decorators';
import { UserService } from '../user';
import { UserLoginDto } from '../user/user.dto';
import { CreateGameDto } from './game.dto';
import { GameService } from './game.service';

@Controller('game')
@UseGuards(JwtTwoFaAuthGuard)
@ApiBearerAuth()
export class GameController {
constructor(
private gameService: GameService,
private userService: UserService,
) {}
constructor(private gameService: GameService) {}

@Post('/create')
async createGame(@Body() game: CreateGameDto) {
const winner = await this.userService.getUnique(game.winnerLogin);
const loser = await this.userService.getUnique(game.loserLogin);
this.gameService.createGame(winner, loser, game.winnerScore, game.loserScore);
this.gameService.createGame(
game.winnerLogin,
game.loserLogin,
game.winnerScore,
game.loserScore,
);
}

@Get('/won')
Expand Down
26 changes: 22 additions & 4 deletions api/src/modules/game/game.service.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
import { Injectable } from '@nestjs/common';
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
import { Game, User } from '@prisma/client';

import { PrismaService } from '@/prisma';

import { UserService } from '../user';

@Injectable()
export class GameService {
constructor(private prisma: PrismaService) {}
constructor(
private prisma: PrismaService,
private userService: UserService,
) {}

async createGame(
winner: User,
loser: User,
winnerLogin: string,
loserLogin: string,
scoreWinner: number,
scoreLoser: number,
): Promise<void> {
const winner = await this.userService.getUnique(winnerLogin);

if (!winner) throw new NotFoundException(`User ${winnerLogin} does not exists`);

const loser = await this.userService.getUnique(loserLogin);

if (!loser) throw new NotFoundException(`User ${loserLogin} does not exists`);

if (scoreWinner < 0 || scoreLoser < 0) throw new BadRequestException('Score must be positive');

if (winner.id === loser.id)
throw new BadRequestException('One player cannot play against himself.');

await this.prisma.game.create({
data: {
winnerScore: scoreWinner,
Expand Down