From faae6b0b9358292ebc6e56d8e2a26f7c7b396c79 Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Sat, 7 Dec 2024 17:50:52 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20endpoint=20derni=C3=A8re=20localisation?= =?UTF-8?q?=20utilisateur=E2=8B=85rice?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/geolocations/geolocations.controller.ts | 16 +++++++++++++++- server/src/geolocations/geolocations.service.ts | 8 ++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/server/src/geolocations/geolocations.controller.ts b/server/src/geolocations/geolocations.controller.ts index 0b6dfcf..2eefed2 100644 --- a/server/src/geolocations/geolocations.controller.ts +++ b/server/src/geolocations/geolocations.controller.ts @@ -47,12 +47,26 @@ export class GeolocationsController { @ApiForbiddenResponse({ description: "Permission refusée" }) @ApiNotFoundResponse({ description: "Objet non trouvé" }) async findOne(@Param('id', ParseIntPipe) id: number): Promise { - const geolocation = await this.geolocationsService.findOne(+id) + const geolocation = await this.geolocationsService.findOne(id) if (!geolocation) throw new NotFoundException(`Géolocalisation inexistante avec l'identifiant ${id}`) return new GeolocationEntity(geolocation) } + @Get('/last-location/:userId') + @UseGuards(JwtAuthGuard) + @ApiBearerAuth() + @ApiOkResponse({ type: GeolocationEntity }) + @ApiUnauthorizedResponse({ description: "Non authentifié⋅e" }) + @ApiForbiddenResponse({ description: "Permission refusée" }) + @ApiNotFoundResponse({ description: "Aucune localisation trouvée" }) + async findLastLocation(@Param('userId', ParseIntPipe) userId: number): Promise { + const geolocation = await this.geolocationsService.findLastLocation(userId) + if (!geolocation) + throw new NotFoundException(`Géolocalisation inexistante pour l'utilisateur⋅rice ${userId}`) + return new GeolocationEntity(geolocation) + } + @Delete(':id') @HttpCode(204) @UseGuards(JwtAuthGuard) diff --git a/server/src/geolocations/geolocations.service.ts b/server/src/geolocations/geolocations.service.ts index ee9b7c4..9757a95 100644 --- a/server/src/geolocations/geolocations.service.ts +++ b/server/src/geolocations/geolocations.service.ts @@ -32,6 +32,14 @@ export class GeolocationsService { return await this.prisma.geolocation.findUnique({ where: { id } }) } + async findLastLocation(userId: number): Promise { + return await this.prisma.geolocation.findFirst({ + where: { userId: userId }, + orderBy: { timestamp: "desc" }, + take: 1 + }) + } + async remove(id: number): Promise { return await this.prisma.geolocation.delete({ where: { id } })