From 4b39a3453251756e53085b64ee90e207fd6a968a Mon Sep 17 00:00:00 2001 From: mjk25 Date: Mon, 31 Mar 2025 19:53:37 +0900 Subject: [PATCH] fix: address filter #99 --- .../search/domain/SearchSpecification.java | 20 +++++++++---------- .../backend/search/service/SearchService.java | 9 +-------- 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/backend/src/main/java/com/example/backend/search/domain/SearchSpecification.java b/backend/src/main/java/com/example/backend/search/domain/SearchSpecification.java index cad7b90c..cab046b9 100644 --- a/backend/src/main/java/com/example/backend/search/domain/SearchSpecification.java +++ b/backend/src/main/java/com/example/backend/search/domain/SearchSpecification.java @@ -4,7 +4,6 @@ import org.springframework.data.jpa.domain.Specification; import java.time.LocalDateTime; -import java.util.List; public class SearchSpecification { @@ -53,25 +52,24 @@ public static Specification hasPolice(String police) { // police == null ? criteriaBuilder.conjunction() : criteriaBuilder.equal(root.get("police"), police); } - - // cctv_id 필터 추가 (cctv_info 테이블에서 가져온 id 값) - public static Specification hasCctvIds(List cctvIds) { + // 주소 포함 필터 + public static Specification hasAddress(String address) { return (root, query, criteriaBuilder) -> { - if (cctvIds == null || cctvIds.isEmpty()) { - return criteriaBuilder.conjunction(); // 조건 없으면 전체 조회 + if (address == null || address.trim().isEmpty()) { + return criteriaBuilder.conjunction(); } - return root.get("cctv").get("id").in(cctvIds); + return criteriaBuilder.like(root.get("cctv").get("address"), "%" + address + "%"); }; } // 여러 조건을 조합하는 메서드 - public static Specification filterCases(String category, LocalDateTime startDate, LocalDateTime endDate, String police, List cctvIds, Integer officeId) { + public static Specification filterCases(String category, LocalDateTime startDate, LocalDateTime endDate, String police, String address, Integer officeId) { return Specification .where(hasOffice(officeId)) .and(hasState("완료")) - .and(hasDateRange(startDate, endDate)) // 날짜 범위 조건을 추가 - .and(hasCctvIds(cctvIds)) - .and(hasPolice(police)) // police 필터 추가 + .and(hasDateRange(startDate, endDate)) + .and(hasAddress(address)) + .and(hasPolice(police)) .and(hasCategory(category)); } diff --git a/backend/src/main/java/com/example/backend/search/service/SearchService.java b/backend/src/main/java/com/example/backend/search/service/SearchService.java index 258347bb..554002c2 100644 --- a/backend/src/main/java/com/example/backend/search/service/SearchService.java +++ b/backend/src/main/java/com/example/backend/search/service/SearchService.java @@ -3,7 +3,6 @@ import com.example.backend.common.domain.CaseEntity; import com.example.backend.search.domain.SearchSpecification; import com.example.backend.search.dto.*; -import com.example.backend.search.repository.CctvRepository; import com.example.backend.search.repository.SearchRepository; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.*; @@ -20,18 +19,12 @@ @RequiredArgsConstructor public class SearchService { private final SearchRepository searchRepository; - private final CctvRepository cctvRepository; private static final int PAGE_SIZE = 8; // 한 페이지당 8개씩 public SearchResult getCheckLog(String category, LocalDateTime startDate, LocalDateTime endDate, String address, String police, String order, Integer page, Integer officeId) { - // address가 있을 경우, cctv_info에서 해당 주소를 가진 cctv_id 리스트 조회 - List cctvIds = null; - if (address != null) { - cctvIds = cctvRepository.findCctvIdsByAddress(address); - } // 동적 검색 조건 생성 - Specification spec = SearchSpecification.filterCases(category, startDate, endDate, police, cctvIds, officeId); + Specification spec = SearchSpecification.filterCases(category, startDate, endDate, police, address, officeId); // 정렬 설정 Sort sort = Sort.unsorted();