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,7 +1,7 @@
import { Controller, Get, Post, Body, Patch, Param, Delete, ParseIntPipe, HttpCode, UseGuards, Req, Query, NotFoundException } from '@nestjs/common'
import { ChallengeActionsService } from './challenge-actions.service'
import { AuthenticatedRequest, JwtAuthGuard } from 'src/auth/jwt-auth.guard'
import { ApiBearerAuth, ApiConflictResponse, ApiCreatedResponse, ApiNotFoundResponse, ApiOkResponse, ApiUnauthorizedResponse } from '@nestjs/swagger'
import { ApiBearerAuth, ApiNoContentResponse } from '@nestjs/swagger'
import { ChallengeActionEntity } from './entities/challenge-action.entity'
import { CreateChallengeActionDto } from './dto/create-challenge-action.dto'
import { ApiOkResponsePaginated, paginateOutput } from 'src/common/utils/pagination.utils'
@ -15,33 +15,44 @@ import { EndChallengeActionDto } from './dto/end-challenge-action.dto'
export class ChallengeActionsController {
constructor(private readonly challengeActionsService: ChallengeActionsService) {}
/**
* Création d'une action de défi
*
* @throws {400} Erreurs dans le formulaire de création
* @throws {401} Non authentifié⋅e
*/
@Post()
@HttpCode(201)
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@ApiCreatedResponse({ type: ChallengeActionEntity, description: "Objet créé avec succès" })
@ApiUnauthorizedResponse({ description: "Non authentifié⋅e" })
async create(@Req() request: AuthenticatedRequest, @Body() createChallengeActionDto: CreateChallengeActionDto): Promise<ChallengeActionEntity> {
const challenge = await this.challengeActionsService.create(request.user, createChallengeActionDto)
return new ChallengeActionEntity(challenge)
}
/**
* Recherche d'actions de défi
*
* @throws {401} Non authentifié⋅e
*/
@Get()
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@ApiOkResponsePaginated(ChallengeActionEntity)
@ApiUnauthorizedResponse({ description: "Non authentifié⋅e" })
async findAll(@Query() queryPagination: QueryPaginationDto, @Query() filterChallengeActions: FilterChallengeActionsDto): Promise<PaginateOutputDto<ChallengeActionEntity>> {
const [challengeActions, total] = await this.challengeActionsService.findAll(queryPagination, filterChallengeActions)
return paginateOutput<ChallengeActionEntity>(challengeActions.map(challengeAction => new ChallengeActionEntity(challengeAction)), total, queryPagination)
}
/**
* Recherche d'une action de défi par identifiant
*
* @throws {401} Non authentifié⋅e
* @throws {404} Action de défi non trouvée
*/
@Get(':id')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@ApiOkResponse({ type: ChallengeActionEntity })
@ApiUnauthorizedResponse({ description: "Non authentifié⋅e" })
@ApiNotFoundResponse({ description: "Objet non trouvé" })
async findOne(@Param('id', ParseIntPipe) id: number): Promise<ChallengeActionEntity> {
const challenge = await this.challengeActionsService.findOne(id)
if (!challenge)
@ -49,34 +60,44 @@ export class ChallengeActionsController {
return new ChallengeActionEntity(challenge)
}
/**
* Modification d'une action de défi par identifiant
*
* @throws {400} Erreurs dans le formulaire de modification
* @throws {401} Non authentifié⋅e
* @throws {404} Action de défi non trouvée
*/
@Patch(':id')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@ApiOkResponse({ type: ChallengeActionEntity })
@ApiUnauthorizedResponse({ description: "Non authentifié⋅e" })
@ApiNotFoundResponse({ description: "Objet non trouvé" })
async update(@Param('id', ParseIntPipe) id: number, @Body() updateChallengeActionDto: UpdateChallengeActionDto) {
async update(@Param('id', ParseIntPipe) id: number, @Body() updateChallengeActionDto: UpdateChallengeActionDto): Promise<ChallengeActionEntity> {
return await this.challengeActionsService.update(id, updateChallengeActionDto)
}
/**
* Suppression d'une action de défi par identifiant
*
* @throws {401} Non authentifié⋅e
* @throws {404} Action de défi non trouvée
*/
@Delete(':id')
@HttpCode(204)
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@ApiOkResponse({ type: ChallengeActionEntity })
@ApiUnauthorizedResponse({ description: "Non authentifié⋅e" })
@ApiNotFoundResponse({ description: "Objet non trouvé" })
async remove(@Param('id', ParseIntPipe) id: number) {
@ApiNoContentResponse({ description: "Action de défi supprimée avec succès" })
async remove(@Param('id', ParseIntPipe) id: number): Promise<void> {
await this.challengeActionsService.remove(id)
}
/**
* Terminer l'action de défi en cours
*
* @throws {401} Non authentifié⋅e
* @throws {409} Aucun défi à terminer n'est en cours
*/
@Post('/end-current')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@ApiOkResponse({ type: ChallengeActionEntity })
@ApiUnauthorizedResponse({ description: "Non authentifié⋅e" })
@ApiNotFoundResponse({ description: "Objet non trouvé" })
@ApiConflictResponse({ description: "Aucun défi à terminer n'est en cours" })
async endCurrent(@Req() request: AuthenticatedRequest, @Body() { success }: EndChallengeActionDto): Promise<ChallengeActionEntity> {
const challengeAction = await this.challengeActionsService.endCurrentChallenge(request.user, success)
return new ChallengeActionEntity(challengeAction)

View File

@ -1,4 +1,4 @@
import { BadRequestException, Injectable, UnprocessableEntityException } from '@nestjs/common'
import { BadRequestException, Injectable, NotFoundException, UnprocessableEntityException } from '@nestjs/common'
import { CreateChallengeActionDto } from './dto/create-challenge-action.dto'
import { UpdateChallengeActionDto } from './dto/update-challenge-action.dto'
import { ChallengeAction, Player } from '@prisma/client'
@ -35,6 +35,8 @@ export class ChallengeActionsService {
}
async update(id: number, updateChallengeActionDto: UpdateChallengeActionDto): Promise<ChallengeAction> {
if (!this.findOne(id))
throw new NotFoundException(`Aucune action de défi trouvée avec l'identifiant ${id}`)
return await this.prisma.challengeAction.update({
where: { id },
data: updateChallengeActionDto,
@ -42,6 +44,8 @@ export class ChallengeActionsService {
}
async remove(id: number): Promise<ChallengeAction> {
if (!this.findOne(id))
throw new NotFoundException(`Aucune action de défi trouvée avec l'identifiant ${id}`)
return await this.prisma.challengeAction.delete({
where: { id },
})