-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* #23870 : Add pagination info to the API response. * As per Freddy Montes' feedback, removing the unnecessary value for the `x-pagination-link-pages` header. * Implementing SonarQube feedback.
- Loading branch information
1 parent
4d158c9
commit 124ec30
Showing
5 changed files
with
246 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package com.dotcms.rest; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* Provides pagination data associated to the entity returned by any dotCMS REST Endpoint. This way, developers can | ||
* still access pagination data even when accessing our APIs through proxies that may remove the already existing | ||
* pagination headers. | ||
* | ||
* @author Jose Castro | ||
* @since Mar 3rd, 2023 | ||
*/ | ||
@JsonDeserialize(builder = Pagination.Builder.class) | ||
public class Pagination implements Serializable { | ||
|
||
private final int currentPage; | ||
private final int perPage; | ||
private final long totalEntries; | ||
|
||
/** | ||
* Private constructor used to create an instance of this class. | ||
* | ||
* @param builder The {@link Builder} class for the pagination object. | ||
*/ | ||
private Pagination(final Builder builder) { | ||
this.currentPage = builder.currentPage; | ||
this.perPage = builder.perPage; | ||
this.totalEntries = builder.totalEntries; | ||
} | ||
|
||
public int getCurrentPage() { | ||
return this.currentPage; | ||
} | ||
|
||
public int getPerPage() { | ||
return this.perPage; | ||
} | ||
|
||
public long getTotalEntries() { | ||
return this.totalEntries; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Pagination{" + "currentPage=" + this.currentPage + ", perPage=" + this.perPage + ", totalEntries=" + this.totalEntries + '}'; | ||
} | ||
|
||
/** | ||
* This builder allows you to create an instance of the {@link Pagination} class. | ||
*/ | ||
public static class Builder { | ||
|
||
@JsonProperty | ||
private int currentPage; | ||
@JsonProperty | ||
private int perPage; | ||
@JsonProperty | ||
private long totalEntries; | ||
|
||
/** | ||
* Returns the currently selected results page, or the first one if not specified. | ||
* | ||
* @param currentPage The current results page. | ||
* | ||
* @return The current {@link Builder} instance. | ||
*/ | ||
public Builder currentPage(int currentPage) { | ||
this.currentPage = currentPage; | ||
return this; | ||
} | ||
|
||
/** | ||
* The maximum number of items that are included in a results page. | ||
* | ||
* @param perPage The maximum number of returned items. | ||
* | ||
* @return The current {@link Builder} instance. | ||
*/ | ||
public Builder perPage(int perPage) { | ||
this.perPage = perPage; | ||
return this; | ||
} | ||
|
||
/** | ||
* The total number of results for a given data query. That is, the total list of <b>unfiltered items</b> for a | ||
* given query. | ||
* | ||
* @param totalEntries The total number of results. | ||
* | ||
* @return The current {@link Builder} instance. | ||
*/ | ||
public Builder totalEntries(long totalEntries) { | ||
this.totalEntries = totalEntries; | ||
return this; | ||
} | ||
|
||
/** | ||
* Creates an instance of the {@link Pagination} class. | ||
* | ||
* @return A new instance of the {@link Pagination} class. | ||
*/ | ||
public Pagination build() { | ||
return new Pagination(this); | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.