From a0fd6ca6abab1c10fc225f415af980eab22a5450 Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Sun, 8 Dec 2024 23:19:56 +0100 Subject: [PATCH] =?UTF-8?q?Correction=20de=20l'endpoint=20qui=20r=C3=A9cup?= =?UTF-8?q?=C3=A8re=20les=20derni=C3=A8res=20g=C3=A9olocalisations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../geolocations/geolocations.controller.ts | 30 +++++++++---------- .../src/geolocations/geolocations.service.ts | 20 ++++++++----- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/server/src/geolocations/geolocations.controller.ts b/server/src/geolocations/geolocations.controller.ts index 381675e..8f8586b 100644 --- a/server/src/geolocations/geolocations.controller.ts +++ b/server/src/geolocations/geolocations.controller.ts @@ -42,6 +42,20 @@ export class GeolocationsController { return paginateOutput(geolocations.map(geolocation => new GeolocationEntity(geolocation)), total, queryPagination) } + /** + * Récupération des dernières posititons de tout le monde + * + * @throws {401} Non authentifié⋅e + * @throws {404} Aucune localisation envoyée + */ + @Get('/last-locations') + @UseGuards(JwtAuthGuard) + @ApiBearerAuth() + async findLastLocations(): Promise { + const lastGeolocations = await this.geolocationsService.findLastLocations() + return lastGeolocations.map(geolocation => new GeolocationEntity(geolocation)) + } + /** * Recherche d'une géolocalisation par identifiant * @@ -58,22 +72,6 @@ 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() - 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 * diff --git a/server/src/geolocations/geolocations.service.ts b/server/src/geolocations/geolocations.service.ts index 3cde2d4..d402162 100644 --- a/server/src/geolocations/geolocations.service.ts +++ b/server/src/geolocations/geolocations.service.ts @@ -33,15 +33,21 @@ export class GeolocationsService { return await this.prisma.geolocation.findUnique({ where: { id } }) } - async findLastLocation(playerId: number): Promise { - return await this.prisma.geolocation.findFirst({ - where: { playerId: playerId }, - orderBy: { timestamp: Prisma.SortOrder.desc }, - take: 1 - }) + async findLastLocations(): Promise { + const players = await this.prisma.player.findMany() + const lastGeolocations = [] + for (const player of players) { + const lastGeolocationPlayer = await this.prisma.geolocation.findFirst({ + where: { playerId: player.id }, + orderBy: { timestamp: 'desc' }, + take: 1, + }) + if (lastGeolocationPlayer) + lastGeolocations.push(lastGeolocationPlayer) + } + return lastGeolocations } - async remove(id: number): Promise { if (!this.findOne(id)) throw new NotFoundException(`Aucune géolocalisation n'existe avec l'identifiant ${id}`)