-
Notifications
You must be signed in to change notification settings - Fork 2
#155 | The infrastructure for Pagination, Order, and Filter has been created. #158
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
aae3539
The infrastructure for Pagination, Order, and Filter has been created.
MenekseYuncu cc4376c
The necessary places have been corrected.
MenekseYuncu 86030bc
range fixed
MenekseYuncu 478c097
range and filter fixed
MenekseYuncu 8794eda
ticket filter fixed
MenekseYuncu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
src/main/java/org/gelecekbilimde/scienceplatform/common/model/BaseFilter.java
This file contains hidden or 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,10 @@ | ||
package org.gelecekbilimde.scienceplatform.common.model; | ||
|
||
import org.springframework.data.jpa.domain.Specification; | ||
|
||
public interface BaseFilter { | ||
|
||
@SuppressWarnings({"java:S3740", "rawtypes"}) | ||
Specification toSpecification(); | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/org/gelecekbilimde/scienceplatform/common/model/BasePage.java
This file contains hidden or 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,65 @@ | ||
package org.gelecekbilimde.scienceplatform.common.model; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.springframework.data.domain.Page; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@Setter | ||
@Builder | ||
public class BasePage<R> { | ||
|
||
private List<R> content; | ||
|
||
private Integer pageNumber; | ||
|
||
private Integer pageSize; | ||
|
||
private Integer totalPageCount; | ||
|
||
private Long totalElementCount; | ||
|
||
private List<BaseSort.BaseOrder> orderedBy; | ||
|
||
private BaseFilter filteredBy; | ||
|
||
|
||
public static <E, C> BasePage<C> of(final Page<E> pageableEntities, | ||
final List<C> content) { | ||
|
||
final var responseBuilder = BasePage.<C>builder() | ||
.content(content) | ||
.pageNumber(pageableEntities.getNumber() + 1) | ||
.pageSize(content.size()) | ||
.totalPageCount(pageableEntities.getTotalPages()) | ||
.totalElementCount(pageableEntities.getTotalElements()); | ||
|
||
if (pageableEntities.getSort().isSorted()) { | ||
responseBuilder.orderedBy(BaseSort.of(pageableEntities.getSort()).getOrders()); | ||
} | ||
|
||
return responseBuilder.build(); | ||
} | ||
|
||
public static <E, C> BasePage<C> of(final BaseFilter filter, | ||
final Page<E> pageableEntities, | ||
final List<C> content) { | ||
|
||
final var responseBuilder = BasePage.<C>builder() | ||
.content(content) | ||
.pageNumber(pageableEntities.getNumber() + 1) | ||
.pageSize(content.size()) | ||
.totalPageCount(pageableEntities.getTotalPages()) | ||
.totalElementCount(pageableEntities.getTotalElements()) | ||
.filteredBy(filter); | ||
|
||
if (pageableEntities.getSort().isSorted()) { | ||
responseBuilder.orderedBy(BaseSort.of(pageableEntities.getSort()).getOrders()); | ||
} | ||
|
||
return responseBuilder.build(); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/org/gelecekbilimde/scienceplatform/common/model/BasePageable.java
This file contains hidden or 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,45 @@ | ||
package org.gelecekbilimde.scienceplatform.common.model; | ||
|
||
|
||
import com.nimbusds.oauth2.sdk.util.CollectionUtils; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import lombok.experimental.SuperBuilder; | ||
import org.hibernate.validator.constraints.Range; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
@Getter | ||
@Setter | ||
@SuperBuilder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class BasePageable extends BaseSort { | ||
|
||
|
||
@Range(min = 1, max = 99999999) | ||
private int page; | ||
|
||
|
||
@Range(min = 10, max = 10) | ||
private int pageSize; | ||
|
||
|
||
public Pageable toPageable() { | ||
|
||
if (CollectionUtils.isNotEmpty(this.getOrders())) { | ||
return PageRequest.of( | ||
this.page - 1, | ||
this.pageSize, | ||
this.toSort() | ||
); | ||
} | ||
|
||
return PageRequest.of( | ||
this.page - 1, | ||
this.pageSize | ||
); | ||
} | ||
MenekseYuncu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
68 changes: 68 additions & 0 deletions
68
src/main/java/org/gelecekbilimde/scienceplatform/common/model/BaseSort.java
This file contains hidden or 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,68 @@ | ||
package org.gelecekbilimde.scienceplatform.common.model; | ||
|
||
import jakarta.validation.Valid; | ||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.*; | ||
import lombok.experimental.SuperBuilder; | ||
import org.springframework.data.domain.Sort; | ||
|
||
import java.util.List; | ||
|
||
|
||
@Getter | ||
@Setter | ||
@SuperBuilder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class BaseSort { | ||
|
||
@Valid | ||
protected List<BaseOrder> orders; | ||
MenekseYuncu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@Getter | ||
@Setter | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public static class BaseOrder { | ||
|
||
@NotBlank | ||
private String property; | ||
|
||
@NotNull | ||
private Direction direction; | ||
} | ||
|
||
public enum Direction { | ||
|
||
ASC, | ||
DESC; | ||
|
||
public Sort.Direction toDirection() { | ||
return Sort.Direction.valueOf(this.name()); | ||
} | ||
} | ||
|
||
protected org.springframework.data.domain.Sort toSort() { | ||
return org.springframework.data.domain.Sort.by( | ||
this.orders.stream() | ||
.map(order -> Sort.Order.by(order.getProperty()).with(order.getDirection().toDirection())) | ||
.toList() | ||
); | ||
} | ||
|
||
public static BaseSort of(final org.springframework.data.domain.Sort sorts) { | ||
MenekseYuncu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
List<BaseOrder> orders = sorts.stream() | ||
.map(order -> BaseOrder.builder() | ||
.property(order.getProperty()) | ||
.direction(Direction.valueOf(order.getDirection().toString())) | ||
.build()) | ||
.toList(); | ||
|
||
return BaseSort.builder() | ||
.orders(orders) | ||
.build(); | ||
} | ||
} |
31 changes: 0 additions & 31 deletions
31
src/main/java/org/gelecekbilimde/scienceplatform/common/model/Paging.java
This file was deleted.
Oops, something went wrong.
62 changes: 43 additions & 19 deletions
62
src/main/java/org/gelecekbilimde/scienceplatform/common/model/request/PagingRequest.java
This file contains hidden or 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 |
---|---|---|
@@ -1,32 +1,56 @@ | ||
package org.gelecekbilimde.scienceplatform.common.model.request; | ||
|
||
import jakarta.validation.Valid; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Data; | ||
import org.hibernate.validator.constraints.Range; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Pageable; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.gelecekbilimde.scienceplatform.common.model.BasePageable; | ||
import org.gelecekbilimde.scienceplatform.common.model.BaseSort; | ||
import org.springframework.util.CollectionUtils; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
|
||
@Data | ||
public class PagingRequest { | ||
|
||
@NotNull | ||
@Range(min = 1) | ||
public Integer page = 1; | ||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
public abstract class PagingRequest { | ||
|
||
@Valid | ||
@NotNull | ||
@Range(min = 2, max = 100) | ||
public Integer pageSize = 20; | ||
protected BasePageable pageable; | ||
|
||
public abstract boolean isOrderPropertyAccepted(); | ||
|
||
public Pageable toPageable() { | ||
return PageRequest.of( | ||
this.getPage(), | ||
this.getPageSize() | ||
); | ||
} | ||
@SuppressWarnings("all") | ||
public boolean isPropertyAccepted(final Set<String> acceptedProperties) { | ||
|
||
if (this.pageable == null || CollectionUtils.isEmpty(this.pageable.getOrders())) { | ||
return true; | ||
} | ||
|
||
for (BaseSort.BaseOrder order : this.pageable.getOrders()) { | ||
if (StringUtils.isBlank(order.getProperty()) || order.getDirection() == null) { | ||
return true; | ||
} | ||
} | ||
MenekseYuncu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
final List<BaseSort.BaseOrder> orders = this.pageable.getOrders(); | ||
if (CollectionUtils.isEmpty(orders)) { | ||
return true; | ||
} | ||
MenekseYuncu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
final boolean isAnyDirectionEmpty = orders.stream().anyMatch(order -> order.getDirection() == null); | ||
final boolean isAnyPropertyEmpty = orders.stream().anyMatch(order -> order.getProperty() == null); | ||
if (isAnyDirectionEmpty || isAnyPropertyEmpty) { | ||
return true; | ||
} | ||
MenekseYuncu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public Integer getPage() { | ||
return this.page - 1; | ||
return orders.stream() | ||
.map(BaseSort.BaseOrder::getProperty) | ||
.allMatch(acceptedProperties::contains); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
27 changes: 27 additions & 0 deletions
27
src/main/java/org/gelecekbilimde/scienceplatform/post/model/AdminPostFilter.java
This file contains hidden or 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,27 @@ | ||
package org.gelecekbilimde.scienceplatform.post.model; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.gelecekbilimde.scienceplatform.common.model.BaseFilter; | ||
import org.gelecekbilimde.scienceplatform.post.model.entity.PostEntity; | ||
import org.springframework.data.jpa.domain.Specification; | ||
|
||
|
||
@Getter | ||
@Setter | ||
public class AdminPostFilter implements BaseFilter { | ||
|
||
private Boolean isActive; | ||
|
||
@Override | ||
public Specification<PostEntity> toSpecification() { | ||
Specification<PostEntity> specification = Specification.where(null); | ||
|
||
if (isActive != null) { | ||
specification = specification.and((root, query, criteriaBuilder) -> | ||
criteriaBuilder.equal(root.get("isActive"), isActive) | ||
); | ||
} | ||
return specification; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.