From 08f5b0789fff221a3bcb28033be1b49577b6bb3b Mon Sep 17 00:00:00 2001 From: Tetardtek Date: Tue, 28 Apr 2026 18:58:45 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20NPC=20controller=20=E2=80=94=20charger?= =?UTF-8?q?=20character=20depuis=20req.user=20(pas=20req.character)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/npc/npc.controller.ts | 27 ++++++++++++++++++++------- src/npc/npc.module.ts | 3 ++- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/npc/npc.controller.ts b/src/npc/npc.controller.ts index e5b477f..c1173c3 100644 --- a/src/npc/npc.controller.ts +++ b/src/npc/npc.controller.ts @@ -1,23 +1,36 @@ -import { Controller, Get, Query, Req, UseGuards } from '@nestjs/common'; +import { Controller, Get, Query, Req, UseGuards, BadRequestException } from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; +import { Repository } from 'typeorm'; import { NpcService } from './npc.service'; import { AuthGuard } from '../auth/guards/auth.guard'; +import { Character } from '../character/entities/character.entity'; @Controller('npcs') @UseGuards(AuthGuard) export class NpcController { - constructor(private readonly npcService: NpcService) {} + constructor( + private readonly npcService: NpcService, + @InjectRepository(Character) + private readonly characterRepo: Repository, + ) {} + + private async getCharacter(req: any) { + const character = await this.characterRepo.findOne({ where: { userId: req.user.id } }); + if (!character) throw new BadRequestException('Aucun personnage trouvé'); + return character; + } /** GET /api/npcs — tous les PNJ visibles pour le joueur */ @Get() async getAll(@Req() req: any) { - const { characterId, level } = req.character; - return this.npcService.getVisibleNpcs(characterId, level); + const char = await this.getCharacter(req); + return this.npcService.getVisibleNpcs(char.id, char.level); } - /** GET /api/npcs?location=village_plaza — PNJ d'un emplacement */ + /** GET /api/npcs/location?location=village_plaza — PNJ d'un emplacement */ @Get('location') async getByLocation(@Req() req: any, @Query('location') location: string) { - const { characterId, level } = req.character; - return this.npcService.getNpcsByLocation(characterId, level, location); + const char = await this.getCharacter(req); + return this.npcService.getNpcsByLocation(char.id, char.level, location); } } diff --git a/src/npc/npc.module.ts b/src/npc/npc.module.ts index aaaf583..0663f37 100644 --- a/src/npc/npc.module.ts +++ b/src/npc/npc.module.ts @@ -4,10 +4,11 @@ import { Npc } from './npc.entity'; import { NpcController } from './npc.controller'; import { NpcService } from './npc.service'; import { PlayerQuestArc } from '../quest/player-quest-arc.entity'; +import { Character } from '../character/entities/character.entity'; import { AuthModule } from '../auth/auth.module'; @Module({ - imports: [TypeOrmModule.forFeature([Npc, PlayerQuestArc]), AuthModule], + imports: [TypeOrmModule.forFeature([Npc, PlayerQuestArc, Character]), AuthModule], controllers: [NpcController], providers: [NpcService], exports: [NpcService],