Files
webapp-back/src/games/pipe/game.pipe.ts
T
2024-12-27 22:54:31 +01:00

47 lines
1.1 KiB
TypeScript

import {
PipeTransform,
Injectable,
HttpException,
HttpStatus,
} from '@nestjs/common';
import { Game } from '../schemas/game.schema';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
@Injectable()
export class GamePipe implements PipeTransform {
constructor(
@InjectRepository(Game) private gameRepository: Repository<Game>,
) {}
async transform(value: any) {
const game = await this.gameRepository.findBy({ id: value });
if (!game) {
throw new HttpException('Game not found', HttpStatus.NOT_FOUND);
}
return game;
}
}
@Injectable()
export class GameStartedPipe implements PipeTransform {
constructor(
@InjectRepository(Game) private gameRepository: Repository<Game>,
) {}
async transform(value: any) {
const game = await this.gameRepository.findOne(value);
if (!game) {
throw new HttpException('Game not found', HttpStatus.NOT_FOUND);
}
if (!game.started) {
throw new HttpException(
'The game must be started',
HttpStatus.BAD_REQUEST,
);
}
return game;
}
}