-
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
5 changed files
with
51 additions
and
14 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 |
---|---|---|
|
@@ -7,4 +7,5 @@ | |
@Getter | ||
public class RequestRequest { | ||
private List<String> category; | ||
private boolean hasReview; | ||
} |
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
40 changes: 40 additions & 0 deletions
40
src/main/java/com/makive/moumi/repository/specification/RequestSpecifications.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,40 @@ | ||
package com.makive.moumi.repository.specification; | ||
|
||
import com.makive.moumi.model.domain.Request; | ||
import com.makive.moumi.model.domain.TranslationCategory; | ||
import jakarta.persistence.criteria.Predicate; | ||
import jakarta.persistence.criteria.Root; | ||
import jakarta.persistence.criteria.Subquery; | ||
import org.springframework.data.jpa.domain.Specification; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class RequestSpecifications { | ||
public static Specification<Request> findAllByClientIdAndCategory(Long clientId, List<String> category, boolean hasReview) { | ||
return (root, query, criteriaBuilder) -> { | ||
List<Predicate> predicates = new ArrayList<>(); | ||
|
||
predicates.add(criteriaBuilder.equal(root.get("client").get("id"), clientId)); | ||
|
||
if (!category.isEmpty()) { | ||
Subquery<Long> subquery = query.subquery(Long.class); | ||
Root<TranslationCategory> subqueryTranslationCategory = subquery.from(TranslationCategory.class); | ||
subquery.select(subqueryTranslationCategory.get("translation").get("id")); | ||
subquery.groupBy(subqueryTranslationCategory.get("translation").get("id")); | ||
subquery.having(criteriaBuilder.equal(criteriaBuilder.count(subqueryTranslationCategory.get("category").get("name")), category.size())); | ||
subquery.where(subqueryTranslationCategory.get("category").get("name").in(category)); | ||
|
||
predicates.add(root.get("id").in(subquery)); | ||
} | ||
|
||
if (hasReview) { | ||
predicates.add(criteriaBuilder.isNotNull(root.get("review"))); | ||
} else { | ||
predicates.add(criteriaBuilder.isNull(root.get("review"))); | ||
} | ||
|
||
return criteriaBuilder.and(predicates.toArray(new Predicate[0])); | ||
}; | ||
} | ||
} |
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