Skip to content

Commit

Permalink
Merge pull request #873 from alphagov/pp_6768_next_link
Browse files Browse the repository at this point in the history
PP-6768 Optimise next_link generation
  • Loading branch information
kbottla authored Aug 5, 2020
2 parents ec4c424 + cf36b81 commit 22deb19
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ public List<TransactionEntity> searchTransactionAfter(TransactionSearchParams se
private TransactionSearchResponse buildTransactionSearchResponse(TransactionSearchParams searchParams, UriInfo uriInfo, List<Transaction> transactionList, Long totalCount) {
Long total = Optional.ofNullable(totalCount).orElse(0L);
PaginationBuilder paginationBuilder = new PaginationBuilder(searchParams, uriInfo);
paginationBuilder = paginationBuilder.withTotalCount(total).buildResponse();
paginationBuilder = paginationBuilder
.withTotalCount(total)
.withCount((long) transactionList.size())
.buildResponse();

List<TransactionView> transactionViewList = mapToTransactionViewList(transactionList, searchParams.getStatusVersion());

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

import javax.ws.rs.core.UriInfo;
import java.net.URI;
import java.util.Objects;

import static com.fasterxml.jackson.annotation.JsonInclude.Include;

Expand All @@ -24,6 +25,8 @@ public class PaginationBuilder {
@JsonIgnore
private Long totalCount;
@JsonIgnore
private Long count;
@JsonIgnore
private Long selfPageNum;
@JsonProperty(SELF_LINK)
private PaginationLink selfLink;
Expand All @@ -47,6 +50,11 @@ public PaginationBuilder withTotalCount(Long total) {
return this;
}

public PaginationBuilder withCount(Long count) {
this.count = count;
return this;
}

public PaginationBuilder buildResponse() {

if (searchParams.limitTotal()) {
Expand Down Expand Up @@ -84,7 +92,9 @@ private void buildLinksForLimitTotal() {
selfLink = PaginationLink.ofValue(uriWithParams(searchParams.buildQueryParamString(searchParams.getPageNumber())));
firstLink = PaginationLink.ofValue(uriWithParams(searchParams.buildQueryParamString(1L)));

nextLink = PaginationLink.ofValue(uriWithParams(searchParams.buildQueryParamString(selfPageNum + 1)));
if (Objects.equals(count, searchParams.getDisplaySize())) {
nextLink = PaginationLink.ofValue(uriWithParams(searchParams.buildQueryParamString(selfPageNum + 1)));
}

if (selfPageNum == 1L) {
prevLink = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ public void shouldBuildLinksCorrectly_whenLimitTotalParamIsSetAndFirstPageIsAcce
transactionSearchParams.setDisplaySize(10L);
transactionSearchParams.setLimitTotal(true);
PaginationBuilder builder = new PaginationBuilder(transactionSearchParams, mockedUriInfo)
.withTotalCount(120L);
.withTotalCount(120L)
.withCount(10L);
builder = builder.buildResponse();
assertThat(builder.getFirstLink().getHref().contains("page=1&display_size=10"), is(true));
assertThat(builder.getLastLink(), is(nullValue()));
Expand All @@ -117,12 +118,24 @@ public void shouldShowAllLinksCorrectly_whenLimitTotalParamIsSetAndMultiplePages
transactionSearchParams.setDisplaySize(10L);
transactionSearchParams.setLimitTotal(true);
PaginationBuilder builder = new PaginationBuilder(transactionSearchParams, mockedUriInfo)
.withTotalCount(120L);
.withTotalCount(120L)
.withCount(10L);
builder = builder.buildResponse();
assertThat(builder.getFirstLink().getHref().contains("page=1&display_size=10"), is(true));
assertThat(builder.getLastLink(), is(nullValue()));
assertThat(builder.getPrevLink().getHref().contains("page=2&display_size=10"), is(true));
assertThat(builder.getNextLink().getHref().contains("page=4&display_size=10"), is(true));
assertThat(builder.getSelfLink().getHref().contains("page=3&display_size=10"), is(true));
}

@Test
public void shouldNotGenerateNextLink_whenLimitTotalIsSetAndCountIsLessThanPageSize() {
transactionSearchParams.setDisplaySize(10L);
transactionSearchParams.setLimitTotal(true);
PaginationBuilder builder = new PaginationBuilder(transactionSearchParams, mockedUriInfo)
.withTotalCount(120L)
.withCount(9L);
builder = builder.buildResponse();
assertThat(builder.getNextLink(), is(nullValue()));
}
}

0 comments on commit 22deb19

Please sign in to comment.