apoio-solidario / apoio-solidario-back-end

Back-end do Projeto de extensão 3 - Apoio Solidário
MIT License
0 stars 0 forks source link

Adicionar paginação #13

Closed Joseulisses065 closed 11 hours ago

Joseulisses065 commented 6 days ago

Adicionar paginação aos endpoints findAll na api

LucJosin commented 6 days ago

Minha implementação de pagination no header @Joseulisses065

 /**
   * Method to build an {@link HttpHeaders} with:
   * <ul>
   *     <li>X-Pagination-Page-Index</li>
   *     <li>X-Pagination-Page-Size</li>
   *     <li>X-Pagination-Page-Total</li>
   *     <li>X-Pagination-Item-Total</li>
   *     <li>Content-Language</li>
   * </ul>
   *
   * @param page     An {@link Page} of {@link UUID}. Cannot be null
   * @param language An {@link String} with language. Can be null
   * @return An {@link HttpHeaders} with all defined params
   * @since 1.0.0
   */
  public HttpHeaders getHeaders(Page<UUID> page, String language) {
      HttpHeaders headers = new HttpHeaders() {{
          // We add +1 because of 'one-indexed-parameters' is set to true
          add("X-Pagination-Page-Index", String.valueOf(page.getNumber() + 1));
          add("X-Pagination-Page-Size", String.valueOf(page.getNumberOfElements()));
          add("X-Pagination-Page-Total", String.valueOf(page.getTotalPages()));
          add("X-Pagination-Item-Total", String.valueOf(page.getTotalElements()));
      }};

      if (!isNullOrEmpty(language)) headers.add("Content-Language", language);

      return headers;
  }

https://github.com/HawAPI/HawAPI/blob/main/src/main/java/com/lucasjosino/hawapi/controllers/utils/ResponseUtils.java#L35-L62

O uso seria algo assim:

@GetMapping
public ResponseEntity<List<T>> findAll(Pageable pageable) {
    // Page<T> pagination = service.findAll(pageable)
    // List<T> res = pagination.getContent()
    HttpHeaders headers = responseUtils.getHeaders(pagination, null);

    return ResponseEntity.ok().headers(headers).body(res);
}
Joseulisses065 commented 11 hours ago

Descrição

Issues