User => Player

This commit is contained in:
2024-12-08 13:41:37 +01:00
parent c6da328023
commit 65576fc5b1
39 changed files with 193 additions and 240 deletions

View File

@ -9,8 +9,8 @@ export class GeolocationEntity implements Geolocation {
@ApiProperty({description: "Identifiant unique"})
id: number
@ApiProperty({description: "Utilisateur⋅rice ayant émis la géolocalisation"})
userId: number
@ApiProperty({description: "Joueur⋅se ayant émis la géolocalisation"})
playerId: number
@ApiProperty({description: "Longitude en degrés"})
longitude: number

View File

@ -7,7 +7,7 @@ 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 { UserFilterDto } from 'src/common/dto/user_filter.dto'
import { PlayerFilterDto } from 'src/common/dto/player_filter.dto'
@Controller('geolocations')
export class GeolocationsController {
@ -21,8 +21,7 @@ export class GeolocationsController {
@ApiUnauthorizedResponse({ description: "Non authentifié⋅e" })
@ApiForbiddenResponse({ description: "Permission refusée" })
async create(@Req() request: AuthenticatedRequest, @Body() createGeolocationDto: CreateGeolocationDto): Promise<GeolocationEntity> {
const user = request.user
const geolocation = await this.geolocationsService.create(user, createGeolocationDto)
const geolocation = await this.geolocationsService.create(request.user, createGeolocationDto)
return new GeolocationEntity(geolocation)
}
@ -32,8 +31,8 @@ export class GeolocationsController {
@ApiOkResponsePaginated(GeolocationEntity)
@ApiUnauthorizedResponse({ description: "Non authentifié⋅e" })
@ApiForbiddenResponse({ description: "Permission refusée" })
async findAll(@Query() queryPagination?: QueryPaginationDto, @Query() userFilter?: UserFilterDto): Promise<PaginateOutputDto<GeolocationEntity>> {
const [geolocations, total] = await this.geolocationsService.findAll(queryPagination, userFilter)
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)
}
@ -51,17 +50,17 @@ export class GeolocationsController {
return new GeolocationEntity(geolocation)
}
@Get('/last-location/:userId')
@Get('/last-location/:playerId')
@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<GeolocationEntity> {
const geolocation = await this.geolocationsService.findLastLocation(userId)
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'utilisateur⋅rice ${userId}`)
throw new NotFoundException(`Géolocalisation inexistante pour læ joueur⋅se ${playerId}`)
return new GeolocationEntity(geolocation)
}

View File

@ -1,23 +1,23 @@
import { Injectable } from '@nestjs/common'
import { CreateGeolocationDto } from './dto/create-geolocation.dto'
import { PrismaService } from 'src/prisma/prisma.service'
import { Geolocation, Prisma, User } from '@prisma/client'
import { Geolocation, Player, Prisma } from '@prisma/client'
import { QueryPaginationDto } from 'src/common/dto/pagination-query.dto'
import { paginate } from 'src/common/utils/pagination.utils'
import { UserFilterDto } from 'src/common/dto/user_filter.dto'
import { PlayerFilterDto } from 'src/common/dto/player_filter.dto'
@Injectable()
export class GeolocationsService {
constructor(private prisma: PrismaService) { }
async create(authenticatedUser: User, createGeolocationDto: CreateGeolocationDto): Promise<Geolocation> {
const data = { ...createGeolocationDto, userId: authenticatedUser.id }
async create(authenticatedPlayer: Player, createGeolocationDto: CreateGeolocationDto): Promise<Geolocation> {
const data = { ...createGeolocationDto, playerId: authenticatedPlayer.id }
return await this.prisma.geolocation.create({ data: data })
}
async findAll(queryPagination?: QueryPaginationDto, userFilter?: UserFilterDto): Promise<[Geolocation[], number]> {
async findAll(queryPagination?: QueryPaginationDto, playerFilter?: PlayerFilterDto): Promise<[Geolocation[], number]> {
const filter = {
where: (userFilter?.userId ? { userId: userFilter.userId } : {}),
where: (playerFilter?.playerId ? { playerId: playerFilter.playerId } : {}),
orderBy: { timestamp: Prisma.SortOrder.desc },
}
return [
@ -33,9 +33,9 @@ export class GeolocationsService {
return await this.prisma.geolocation.findUnique({ where: { id } })
}
async findLastLocation(userId: number): Promise<Geolocation> {
async findLastLocation(playerId: number): Promise<Geolocation> {
return await this.prisma.geolocation.findFirst({
where: { userId: userId },
where: { playerId: playerId },
orderBy: { timestamp: Prisma.SortOrder.desc },
take: 1
})