diff --git a/server/src/common/dto/user_filter.dto.ts b/server/src/common/dto/user_filter.dto.ts new file mode 100644 index 0000000..8835647 --- /dev/null +++ b/server/src/common/dto/user_filter.dto.ts @@ -0,0 +1,9 @@ +import { Type } from "class-transformer"; +import { IsNumber, IsOptional } from "class-validator"; + +export class UserFilterDto { + @IsOptional() + @IsNumber() + @Type(() => Number) + userId?: number +} diff --git a/server/src/geolocations/geolocations.controller.ts b/server/src/geolocations/geolocations.controller.ts index 2b0c6e7..0b6dfcf 100644 --- a/server/src/geolocations/geolocations.controller.ts +++ b/server/src/geolocations/geolocations.controller.ts @@ -7,6 +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' @Controller('geolocations') export class GeolocationsController { @@ -33,8 +34,8 @@ export class GeolocationsController { @ApiUnauthorizedResponse({ description: "Non authentifié⋅e" }) @ApiForbiddenResponse({ description: "Permission refusée" }) @ApiNotFoundResponse({ description: "Objet non trouvé" }) - async findAll(@Query() queryPagination?: QueryPaginationDto): Promise> { - const [geolocations, total] = await this.geolocationsService.findAll(queryPagination) + async findAll(@Query() queryPagination?: QueryPaginationDto, @Query() userFilter?: UserFilterDto): Promise> { + const [geolocations, total] = await this.geolocationsService.findAll(queryPagination, userFilter) return paginateOutput(geolocations.map(geolocation => new GeolocationEntity(geolocation)), total, queryPagination) } diff --git a/server/src/geolocations/geolocations.service.ts b/server/src/geolocations/geolocations.service.ts index e8e2ac2..ee9b7c4 100644 --- a/server/src/geolocations/geolocations.service.ts +++ b/server/src/geolocations/geolocations.service.ts @@ -4,6 +4,7 @@ import { PrismaService } from 'src/prisma/prisma.service' import { Geolocation, User } 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' @Injectable() export class GeolocationsService { @@ -14,12 +15,16 @@ export class GeolocationsService { 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 [ await this.prisma.geolocation.findMany({ + ...filter, ...paginate(queryPagination), }), - await this.prisma.geolocation.count(), + await this.prisma.geolocation.count(filter), ] }