Utilisation du plugin swagger pour de la meilleure documentation, et meilleure prise en charge d'erreurs

This commit is contained in:
2024-12-08 18:18:11 +01:00
parent 83d3a573ca
commit 77b33144f6
15 changed files with 249 additions and 107 deletions

View File

@ -1,6 +1,6 @@
import { Body, Controller, Get, HttpCode, NotFoundException, Param, ParseIntPipe, Patch, Query, Req, UseGuards } from '@nestjs/common'
import { PlayersService } from './players.service'
import { ApiBadRequestResponse, ApiBearerAuth, ApiNoContentResponse, ApiNotFoundResponse, ApiOkResponse, ApiUnauthorizedResponse } from '@nestjs/swagger'
import { ApiBearerAuth, ApiNoContentResponse } from '@nestjs/swagger'
import { PlayerEntity } from './entities/player.entity'
import { AuthenticatedRequest, JwtAuthGuard } from 'src/auth/jwt-auth.guard'
import { UpdatePasswordDto } from './dto/player_password.dto'
@ -12,37 +12,48 @@ import { PaginateOutputDto } from 'src/common/dto/pagination-output.dto'
export class PlayersController {
constructor(private readonly playersService: PlayersService) {}
/**
* Récupération de toustes les joueur⋅ses
*
* @throws {401} Non authentifié⋅e
*/
@Get()
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@ApiOkResponsePaginated(PlayerEntity)
@ApiUnauthorizedResponse({ description: "Non authentifié⋅e" })
async findAll(@Query() queryPagination?: QueryPaginationDto): Promise<PaginateOutputDto<PlayerEntity>> {
const [players, total] = await this.playersService.findAll(queryPagination)
return paginateOutput<PlayerEntity>(players.map(player => new PlayerEntity(player)), total, queryPagination)
}
/**
* Récupération d'un⋅e joueur⋅se par son identifiant
*
* @throws {401} Non authentifié⋅e
* @throws {404} Joueur⋅se non trouvé⋅e
*/
@Get(':id')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@ApiOkResponse({ type: PlayerEntity })
@ApiUnauthorizedResponse({ description: "Non authentifié⋅e" })
@ApiNotFoundResponse({ description: "Joueur⋅se non trouvé⋅e" })
async findOne(@Param('id', ParseIntPipe) id: number) {
async findOne(@Param('id', ParseIntPipe) id: number): Promise<PlayerEntity> {
const player = await this.playersService.findOne(id)
if (!player)
throw new NotFoundException(`Læ joueur⋅se avec l'identifiant ${id} n'existe pas`)
return new PlayerEntity(player)
}
/**
* Modification du mot de passe
*
* @throws {400} Mot de passe invalide
* @throws {401} Non authentifié⋅e
*/
@Patch('/update-password')
@HttpCode(204)
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@ApiNoContentResponse({description: "Le mot de passe a bien été modifié."})
@ApiBadRequestResponse({description: "Erreur dans la saisie du nouveau mot de passe."})
@ApiUnauthorizedResponse({ description: "Non authentifié⋅e" })
async updatePassword(@Req() request: AuthenticatedRequest, @Body() body: UpdatePasswordDto) {
@ApiNoContentResponse({ description: "Le mot de passe a bien été modifié." })
async updatePassword(@Req() request: AuthenticatedRequest, @Body() body: UpdatePasswordDto): Promise<void> {
await this.playersService.updatePassword(request.user, body)
}
}