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, ) {} 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, ) {} 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; } }