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,8 +1,8 @@
import { Controller, Get, Post, Body, Patch, Param, Delete, ParseIntPipe, UseGuards, HttpCode, Req, NotFoundException, Query } from '@nestjs/common'
import { Controller, Get, Post, Body, Param, Delete, ParseIntPipe, UseGuards, HttpCode, Req, NotFoundException, Query } from '@nestjs/common'
import { GeolocationsService } from './geolocations.service'
import { CreateGeolocationDto } from './dto/create-geolocation.dto'
import { AuthenticatedRequest, JwtAuthGuard } from 'src/auth/jwt-auth.guard'
import { ApiBearerAuth, ApiCreatedResponse, ApiNoContentResponse, ApiNotFoundResponse, ApiOkResponse, ApiUnauthorizedResponse } from '@nestjs/swagger'
import { ApiBearerAuth, ApiNoContentResponse } from '@nestjs/swagger'
import { GeolocationEntity } from './entities/geolocation.entity'
import { ApiOkResponsePaginated, paginateOutput } from 'src/common/utils/pagination.utils'
import { QueryPaginationDto } from 'src/common/dto/pagination-query.dto'
@ -13,33 +13,44 @@ import { PlayerFilterDto } from 'src/common/dto/player_filter.dto'
export class GeolocationsController {
constructor(private readonly geolocationsService: GeolocationsService) {}
/**
* Ajout d'une géolocalisation pour læ joueur⋅se connecté⋅e
*
* @throws {400} Erreurs dans le formulaire de création
* @throws {401} Non authentifié⋅e
*/
@Post()
@HttpCode(201)
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@ApiCreatedResponse({ type: GeolocationEntity, description: "Objet créé avec succès" })
@ApiUnauthorizedResponse({ description: "Non authentifié⋅e" })
async create(@Req() request: AuthenticatedRequest, @Body() createGeolocationDto: CreateGeolocationDto): Promise<GeolocationEntity> {
const geolocation = await this.geolocationsService.create(request.user, createGeolocationDto)
return new GeolocationEntity(geolocation)
}
/**
* Recherche de géolocalisations
*
* @throws {401} Non authentifié⋅e
*/
@Get()
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@ApiOkResponsePaginated(GeolocationEntity)
@ApiUnauthorizedResponse({ description: "Non authentifié⋅e" })
async findAll(@Query() queryPagination?: QueryPaginationDto, @Query() playerFilter?: PlayerFilterDto): Promise<PaginateOutputDto<GeolocationEntity>> {
const [geolocations, total] = await this.geolocationsService.findAll(queryPagination, playerFilter)
return paginateOutput<GeolocationEntity>(geolocations.map(geolocation => new GeolocationEntity(geolocation)), total, queryPagination)
}
/**
* Recherche d'une géolocalisation par identifiant
*
* @throws {401} Non authentifié⋅e
* @throws {404} Géolocalisation non trouvée
*/
@Get(':id')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@ApiOkResponse({ type: GeolocationEntity })
@ApiUnauthorizedResponse({ description: "Non authentifié⋅e" })
@ApiNotFoundResponse({ description: "Objet non trouvé" })
async findOne(@Param('id', ParseIntPipe) id: number): Promise<GeolocationEntity> {
const geolocation = await this.geolocationsService.findOne(id)
if (!geolocation)
@ -47,12 +58,15 @@ export class GeolocationsController {
return new GeolocationEntity(geolocation)
}
/**
* Récupération de la dernière posititon
*
* @throws {401} Non authentifié⋅e
* @throws {404} Aucune localisation envoyée
*/
@Get('/last-location/:playerId')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@ApiOkResponse({ type: GeolocationEntity })
@ApiUnauthorizedResponse({ description: "Non authentifié⋅e" })
@ApiNotFoundResponse({ description: "Aucune localisation trouvée" })
async findLastLocation(@Param('playerId', ParseIntPipe) playerId: number): Promise<GeolocationEntity> {
const geolocation = await this.geolocationsService.findLastLocation(playerId)
if (!geolocation)
@ -60,13 +74,17 @@ export class GeolocationsController {
return new GeolocationEntity(geolocation)
}
/**
* Suppression d'une localisation
*
* @throws {401} Non authentifié⋅e
* @throws {404} Géolocalisation non trouvée
*/
@Delete(':id')
@HttpCode(204)
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
@ApiNoContentResponse({ description: "Objet supprimé avec succès" })
@ApiUnauthorizedResponse({ description: "Non authentifié⋅e" })
@ApiNotFoundResponse({ description: "Objet non trouvé" })
@ApiNoContentResponse({ description: "La géolocalisation a bien été supprimée" })
async remove(@Param('id', ParseIntPipe) id: number): Promise<void> {
await this.geolocationsService.remove(+id)
}

View File

@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common'
import { Injectable, NotFoundException } from '@nestjs/common'
import { CreateGeolocationDto } from './dto/create-geolocation.dto'
import { PrismaService } from 'src/prisma/prisma.service'
import { Geolocation, Player, Prisma } from '@prisma/client'
@ -43,6 +43,8 @@ export class GeolocationsService {
async remove(id: number): Promise<Geolocation> {
if (!this.findOne(id))
throw new NotFoundException(`Aucune géolocalisation n'existe avec l'identifiant ${id}`)
return await this.prisma.geolocation.delete({ where: { id } })
}
}