Template
47 lines
1.1 KiB
TypeScript
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;
|
|
}
|
|
}
|