-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
88 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.test.criteria; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class Customer { | ||
|
||
private final Class<?> typeTocken; | ||
} |
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,15 @@ | ||
package org.test.criteria; | ||
|
||
import org.springframework.data.jpa.domain.Specification; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; | ||
import org.springframework.lang.Nullable; | ||
|
||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
public interface CustomerRepository | ||
extends JpaRepository<Customer, UUID>, JpaSpecificationExecutor<Customer> { | ||
|
||
List<Customer> findAll(@Nullable Specification<Customer> customerSpecification); | ||
} |
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,23 @@ | ||
package org.test.criteria; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import java.util.List; | ||
|
||
import static org.springframework.data.jpa.domain.Specification.where; | ||
import static org.test.criteria.CustomerSpecifications.CUSTOMER_HAS_BIRTHDAY; | ||
import static org.test.criteria.CustomerSpecifications.IS_LONG_TERM_CUSTOMER; | ||
|
||
public class CustomerService { | ||
|
||
private final CustomerRepository repository; | ||
|
||
@Autowired | ||
public CustomerService(CustomerRepository repository) { | ||
this.repository = repository; | ||
} | ||
|
||
public List<Customer> nothing() { | ||
return repository.findAll(where(CUSTOMER_HAS_BIRTHDAY).and(IS_LONG_TERM_CUSTOMER)); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/org/test/criteria/CustomerSpecifications.java
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,34 @@ | ||
package org.test.criteria; | ||
|
||
import javax.persistence.criteria.CriteriaBuilder; | ||
import javax.persistence.criteria.CriteriaQuery; | ||
import javax.persistence.criteria.Predicate; | ||
import javax.persistence.criteria.Root; | ||
import org.springframework.data.jpa.domain.Specification; | ||
|
||
import java.time.LocalDate; | ||
|
||
enum CustomerSpecifications implements Specification<Customer> { | ||
|
||
CUSTOMER_HAS_BIRTHDAY { | ||
@Override | ||
public Predicate toPredicate(Root<Customer> root, | ||
CriteriaQuery<?> query, | ||
CriteriaBuilder criteriaBuilder) { | ||
var birthday = root.<LocalDate>get("birthday"); | ||
var today = LocalDate.now(); | ||
return criteriaBuilder.equal(birthday, today); | ||
} | ||
}, | ||
|
||
IS_LONG_TERM_CUSTOMER { | ||
@Override | ||
public Predicate toPredicate(Root<Customer> root, | ||
CriteriaQuery<?> query, | ||
CriteriaBuilder criteriaBuilder) { | ||
var createdAt1 = root.<LocalDate>get("createdAt"); | ||
var twoYearsFromNow = LocalDate.now().minusYears(2); | ||
return criteriaBuilder.lessThan(createdAt1, twoYearsFromNow); | ||
} | ||
} | ||
} |
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,6 @@ | ||
@NonNullApi | ||
@CheckReturnValue | ||
package org.test.criteria; | ||
|
||
import com.google.errorprone.annotations.CheckReturnValue; | ||
import org.springframework.lang.NonNullApi; |