Skip to content
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

Feature: remove custom pageable #14

Merged
merged 3 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
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
108 changes: 53 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,63 @@ dependencies {

### Pagination Parameters

Use default pagination from Spring.

The following query parameters manage pagination options:
- `page`: Integer starting at 0, representing the desired page.
- `pageSize`: Integer from 1 to 100, representing the number of elements per page. Defaults to 10.
- `order`: Field name to sort by.
- `sort`: Sort direction, either asc (ascending) or desc (descending). Defaults to descending.
- `size`: Integer from 1 to 100, representing the number of elements per page. Defaults to 10.
- `sort`: Field name to sort by.
- `direction`: Sort direction, either asc (ascending) or desc (descending). Defaults to descending.

**Example:**

```text
http://localhost:8080/myEndpoint?page=2&pageSize=7&order=name&sort=asc
```

You can use `sort` parameter for multiples sort:

**Example:**

```text
http://localhost:8080/myEndpoint?sort=name,asc&sort=price,desc
```

Here’s an improved version of your documentation section in English:

---

## Usage in HTTP Requests

### Pagination Parameters

The library uses Spring's default pagination mechanism to manage paginated responses.

You can control pagination through the following query parameters:

- **`page`**: Specifies the zero-based page index to retrieve. Defaults to `0` if not provided.
- **`size`**: Specifies the number of elements per page. Must be between `1` and `100`, with a default of `10`.
- **`sort`**: Specifies the field(s) by which to sort the results.
- **`direction`**: Specifies the sorting direction. Acceptable values are `asc` (ascending) or `desc` (descending). Defaults to `desc`.

#### Examples

**Single Sort**

To fetch the second page with 7 elements per page, sorted by `name` in ascending order:

```text
GET http://localhost:8080/myEndpoint?page=1&size=7&sort=name,asc
```

**Multiple Sort Criteria**

To sort by multiple fields, chain `sort` parameters. For example, to sort by `name` in ascending order and then by `price` in descending order:

```text
GET http://localhost:8080/myEndpoint?sort=name,asc&sort=price,desc
```

### Filtering Operators

Here is the list of operator that can be used to filter data:
Expand Down Expand Up @@ -162,7 +207,7 @@ public class ExampleController {

@GetMapping("/myEndpoint")
public Page<MyEntity> find(@RequestParam Map<String, List<String>> allParams,
@ModelAttribute SpringQueryFilter queryFilter) {
@PageableDefault(page = 0, size = 10, sort = "lastName", direction = Sort.Direction.ASC) Pageable pageable) {
return myService.find(allParams, queryFilter);
}
}
Expand All @@ -185,7 +230,7 @@ public interface YourEntityRepository extends JpaRepository<YourEntity, Long> {
public interface YourEntityService {
(...)

Page<YourEntity> find(Map<String, List<String>> filters, SpringQueryFilter queryFilter);
Page<YourEntity> find(Map<String, List<String>> filters, Pageable pageable);
}
```

Expand All @@ -199,20 +244,15 @@ public class YourEntityServiceImpl implements YourEntityService {

@Override
public Page<YourEntity> find(final Map<String, List<String>> filters,
final SpringQueryFilter queryFilter) {
final Pageable pageable) {
return this.yourEntityRepository.find(
new QueryFilterSpecification<>(YourEntity.class, filters),
queryFilter.getPageable() // or queryFilter.getPageable("name") to apply a default order.
pageable
);
}
}
```

You can define default sort in your service by using:
```java
queryFilter.getPageable("name");
```

### Filter without queryParameters

To apply filters without using query parameters directly from the controller, you can manually define filter conditions:
Expand All @@ -237,53 +277,11 @@ public class YourEntityServiceImpl implements YourEntityService {

return this.yourEntityRepository.find(
new QueryFilterSpecification<>(YourEntity.class, filters),
// Current page, limit, field to order and sort
new SpringQueryFilter(0, 10, "name", "asc").getPageable()
PageRequest.of(0, 10, Sort.by(Sort.Order.asc("name")))
);
}
}
```
## Custom Pagination Options

If you need specific pagination settings, implement `ISpringQueryFilter` to define your own minimum, maximum, sort values, or any other pagination-related configurations.

```java
@NoArgsConstructor
@AllArgsConstructor
public class SpringQueryFilter implements ISpringQueryFilter {

private static final int DEFAULT_PAGE_SIZE = 1;
private static final int MIN_PAGE_SIZE = 1;
private static final int MAX_PAGE_SIZE = 10;
private Integer page = 0;
private Integer pageSize = 1;
private String order;
private String sort;

@Override
public int getComputedPage() {
return Math.max(Optional.ofNullable(page).orElse(0), 0);
}

@Override
public int getComputedSize() {
return Math.clamp(Optional.ofNullable(pageSize).orElse(DEFAULT_PAGE_SIZE), MIN_PAGE_SIZE, MAX_PAGE_SIZE);
}

@Override
public String getOrder() {
return this.order;
}


@Override
public boolean isAscendantSort() {
return "asc".equalsIgnoreCase(sort);
}
}
```

This configuration allows you to control the min, max, sort values, and create a customized pageable option name in query parameters.

## Custom Types

Expand Down
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html)

## [Unreleased]

### Removed

- `SpringQueryFilter` to uses `Pageable` of spring.

## [1.1.3] 2024/11/20

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.zorin95670</groupId>
<artifactId>spring-query-filter</artifactId>
<version>1.1.3</version>
<version>1.1.4-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring-query-filter</name>
Expand Down
89 changes: 0 additions & 89 deletions src/main/java/io/github/zorin95670/query/ISpringQueryFilter.java

This file was deleted.

Loading
Loading