import { Controller, Get, Post, Param, Req, UseGuards, BadRequestException } from '@nestjs/common'; import { ShopService } from './shop.service'; import { AuthGuard } from '../auth/guards/auth.guard'; import { Request } from 'express'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { Character } from '../character/entities/character.entity'; @Controller('shop') @UseGuards(AuthGuard) export class ShopController { constructor( private readonly shopService: ShopService, @InjectRepository(Character) private readonly characterRepo: Repository, ) {} @Get() async getCatalogue(@Req() req: Request) { const char = await this.getCharacter(req); return this.shopService.getCatalogue(char.id); } @Post('buy/:itemId') async buy(@Param('itemId') itemId: string, @Req() req: Request) { const char = await this.getCharacter(req); return this.shopService.buy(itemId, char.id); } @Post('sell/:charItemId') async sell(@Param('charItemId') charItemId: string, @Req() req: Request) { const char = await this.getCharacter(req); return this.shopService.sell(charItemId, char.id); } private async getCharacter(req: Request): Promise { const user = (req as any).user; const character = await this.characterRepo.findOne({ where: { userId: user.id } }); if (!character) throw new BadRequestException('Aucun personnage trouvé'); return character; } }