Template
Initial commit
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
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.findBy({id: 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user