Ajout de la pagination sur l'API
This commit is contained in:
@ -1,9 +1,12 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete, ParseIntPipe, UseGuards, HttpCode, Req, NotFoundException } from '@nestjs/common'
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete, ParseIntPipe, UseGuards, HttpCode, Req, NotFoundException, Query } from '@nestjs/common'
|
||||
import { GeolocationsService } from './geolocations.service'
|
||||
import { CreateGeolocationDto } from './dto/create-geolocation.dto'
|
||||
import { AuthenticatedRequest, JwtAuthGuard } from 'src/auth/jwt-auth.guard'
|
||||
import { ApiBearerAuth, ApiCreatedResponse, ApiForbiddenResponse, ApiNoContentResponse, ApiNotFoundResponse, ApiOkResponse, ApiUnauthorizedResponse } from '@nestjs/swagger'
|
||||
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'
|
||||
|
||||
@Controller('geolocations')
|
||||
export class GeolocationsController {
|
||||
@ -26,13 +29,13 @@ export class GeolocationsController {
|
||||
@Get()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
@ApiOkResponse({ type: GeolocationEntity, isArray: true })
|
||||
@ApiOkResponsePaginated(GeolocationEntity)
|
||||
@ApiUnauthorizedResponse({ description: "Non authentifié⋅e" })
|
||||
@ApiForbiddenResponse({ description: "Permission refusée" })
|
||||
@ApiNotFoundResponse({ description: "Objet non trouvé" })
|
||||
async findAll(): Promise<GeolocationEntity[]> {
|
||||
const geolocations = await this.geolocationsService.findAll()
|
||||
return geolocations.map(geolocation => new GeolocationEntity(geolocation))
|
||||
async findAll(@Query() queryPagination?: QueryPaginationDto): Promise<PaginateOutputDto<GeolocationEntity>> {
|
||||
const [geolocations, total] = await this.geolocationsService.findAll(queryPagination)
|
||||
return paginateOutput<GeolocationEntity>(geolocations.map(geolocation => new GeolocationEntity(geolocation)), total, queryPagination)
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
|
@ -2,6 +2,8 @@ import { Injectable } from '@nestjs/common'
|
||||
import { CreateGeolocationDto } from './dto/create-geolocation.dto'
|
||||
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'
|
||||
|
||||
@Injectable()
|
||||
export class GeolocationsService {
|
||||
@ -12,8 +14,13 @@ export class GeolocationsService {
|
||||
return await this.prisma.geolocation.create({ data: data })
|
||||
}
|
||||
|
||||
async findAll(): Promise<Geolocation[]> {
|
||||
return await this.prisma.geolocation.findMany()
|
||||
async findAll(queryPagination?: QueryPaginationDto): Promise<[Geolocation[], number]> {
|
||||
return [
|
||||
await this.prisma.geolocation.findMany({
|
||||
...paginate(queryPagination),
|
||||
}),
|
||||
await this.prisma.geolocation.count(),
|
||||
]
|
||||
}
|
||||
|
||||
async findOne(id: number): Promise<Geolocation> {
|
||||
|
Reference in New Issue
Block a user