Skip to content

Commit

Permalink
#40 [Update] 시/군/구 보내주는 로직 수정
Browse files Browse the repository at this point in the history
중복 제거, 정렬
  • Loading branch information
Anna-Jin committed Jul 30, 2022
1 parent 621c141 commit e2d80c6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
16 changes: 15 additions & 1 deletion src/main/java/com/mpnp/baechelin/common/QueryDslSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@
import static com.mpnp.baechelin.store.domain.QStore.store;

public class QueryDslSearch {
public static BooleanExpression matchAddress(String sido, String sigungu) {

public static BooleanExpression matchAddressWithSido(String sido) {
if (StringUtils.isEmpty(sido)) {
return null;
}

return Expressions.numberTemplate(
Integer.class,
"function('match', {0}, {1}, {2})", store.address, store.address, sido).gt(0);
}
public static BooleanExpression matchAddressWithSidoAndSigungu(String sido, String sigungu) {
// sido가 null이면 sigungu는 무조건 null
// sido가 null이 아니면 sigungu는 null 또는 not null
if (StringUtils.isEmpty(sido)) {
Expand All @@ -16,6 +26,10 @@ public static BooleanExpression matchAddress(String sido, String sigungu) {
return Expressions.numberTemplate(
Integer.class,
"function('match', {0}, {1}, {2})", store.address, store.address, sido).gt(0);
} else if (sigungu.split(" ").length > 1) {
return Expressions.numberTemplate(
Integer.class,
"function('match', {0}, {1}, {2})", store.address, store.address, sido + " +" + sigungu).gt(0);
}
return Expressions.numberTemplate(
Integer.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,19 @@ public Page<Store> findStoreOrderByBookmark(BigDecimal lat,
return new PageImpl<>(storeList, pageable, fetchCount);
}

public List<Store> getSigungu(String sido) {
BooleanExpression matchAddress = QueryDslSearch.matchAddressWithSido(sido);

return queryFactory
.selectFrom(store)
.where(matchAddress)
.fetch();
}


// 주소로 검색, 검색어로 검색
public List<Store> searchStores(String sido, String sigungu, String keyword, Pageable pageable) {
BooleanExpression matchAddress = QueryDslSearch.matchAddress(sido, sigungu);
BooleanExpression matchAddress = QueryDslSearch.matchAddressWithSidoAndSigungu(sido, sigungu);
BooleanExpression matchKeyword = QueryDslSearch.matchKeyword(keyword);

return queryFactory
Expand Down
36 changes: 23 additions & 13 deletions src/main/java/com/mpnp/baechelin/store/service/StoreService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;

@Service
Expand Down Expand Up @@ -191,23 +188,36 @@ public StoreDetailResponseDto getStore(long storeId, String socialId) {
/**
* 시/도 (ex. 서울시, 대전광역시)의 시/군/구 리스트 조회
* @param sido 시/도
* @return 시/군/구 리스트
* @return json 형태의 시/군/구 리스트
*/
public Map<String, List<String>> getSigungu(String sido) {
List<Store> storeList = storeRepository.findAll();
// FullText Search
List<Store> foundAddress = storeQueryRepository.getSigungu(sido);

// 결과를 json 형태로 리턴
Map<String, List<String>> result = new HashMap<>();
List<String> sigungu = new ArrayList<>();
// 중복 제거를 위해 Set 생성
Set<String> sigunguSet = new HashSet<>();

for (Store store : storeList) {
String addressSido = store.getAddress().split(" ")[0];
String addressSigungu = store.getAddress().split(" ")[1];
for (Store store : foundAddress) {
String[] address = store.getAddress().split(" "); // [0] : 시/도, [1] : 시/군/구, [2] : 구

if (addressSido.equals(sido)) {
sigungu.add(addressSigungu);
// 인자값인 sido와 주소의 첫번째 "시"가 같을 때
if (address[0].contains(sido)) {
// [경기도 성남시 분당구]처럼 도 - 시 - 구 로 나눠지는 경우 시 + 구로 반환
if (address[2].charAt(address[2].length() - 1) == '구') {
sigunguSet.add(address[1] + " " + address[2]);
} else {
sigunguSet.add(address[1]);
}
}
}

// 정렬을 위해 Set -> List로 변환
List<String> sigungu = new ArrayList<>(sigunguSet);
// List 정렬
Collections.sort(sigungu);

result.put("sigungu", sigungu);

return result;
Expand All @@ -220,7 +230,7 @@ public Map<String, List<String>> getSigungu(String sido) {
* @param keyword 검색어
* @param socialId 업장 pk
* @param pageable page, size
* @return
* @return 검색된 업장 리스트
*/
public List<StoreCardResponseDto> searchStores(String sido, String sigungu, String keyword, String socialId, Pageable pageable) {
List<Store> storeList = storeQueryRepository.searchStores(sido, sigungu, keyword, pageable);
Expand Down

0 comments on commit e2d80c6

Please sign in to comment.