From ab8854d075d6bb4450bf7f87711bf663664fea9b Mon Sep 17 00:00:00 2001 From: Anna-Jin Date: Sun, 31 Jul 2022 01:41:41 +0900 Subject: [PATCH] =?UTF-8?q?#39=20[Update]=20=EA=B2=80=EC=83=89=20=EA=B2=B0?= =?UTF-8?q?=EA=B3=BC=EC=97=90=20=ED=8E=98=EC=9D=B4=EC=A7=95=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9=ED=95=98=EC=97=AC=20=EB=A6=AC=ED=84=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpnp/baechelin/common/QueryDslSearch.java | 15 +++-- .../store/controller/StoreController.java | 2 +- .../repository/StoreQueryRepository.java | 12 ++-- .../baechelin/store/service/StoreService.java | 60 ++++++------------- 4 files changed, 36 insertions(+), 53 deletions(-) diff --git a/src/main/java/com/mpnp/baechelin/common/QueryDslSearch.java b/src/main/java/com/mpnp/baechelin/common/QueryDslSearch.java index 97ad921..5dee665 100644 --- a/src/main/java/com/mpnp/baechelin/common/QueryDslSearch.java +++ b/src/main/java/com/mpnp/baechelin/common/QueryDslSearch.java @@ -18,22 +18,27 @@ public static BooleanExpression matchAddressWithSido(String sido) { "function('match', {0}, {1}, {2})", store.address, store.address, sido).gt(0); } public static BooleanExpression matchAddressWithSidoAndSigungu(String sido, String sigungu) { + String[] diviedSigungu = sigungu.split(" "); + // sido가 null이면 sigungu는 무조건 null - // sido가 null이 아니면 sigungu는 null 또는 not null if (StringUtils.isEmpty(sido)) { return null; } else if (StringUtils.isEmpty(sigungu)) { + // sido가 null이 아니고 sigungu가 null이면 sido 검색 결과 리턴 return Expressions.numberTemplate( Integer.class, "function('match', {0}, {1}, {2})", store.address, store.address, sido).gt(0); - } else if (sigungu.split(" ").length > 1) { + } else if (diviedSigungu.length > 1) { + // sigungu가 도/시/구 로 나눠져있을 때 (ex. 경기도 성남시 분당구) + // 정확한 검색을 위해 + 연산자 추가 + return Expressions.numberTemplate( + Integer.class, + "function('match', {0}, {1}, {2})", store.address, store.address, sido + " +" + diviedSigungu[0] + " +" + diviedSigungu[1]).gt(0); + } else { return Expressions.numberTemplate( Integer.class, "function('match', {0}, {1}, {2})", store.address, store.address, sido + " +" + sigungu).gt(0); } - return Expressions.numberTemplate( - Integer.class, - "function('match', {0}, {1}, {2})", store.address, store.address, sido + " +" + sigungu).gt(0); } public static BooleanExpression matchKeyword(String keyword) { diff --git a/src/main/java/com/mpnp/baechelin/store/controller/StoreController.java b/src/main/java/com/mpnp/baechelin/store/controller/StoreController.java index 2eaff96..d6babd8 100644 --- a/src/main/java/com/mpnp/baechelin/store/controller/StoreController.java +++ b/src/main/java/com/mpnp/baechelin/store/controller/StoreController.java @@ -95,7 +95,7 @@ public Map> getSigungu(@RequestParam String sido) { @ApiOperation(value = "시/도, 시/군/구, 검색어를 이용해 업장 리스트를 조회하는 메소드") @GetMapping("/search") - public List searchStoresByKeyword( + public StorePagedResponseDto searchStoresByKeyword( @RequestParam(required = false) String sido, @RequestParam(required = false) String sigungu, @RequestParam(required = false) String keyword, diff --git a/src/main/java/com/mpnp/baechelin/store/repository/StoreQueryRepository.java b/src/main/java/com/mpnp/baechelin/store/repository/StoreQueryRepository.java index 9f0124f..0fa6ac1 100644 --- a/src/main/java/com/mpnp/baechelin/store/repository/StoreQueryRepository.java +++ b/src/main/java/com/mpnp/baechelin/store/repository/StoreQueryRepository.java @@ -106,6 +106,7 @@ public Page findStoreOrderByBookmark(BigDecimal lat, return new PageImpl<>(storeList, pageable, fetchCount); } + // 시/도 정보로 시/군/구 정보를 조회 public List getSigungu(String sido) { BooleanExpression matchAddress = QueryDslSearch.matchAddressWithSido(sido); @@ -117,17 +118,20 @@ public List getSigungu(String sido) { // 주소로 검색, 검색어로 검색 - public List searchStores(String sido, String sigungu, String keyword, Pageable pageable) { + public Page searchStores(String sido, String sigungu, String keyword, Pageable pageable) { BooleanExpression matchAddress = QueryDslSearch.matchAddressWithSidoAndSigungu(sido, sigungu); BooleanExpression matchKeyword = QueryDslSearch.matchKeyword(keyword); - return queryFactory + List storeList = queryFactory .selectFrom(store) - .where(matchAddress, - matchKeyword) + .where(matchAddress, matchKeyword) .limit(pageable.getPageSize()) .offset(pageable.getOffset()) .fetch(); + int fetchSize = queryFactory.selectFrom(store) + .where(matchAddress, matchKeyword) + .fetch().size(); + return new PageImpl<>(storeList, pageable, fetchSize); } } diff --git a/src/main/java/com/mpnp/baechelin/store/service/StoreService.java b/src/main/java/com/mpnp/baechelin/store/service/StoreService.java index e1f1bfd..7788989 100644 --- a/src/main/java/com/mpnp/baechelin/store/service/StoreService.java +++ b/src/main/java/com/mpnp/baechelin/store/service/StoreService.java @@ -163,26 +163,15 @@ public StoreDetailResponseDto getStore(long storeId, String socialId) { List storeImageList = new ArrayList<>(); - store.getStoreImageList() - .forEach(storeImage -> storeImageList.add(storeImage.getStoreImageUrl())); - - store.getReviewList() - .forEach(review -> review.getReviewImageList() + store.getStoreImageList().forEach(storeImage -> storeImageList.add(storeImage.getStoreImageUrl())); + store.getReviewList().forEach(review -> review.getReviewImageList() .forEach(reviewImage -> storeImageList.add(reviewImage.getReviewImageUrl()))); - if (socialId == null) { - return new StoreDetailResponseDto(store, "N", storeImageList); - } else { - String isBookmark = "N"; - for (Bookmark bookmark : store.getBookmarkList()) { - if (bookmark.getStoreId().getId() == store.getId() - && bookmark.getUserId().getSocialId().equals(socialId)) { - isBookmark = "Y"; - break; - } - } - return new StoreDetailResponseDto(store, isBookmark, storeImageList); - } + User targetUser = socialId == null ? null : userRepository.findBySocialId(socialId); + + boolean isBookmark = bookmarkRepository.existsByStoreIdAndUserId(store, targetUser); + return new StoreDetailResponseDto(store, isBookmark ? "Y" : "N", storeImageList); + } /** @@ -224,34 +213,19 @@ public Map> getSigungu(String sido) { } /** - * 업장 검색 - * @param sido 시/도명 - * @param sigungu 시/군/구명 + * 엄장 검색 + * @param sido 시/도 명 + * @param sigungu 시/군/구 명 * @param keyword 검색어 - * @param socialId 업장 pk - * @param pageable page, size - * @return 검색된 업장 리스트 + * @param socialId 사용자 소셜 아이디 + * @param pageable 페이징 + * @return 페이징이 적용된 검색 결과 리턴 */ - public List searchStores(String sido, String sigungu, String keyword, String socialId, Pageable pageable) { - List storeList = storeQueryRepository.searchStores(sido, sigungu, keyword, pageable); + public StorePagedResponseDto searchStores(String sido, String sigungu, String keyword, String socialId, Pageable pageable) { + Page searchStores = storeQueryRepository.searchStores(sido, sigungu, keyword, pageable); - List result = new ArrayList<>(); + User targetUser = socialId == null ? null : userRepository.findBySocialId(socialId); - for (Store store : storeList) { - if (socialId == null) { - result.add(new StoreCardResponseDto(store, "N")); - } else { - String isBookmark = "N"; - for (Bookmark bookmark : store.getBookmarkList()) { - if (bookmark.getStoreId().getId() == store.getId() - && bookmark.getUserId().getSocialId().equals(socialId)) { - isBookmark = "Y"; - break; - } - } - result.add(new StoreCardResponseDto(store, isBookmark)); - } - } - return result; + return getStoreCardPagedResponseDto(targetUser, searchStores); } } \ No newline at end of file