Skip to content

Commit

Permalink
feat: update fruit resource
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Santana <[email protected]>
  • Loading branch information
otaviojava committed Apr 15, 2024
1 parent f5ab198 commit b382105
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions quarkus-pagination/src/main/java/os/expert/FruitResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
@Path("/fruits")
public class FruitResource {

private static final int MAX_PAGE_SIZE = 2;
private final FruitRepository fruitRepository;

private static final Sort<Fruit> ASC = Sort.asc("name");
Expand All @@ -25,24 +24,26 @@ public FruitResource(FruitRepository fruitRepository) {
@Path("/offset")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Iterable<Fruit> hello(@QueryParam("page") @DefaultValue("1") long page) {
var pageRequest = PageRequest.ofPage(page).size(MAX_PAGE_SIZE);
public Iterable<Fruit> hello(@QueryParam("page") @DefaultValue("1") long page,
@QueryParam("size") @DefaultValue("2") int size) {
var pageRequest = PageRequest.ofPage(page).size(size);
return fruitRepository.offSet(pageRequest).content();
}

@Path("/cursor")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Iterable<Fruit> cursor(@QueryParam("after") @DefaultValue("") String after,
@QueryParam("before") @DefaultValue("") String before) {
@QueryParam("before") @DefaultValue("") String before,
@QueryParam("size") @DefaultValue("2") int size) {
if (!after.isBlank()) {
var pageRequest = PageRequest.ofSize(MAX_PAGE_SIZE).afterCursor(PageRequest.Cursor.forKey(after));
var pageRequest = PageRequest.ofSize(size).afterCursor(PageRequest.Cursor.forKey(after));
return fruitRepository.cursor(pageRequest, ASC).content();
} else if (!before.isBlank()) {
var pageRequest = PageRequest.ofSize(MAX_PAGE_SIZE).beforeCursor(PageRequest.Cursor.forKey(before));
var pageRequest = PageRequest.ofSize(size).beforeCursor(PageRequest.Cursor.forKey(before));
return fruitRepository.cursor(pageRequest, DESC).stream().toList();
}
var pageRequest = PageRequest.ofSize(MAX_PAGE_SIZE);
var pageRequest = PageRequest.ofSize(size);
return fruitRepository.cursor(pageRequest, ASC).content();
}
}

0 comments on commit b382105

Please sign in to comment.