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 25, 2022
1 parent 587bdf2 commit 89d6c3a
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 9 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ dependencies {
implementation 'io.awspring.cloud:spring-cloud-starter-aws:2.4.1'
//swagger
implementation 'io.springfox:springfox-boot-starter:3.0.0'
// commons lang3
implementation 'org.apache.commons:commons-lang3:3.12.0'

}
tasks.named('test') {
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/com/mpnp/baechelin/common/QueryDslSearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.mpnp.baechelin.common;

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

import static com.mpnp.baechelin.store.domain.QStore.store;

public class QueryDslSearch {
public static BooleanExpression matchAddress(String sido, String sigungu) {
// sido가 null이면 sigungu는 무조건 null
// sido가 null이 아니면 sigungu는 null 또는 not null
if (StringUtils.isEmpty(sido)) {
return null;
} else if (StringUtils.isEmpty(sigungu)) {
return null;
}
return Expressions.numberTemplate(
Integer.class,
"function('match', {0}, {1}, {2})", store.address, store.address, sido + " " + sigungu).gt(0);
}

public 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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.mpnp.baechelin.config.mysql;

import org.hibernate.dialect.MySQL57Dialect;
import org.hibernate.dialect.function.SQLFunctionTemplate;
import org.hibernate.type.StandardBasicTypes;

public class MySqlDialectCustom extends MySQL57Dialect {

public MySqlDialectCustom() {
registerFunction(
"match",
new SQLFunctionTemplate(StandardBasicTypes.INTEGER, "match(?1, ?2) against (?3 in boolean mode)")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,12 @@ public StoreCardResponseDto getStore(
@AuthenticationPrincipal User user) {

AuthToken authToken = tokenProvider.convertAccessToken(request);
if (!authToken.tokenValidate()) {
throw new CustomException(ErrorCode.INVALID_ACCESS_TOKEN);
}

String socialId = "";
if (user != null) {
socialId = user.getUsername();
if (authToken != null && !authToken.tokenValidate()) {
throw new CustomException(ErrorCode.INVALID_ACCESS_TOKEN);
}

return storeService.getStore(storeId, socialId);
return storeService.getStore(storeId, user == null ? null : user.getUsername());
}

@ApiOperation(value = "시/도 정보를 이용해 DB에 존재하는 시/군/구 정보를 조회하는 메소드")
Expand All @@ -100,7 +96,10 @@ public Map<String, List<String>> getSigungu(@RequestParam(required = false) Stri
public List<StoreCardResponseDto> searchStoresByKeyword(
@RequestParam String sido,
@RequestParam String sigungu,
@RequestParam String keyword) {
@RequestParam String keyword,
@AuthenticationPrincipal User user) {

storeService.searchStores(sido, sigungu, keyword);
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.mpnp.baechelin.store.repository;

import com.mpnp.baechelin.common.QueryDslSearch;
import com.mpnp.baechelin.common.QuerydslLocation;
import com.mpnp.baechelin.store.domain.QStore;
import com.mpnp.baechelin.store.domain.Store;
import com.mpnp.baechelin.store.dto.StoreCardResponseDto;
import com.querydsl.core.BooleanBuilder;
Expand Down Expand Up @@ -108,4 +110,16 @@ public Page<Store> findStoreOrderByBookmark(BigDecimal lat,
return new PageImpl<>(storeList, pageable, fetchCount);
}

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

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,12 @@ public Map<String, List<String>> getSigungu(String sido) {
return result;
}

public List<StoreCardResponseDto> searchStoresByKeyword(String sido, String sigungu, String keyword) {
public List<StoreCardResponseDto> searchStores(String sido, String sigungu, String keyword) {
List<Store> storeList = storeQueryRepository.searchStores(sido, sigungu, keyword);

// for (Store store : storeList) {
//
// }
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.mpnp.baechelin.store.repository;

import com.mpnp.baechelin.store.domain.Store;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
class StoreQueryRepositoryTest {

@Autowired
private StoreQueryRepository storeQueryRepository;


@Test
public void mysqlFullTextMath() {
String sido = null;
String sigungu = "강남구";
String keyword = "갈비";

List<Store> stores = storeQueryRepository.searchStores(sido, sigungu, keyword);

for (Store store : stores) {
System.out.println(store.getAddress());
}
}

}

0 comments on commit 89d6c3a

Please sign in to comment.