Skip to content

Commit

Permalink
#39 [Update] 업장 검색에 카테고리, 시설 분류 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Anna-Jin committed Jul 31, 2022
1 parent 6c43aec commit 876af65
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 17 deletions.
24 changes: 22 additions & 2 deletions src/main/java/com/mpnp/baechelin/common/QueryDslSearch.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.mpnp.baechelin.common;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.core.types.dsl.StringPath;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

import static com.mpnp.baechelin.common.QuerydslLocation.getBooleanBuilder;
import static com.mpnp.baechelin.store.domain.QStore.store;

public class QueryDslSearch {
Expand All @@ -17,7 +22,7 @@ public static BooleanExpression matchAddressWithSido(String sido) {
Integer.class,
"function('match', {0}, {1}, {2})", store.address, store.address, sido).gt(0);
}
public static BooleanExpression matchAddressWithSidoAndSigungu(String sido, String sigungu) {
private static BooleanExpression matchAddressWithSidoAndSigungu(String sido, String sigungu) {

// sido가 null이면 sigungu는 무조건 null
if (StringUtils.isEmpty(sido)) {
Expand All @@ -40,12 +45,27 @@ public static BooleanExpression matchAddressWithSidoAndSigungu(String sido, Stri
}
}

public static BooleanExpression matchKeyword(String keyword) {
private static BooleanExpression matchKeyword(String keyword) {
if (StringUtils.isEmpty(keyword)) {
return null;
}
return Expressions.numberTemplate(
Integer.class,
"function('match', {0}, {1}, {2})", store.name, store.category, keyword).gt(0);
}

public static BooleanBuilder getSearchBooleanBuilder(String sido, String sigungu, String keyword, String category, List<String> facility) {
BooleanBuilder builder = new BooleanBuilder();

// 지역 fulltext search
builder.and(matchAddressWithSidoAndSigungu(sido, sigungu));

// 검색어 fulltext search
builder.and(matchKeyword(keyword));

// 카테고리, 시설
builder.and(getBooleanBuilder(category, facility, builder));

return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,11 @@ public StorePagedResponseDto searchStoresByKeyword(
@RequestParam(required = false) String sido,
@RequestParam(required = false) String sigungu,
@RequestParam(required = false) String keyword,
@RequestParam(required = false) String category,
@RequestParam(required = false) List<String> facility,
@PageableDefault Pageable pageable,
@AuthenticationPrincipal User user) {

return storeService.searchStores(sido, sigungu, keyword, user == null ? null : user.getUsername(), pageable);
return storeService.searchStores(sido, sigungu, keyword, category, facility, user == null ? null : user.getUsername(), pageable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

import com.mpnp.baechelin.common.QueryDslSearch;
import com.mpnp.baechelin.common.QuerydslLocation;
import com.mpnp.baechelin.review.domain.Review;
import com.mpnp.baechelin.store.domain.QStore;
import com.mpnp.baechelin.store.domain.Store;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.Tuple;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.core.types.dsl.NumberPath;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
Expand All @@ -23,8 +19,8 @@
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.stream.Collectors;

import static com.mpnp.baechelin.common.QueryDslSearch.getSearchBooleanBuilder;
import static com.mpnp.baechelin.common.QuerydslLocation.locTwoPointAndConditions;
import static com.mpnp.baechelin.store.domain.QStore.store;

Expand Down Expand Up @@ -156,18 +152,17 @@ public List<Store> getSigungu(String sido) {


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

List<Store> storeList = queryFactory
.selectFrom(store)
.where(matchAddress, matchKeyword)
.where(builder)
.limit(pageable.getPageSize())
.offset(pageable.getOffset())
.fetch();
int fetchSize = queryFactory.selectFrom(store)
.where(matchAddress, matchKeyword)
.where(builder)
.fetch().size();
return new PageImpl<>(storeList, pageable, fetchSize);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.mpnp.baechelin.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.scheduling.annotation.EnableScheduling;
Expand Down Expand Up @@ -155,7 +156,6 @@ private List<StoreCardResponseDto> getStoreCardResponseDtos(User targetUser, Lis

/**
* 업장 상세 조회
*
* @param storeId 업장 아이디
* @param socialId 유저 social 아이디
* @return 업장 상세 정보
Expand Down Expand Up @@ -216,16 +216,16 @@ public Map<String, List<String>> getSigungu(String sido) {
}

/**
* 엄장 검색
* 업장 검색
* @param sido 시/도명
* @param sigungu 시/군/구명
* @param keyword 검색어
* @param socialId 사용자 소셜 아이디
* @param pageable 페이징
* @return 페이징이 적용된 검색 결과 리턴
*/
public StorePagedResponseDto searchStores(String sido, String sigungu, String keyword, String socialId, Pageable pageable) {
Page<Store> searchStores = storeQueryRepository.searchStores(sido, sigungu, keyword, pageable);
public StorePagedResponseDto searchStores(String sido, String sigungu, String keyword, String category, List<String> facility, String socialId, Pageable pageable) {
Page<Store> searchStores = storeQueryRepository.searchStores(sido, sigungu, keyword, category, facility, pageable);

User targetUser = socialId == null ? null : userRepository.findBySocialId(socialId);

Expand Down

0 comments on commit 876af65

Please sign in to comment.