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, 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' import { PaginateOutputDto } from 'src/common/dto/pagination-output.dto' import { PlayerFilterDto } from 'src/common/dto/player_filter.dto' @Controller('geolocations') 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() async create(@Req() request: AuthenticatedRequest, @Body() createGeolocationDto: CreateGeolocationDto): Promise { 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) async findAll(@Query() queryPagination?: QueryPaginationDto, @Query() playerFilter?: PlayerFilterDto): Promise> { const [geolocations, total] = await this.geolocationsService.findAll(queryPagination, playerFilter) return paginateOutput(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() async findOne(@Param('id', ParseIntPipe) id: number): Promise { const geolocation = await this.geolocationsService.findOne(id) if (!geolocation) throw new NotFoundException(`Géolocalisation inexistante avec l'identifiant ${id}`) 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() async findLastLocation(@Param('playerId', ParseIntPipe) playerId: number): Promise { const geolocation = await this.geolocationsService.findLastLocation(playerId) if (!geolocation) throw new NotFoundException(`Géolocalisation inexistante pour læ joueur⋅se ${playerId}`) 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: "La géolocalisation a bien été supprimée" }) async remove(@Param('id', ParseIntPipe) id: number): Promise { await this.geolocationsService.remove(+id) } }