-
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.
refactor: be 카테고리 검색 Specification을 통해 검색조건 확장 쉽도록 바꿈
Former-commit-id: b3433716dff68b6e885605011760fd8fd3a727d6
- Loading branch information
Showing
3 changed files
with
211 additions
and
104 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
83 changes: 83 additions & 0 deletions
83
Backend/src/main/java/com/ssafy/buki/domain/hobbyclass/HobbyClassSpec.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,83 @@ | ||
package com.ssafy.buki.domain.hobbyclass; | ||
|
||
import com.ssafy.buki.exception.BusinessException; | ||
import com.ssafy.buki.exception.ErrorCode; | ||
import org.springframework.data.jpa.domain.Specification; | ||
|
||
import javax.persistence.criteria.CriteriaBuilder; | ||
import javax.persistence.criteria.CriteriaQuery; | ||
import javax.persistence.criteria.Predicate; | ||
import javax.persistence.criteria.Root; | ||
|
||
public class HobbyClassSpec { | ||
//대분류 | ||
//소분류 | ||
//minPrice~ maxPrice | ||
//minPrice | ||
//시군구 | ||
//시도 | ||
|
||
public static Specification<HobbyClass> BigCategory(Integer bigCategoryId) { | ||
return new Specification<HobbyClass>() { | ||
@Override | ||
public Predicate toPredicate(Root<HobbyClass> root, | ||
CriteriaQuery<?> query, | ||
CriteriaBuilder builder) { | ||
if (bigCategoryId == null) throw new BusinessException(ErrorCode.NOT_RIGHT_DATA); | ||
|
||
return builder.equal(root.get("bigCategory"), bigCategoryId); | ||
} | ||
}; | ||
} | ||
|
||
public static Specification<HobbyClass> SmallCategory(Integer smallCategoryId) { | ||
return new Specification<HobbyClass>() { | ||
@Override | ||
public Predicate toPredicate(Root<HobbyClass> root, | ||
CriteriaQuery<?> query, | ||
CriteriaBuilder builder) { | ||
if (smallCategoryId == null) return null; | ||
|
||
return builder.equal(root.get("smallCategory"), smallCategoryId); | ||
} | ||
}; | ||
} | ||
|
||
public static Specification<HobbyClass> Sigungu(Integer sigunguId) { | ||
return new Specification<HobbyClass>() { | ||
@Override | ||
public Predicate toPredicate(Root<HobbyClass> root, | ||
CriteriaQuery<?> query, | ||
CriteriaBuilder builder) { | ||
if (sigunguId == null) return null; | ||
Integer sidoId = null; | ||
if (sigunguId.equals(1)) { | ||
sidoId = 1; | ||
} | ||
if (sigunguId.equals(13)) { | ||
sidoId = 2; | ||
} | ||
if (sidoId != null) { | ||
return builder.equal(root.get("sido"), sidoId); | ||
} | ||
return builder.equal(root.get("sigunguId"), sigunguId); | ||
} | ||
}; | ||
} | ||
|
||
public static Specification<HobbyClass> Price(Integer minPrice, Integer maxPrice) { | ||
return new Specification<HobbyClass>() { | ||
@Override | ||
public Predicate toPredicate(Root<HobbyClass> root, | ||
CriteriaQuery<?> query, | ||
CriteriaBuilder builder) { | ||
if (minPrice == null && maxPrice == null) return null; | ||
if (maxPrice == null) throw new BusinessException(ErrorCode.NOT_RIGHT_DATA); | ||
Integer minTempPrice = minPrice == null ? 0 : minPrice; | ||
|
||
return builder.between(root.get("price"), minTempPrice, maxPrice); | ||
} | ||
}; | ||
} | ||
|
||
} |
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