Filtrage de géolocalisation par utilisateur⋅rice
This commit is contained in:
parent
138ff1df65
commit
7750c1b75c
9
server/src/common/dto/user_filter.dto.ts
Normal file
9
server/src/common/dto/user_filter.dto.ts
Normal 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
|
||||
}
|
@ -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<PaginateOutputDto<GeolocationEntity>> {
|
||||
const [geolocations, total] = await this.geolocationsService.findAll(queryPagination)
|
||||
async findAll(@Query() queryPagination?: QueryPaginationDto, @Query() userFilter?: UserFilterDto): Promise<PaginateOutputDto<GeolocationEntity>> {
|
||||
const [geolocations, total] = await this.geolocationsService.findAll(queryPagination, userFilter)
|
||||
return paginateOutput<GeolocationEntity>(geolocations.map(geolocation => new GeolocationEntity(geolocation)), total, queryPagination)
|
||||
}
|
||||
|
||||
|
@ -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),
|
||||
]
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user