Skip to content

Commit

Permalink
fix(pagination) add pagination management
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-frage-ouest-france committed Jan 15, 2024
1 parent e17d09a commit 512c039
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public Page<T> search(Object criteria, Pageable pageable) {
Page<T> response = client.search(annotation.resource(), toMap(queryParams), headers, clazz);
if (response instanceof PageImpl<T> page) {
page.setPageable(pageable);
if (pageable.getPageNumber() + 1 < response.getTotalPages()) {
page.setNextPageable(Pageable.ofSize(pageable.getPageNumber() + 1, pageable.getPageSize(), pageable.getSort()));
}
}
// Retrieve result headers
return response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public <T> Page<T> search(String resource, MultiValueMap<String, String> params,
return Optional.ofNullable(response)
.map(HttpEntity::getBody)
.map(x -> {
PageImpl<T> page = new PageImpl<>(x, null, x.size(), 1);
PageImpl<T> page = new PageImpl<>(x, null, x.size(), 1, null);
List<String> contentRangeHeaders = response.getHeaders().get("Content-Range");
if (contentRangeHeaders != null && !contentRangeHeaders.isEmpty()) {
Range range = Range.of(contentRangeHeaders.stream().findFirst().toString());
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/fr/ouestfrance/querydsl/postgrest/model/Page.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public interface Page<T> extends Iterable<T> {
*/
@SafeVarargs
static <T> Page<T> of(T... items) {
return new PageImpl<>(Arrays.asList(items), Pageable.unPaged(), items.length, 1);
return new PageImpl<>(Arrays.asList(items), Pageable.unPaged(), items.length, 1, null);
}

/**
Expand All @@ -33,7 +33,7 @@ static <T> Page<T> of(T... items) {
* @return empty page
*/
static <T> Page<T> empty() {
return new PageImpl<>(List.of(), Pageable.unPaged(), 0, 0);
return new PageImpl<>(List.of(), Pageable.unPaged(), 0, 0, null);
}

/**
Expand All @@ -50,6 +50,13 @@ static <T> Page<T> empty() {
*/
Pageable getPageable();

/**
* Get next page request infomations
*
* @return Pageable information with page number, number of elements, number of the page and sort options
*/
Pageable getNextPageable();

/**
* Get size of page
*
Expand Down Expand Up @@ -95,6 +102,6 @@ default Iterator<T> iterator() {
* @return page converted
*/
default <U> Page<U> map(Function<T, U> converter) {
return new PageImpl<>(stream().map(converter).toList(), getPageable(), getTotalElements(), getTotalPages());
return new PageImpl<>(stream().map(converter).toList(), getPageable(), getTotalElements(), getTotalPages(), Pageable.ofSize(getPageable().getPageNumber() + 1, getPageable().getPageSize(), getPageable().getSort()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public class PageImpl<T> implements Page<T> {
*/
private int totalPages;

@Setter
private Pageable nextPageable;

/**
* Apply range to a specific Page
* @param range range to apply
Expand All @@ -42,4 +45,6 @@ public void withRange(Range range) {
totalPages = (int) (totalElements / (range.getLimit() - range.getOffset()));
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,19 @@ static Pageable ofSize(int pageSize) {
* @return pageable object
*/
static Pageable ofSize(int pageSize, Sort sort) {
return new PageRequest(0, pageSize, sort);
return ofSize(0, pageSize, sort);
}


/**
* Create a simple pageRequest with page number and size and declarative sort
* @param pageNumber number of the request
* @param pageSize number of element in one page
* @param sort sort information
* @return pageable object
*/
static Pageable ofSize(int pageNumber, int pageSize, Sort sort) {
return new PageRequest(pageNumber, pageSize, sort);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import fr.ouestfrance.querydsl.postgrest.app.*;
import fr.ouestfrance.querydsl.postgrest.model.Page;
import fr.ouestfrance.querydsl.postgrest.model.PageImpl;
import fr.ouestfrance.querydsl.postgrest.model.Pageable;
import fr.ouestfrance.querydsl.postgrest.model.Sort;
import fr.ouestfrance.querydsl.postgrest.model.exceptions.PostgrestRequestException;
Expand Down Expand Up @@ -81,6 +82,42 @@ void shouldSearchWithPaginate() {
MultiValueMap<String, String> value = headerArgs.getValue();
assertEquals("0-9", value.getFirst("Range"));
assertEquals("items", value.getFirst("Range-Unit"));
// Assert pagination
assertNotNull(search.getPageable());
assertEquals(10, search.getPageable().getPageSize());
assertEquals(0, search.getPageable().getPageNumber());
assertEquals(2, search.getTotalElements());
assertEquals(1, search.getTotalPages());
assertNull(search.getNextPageable());
}

@Test
void shouldSearchWithNextPaginate() {
PostRequest request = new PostRequest();
request.setUserId(1);
request.setId(1);
request.setTitle("Test*");
request.setCodes(List.of("a", "b", "c"));
request.setExcludes(List.of("z"));
request.setValidDate(LocalDate.of(2023, 11, 10));
ArgumentCaptor<MultiValueMap<String, String>> queryArgs = multiMapCaptor();
ArgumentCaptor<MultiValueMap<String, String>> headerArgs = multiMapCaptor();
when(webClient.search(anyString(), queryArgs.capture(), headerArgs.capture(), eq(Post.class))).thenReturn(new PageImpl<>(List.of(new Post()), null, 2, 2, null));

Page<Post> search = repository.search(request, Pageable.ofSize(1, Sort.by(Sort.Order.asc("id"))));
assertNotNull(search);
assertEquals(1, search.size());
// Assert pagination
assertNotNull(search.getPageable());
assertEquals(1, search.getPageable().getPageSize());
assertEquals(0, search.getPageable().getPageNumber());
assertEquals(2, search.getTotalElements());
assertEquals(2, search.getTotalPages());
// Assert next pagination
assertNotNull(search.getNextPageable());
assertEquals(1, search.getNextPageable().getPageSize());
assertEquals(1, search.getNextPageable().getPageNumber());
assertEquals(search.getPageable().getSort(), search.getNextPageable().getSort());
}

@Test
Expand Down

0 comments on commit 512c039

Please sign in to comment.