compostage-dans-ma-ville / backend

0 stars 0 forks source link

Refactor API Pagination with decorator #16

Open Lyokolux opened 1 year ago

Lyokolux commented 1 year ago

Description

L'implémentation actuelle de la pagination s'utilise come suit:

@Get()
@ApiPaginatedResponse(GetSiteDto)
async findAll(
  @Req() req: Request,
  @Query('items', ItemsQueryPipe) items: number,
  @Query('page', PageQueryPipe) page: number,
): Promise<PaginatedData<Site>> {
  // ... actions 

  return createPaginationData<Site>({
    url: getEndpoint(req),
    items: sites,
    queryOptions: { items, page },
    totalItemCount
  })
}

As developer, it would be more maintainable to use the pagination as follow in the controllers:

@Get()
@ApiPaginatedEndpoint(...metadataForPagination)
async findAll(
  pagination: Pagination
): Promise<PaginatedData<Site>> {

  // ... actions

  return createPaginationData({
    data,
    totalItemCount
    ...pagination
  })
}

Thus the decorator provides the url and the queryOptions objects inside a pagination object.

DoD

At least one findAll controller use the pagination recommended.