Correction de l'endpoint qui récupère les dernières géolocalisations
This commit is contained in:
parent
20b4f2e7e8
commit
a0fd6ca6ab
@ -42,6 +42,20 @@ export class GeolocationsController {
|
||||
return paginateOutput<GeolocationEntity>(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<GeolocationEntity[]> {
|
||||
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<GeolocationEntity> {
|
||||
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
|
||||
*
|
||||
|
@ -33,15 +33,21 @@ export class GeolocationsService {
|
||||
return await this.prisma.geolocation.findUnique({ where: { id } })
|
||||
}
|
||||
|
||||
async findLastLocation(playerId: number): Promise<Geolocation> {
|
||||
return await this.prisma.geolocation.findFirst({
|
||||
where: { playerId: playerId },
|
||||
orderBy: { timestamp: Prisma.SortOrder.desc },
|
||||
take: 1
|
||||
})
|
||||
async findLastLocations(): Promise<Geolocation[]> {
|
||||
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<Geolocation> {
|
||||
if (!this.findOne(id))
|
||||
throw new NotFoundException(`Aucune géolocalisation n'existe avec l'identifiant ${id}`)
|
||||
|
Loading…
x
Reference in New Issue
Block a user