Filtrage de géolocalisation par utilisateur⋅rice

This commit is contained in:
Emmy D'Anello 2024-12-07 17:22:15 +01:00
parent 138ff1df65
commit 7750c1b75c
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
3 changed files with 19 additions and 4 deletions

View File

@ -0,0 +1,9 @@
import { Type } from "class-transformer";
import { IsNumber, IsOptional } from "class-validator";
export class UserFilterDto {
@IsOptional()
@IsNumber()
@Type(() => Number)
userId?: number
}

View File

@ -7,6 +7,7 @@ import { GeolocationEntity } from './entities/geolocation.entity'
import { ApiOkResponsePaginated, paginateOutput } from 'src/common/utils/pagination.utils' import { ApiOkResponsePaginated, paginateOutput } from 'src/common/utils/pagination.utils'
import { QueryPaginationDto } from 'src/common/dto/pagination-query.dto' import { QueryPaginationDto } from 'src/common/dto/pagination-query.dto'
import { PaginateOutputDto } from 'src/common/dto/pagination-output.dto' import { PaginateOutputDto } from 'src/common/dto/pagination-output.dto'
import { UserFilterDto } from 'src/common/dto/user_filter.dto'
@Controller('geolocations') @Controller('geolocations')
export class GeolocationsController { export class GeolocationsController {
@ -33,8 +34,8 @@ export class GeolocationsController {
@ApiUnauthorizedResponse({ description: "Non authentifié⋅e" }) @ApiUnauthorizedResponse({ description: "Non authentifié⋅e" })
@ApiForbiddenResponse({ description: "Permission refusée" }) @ApiForbiddenResponse({ description: "Permission refusée" })
@ApiNotFoundResponse({ description: "Objet non trouvé" }) @ApiNotFoundResponse({ description: "Objet non trouvé" })
async findAll(@Query() queryPagination?: QueryPaginationDto): Promise<PaginateOutputDto<GeolocationEntity>> { async findAll(@Query() queryPagination?: QueryPaginationDto, @Query() userFilter?: UserFilterDto): Promise<PaginateOutputDto<GeolocationEntity>> {
const [geolocations, total] = await this.geolocationsService.findAll(queryPagination) const [geolocations, total] = await this.geolocationsService.findAll(queryPagination, userFilter)
return paginateOutput<GeolocationEntity>(geolocations.map(geolocation => new GeolocationEntity(geolocation)), total, queryPagination) return paginateOutput<GeolocationEntity>(geolocations.map(geolocation => new GeolocationEntity(geolocation)), total, queryPagination)
} }

View File

@ -4,6 +4,7 @@ import { PrismaService } from 'src/prisma/prisma.service'
import { Geolocation, User } from '@prisma/client' import { Geolocation, User } from '@prisma/client'
import { QueryPaginationDto } from 'src/common/dto/pagination-query.dto' import { QueryPaginationDto } from 'src/common/dto/pagination-query.dto'
import { paginate } from 'src/common/utils/pagination.utils' import { paginate } from 'src/common/utils/pagination.utils'
import { UserFilterDto } from 'src/common/dto/user_filter.dto'
@Injectable() @Injectable()
export class GeolocationsService { export class GeolocationsService {
@ -14,12 +15,16 @@ export class GeolocationsService {
return await this.prisma.geolocation.create({ data: data }) return await this.prisma.geolocation.create({ data: data })
} }
async findAll(queryPagination?: QueryPaginationDto): Promise<[Geolocation[], number]> { async findAll(queryPagination?: QueryPaginationDto, userFilter?: UserFilterDto): Promise<[Geolocation[], number]> {
const filter = {
where: userFilter?.userId ? { userId: userFilter.userId } : {}
}
return [ return [
await this.prisma.geolocation.findMany({ await this.prisma.geolocation.findMany({
...filter,
...paginate(queryPagination), ...paginate(queryPagination),
}), }),
await this.prisma.geolocation.count(), await this.prisma.geolocation.count(filter),
] ]
} }