Skip to content

Pagination bug fix: Previous page request will now be created properly. #275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions org.ektorp/src/main/java/org/ektorp/impl/PageResponseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,33 @@ public Page<T> success(HttpResponse hr) throws Exception {

int rowsSize = rows.size();
LOG.debug("got {} rows", rowsSize);
String nextId = null;
JsonNode nextKey = null;
String firstId = null;
JsonNode firstKey = null;

if (pageRequest.isBack()) {
Collections.reverse(rows);
//since rows are now reversed
nextId = parser.getFirstId();
nextKey = parser.getFirstKey();
firstId = parser.getLastId();
firstKey = parser.getLastKey();
}else {
nextId = parser.getLastId();
nextKey = parser.getLastKey();
firstId = parser.getFirstId();
firstKey = parser.getFirstKey();
}
int offset = pageRequest.isBack() ? 1 : 1;

String nextId = parser.getLastId();
JsonNode nextKey = parser.getLastKey();


PageRequest.Builder b = pageRequest.nextRequest(nextKey, nextId);
int currentPage = b.getPageNo();
PageRequest.Builder nextRequestBuilder = pageRequest.nextRequest(nextKey, nextId).back(false);
PageRequest.Builder prevRequestBuilder = pageRequest.nextRequest(firstKey, firstId).back(true);
int currentPage = pageRequest.getPageNo();

PageRequest nextRequest = b.page(currentPage + 1).build();
PageRequest nextRequest = nextRequestBuilder.page(currentPage + 1).build();
PageRequest previousRequest = currentPage == 1 ? PageRequest.firstPage(pageRequest.getPageSize()) :
b.back(true).page(currentPage - 1).build();
prevRequestBuilder.page(currentPage - 1).build();

boolean hasMore = rowsSize == pageRequest.getPageSize() + offset;
if (hasMore) {
Expand Down