From 345332922cc0483ec4286a379e1999fc72e9ebd0 Mon Sep 17 00:00:00 2001 From: legonzaur Date: Fri, 5 Jul 2024 09:54:33 +0200 Subject: [PATCH] Chore : add cardPipe --- src/games/pipe/card.pipe.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/games/pipe/card.pipe.ts diff --git a/src/games/pipe/card.pipe.ts b/src/games/pipe/card.pipe.ts new file mode 100644 index 0000000..0c844f3 --- /dev/null +++ b/src/games/pipe/card.pipe.ts @@ -0,0 +1,22 @@ +import { + PipeTransform, + Injectable, + HttpException, + HttpStatus, +} from '@nestjs/common'; +import { InjectModel } from '@nestjs/mongoose'; +import { Model } from 'mongoose'; +import { Card } from 'src/cards/schemas/cards.schema'; + +@Injectable() +export class CardPipe implements PipeTransform { + constructor(@InjectModel(Card.name) private cardmodel: Model) {} + + async transform(value: any) { + const card = await this.cardmodel.findById(value); + if (!card) { + throw new HttpException('Card not found', HttpStatus.NOT_FOUND); + } + return card; + } +}