Skip to content

Commit

Permalink
feat: # 101 카테고리 검색 유지 구현
Browse files Browse the repository at this point in the history
feat: #101 카테고리 검색 유지 구현
  • Loading branch information
parkjonggyeong18 authored May 8, 2024
2 parents 29b50d8 + edddc6a commit 34b30b6
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -31,7 +32,6 @@ public class ElasticController {
* @param searchType 검색 유형
* @param pageNo 페이지 번호 (기본값: 0)
* @param pageSize 페이지 크기 (기본값: 10)
* @param categoryId 카테고리 검색을 위한 카테고리번호
* @param sortBy 정렬 기준 (기본값: "_socre")
* @return HTTP 상태 및 도서 목록 응답
* @author parkjonggyeong18(박종경)
Expand All @@ -43,16 +43,47 @@ public ResponseEntity<BaseResponse<PageResponse<ElasticResponse>>> getSearchPage
@RequestParam(value = "searchType") String searchType,
@RequestParam(value = "pageNo", defaultValue = "0", required = false) int pageNo,
@RequestParam(value = "pageSize", defaultValue = "10", required = false) int pageSize,
@RequestParam(value = "categoryId", required = false) BigDecimal categoryId,
@RequestParam(value = "sortBy", defaultValue = "_score", required = false) String sortBy) {
if (query == null) {
return ResponseEntity.badRequest().body(new BaseResponse<>());
}

Pageable pageable = PageRequest.of(pageNo, pageSize, Sort.by(sortBy));
PageResponse<ElasticResponse> searchList = (categoryId == null)
? elasticService.search(query, searchType, pageable)
: elasticCategoryService.search(query, searchType, categoryId, pageable);
PageResponse<ElasticResponse> searchList = elasticService.search(query, searchType, pageable);

BaseResponse<PageResponse<ElasticResponse>> responseBody = new BaseResponse<>();
return searchList.getContent().isEmpty()
? ResponseEntity.status(HttpStatus.NO_CONTENT).body(responseBody.message("검색한 도서가 없습니다."))
: ResponseEntity.ok(responseBody.data(searchList));
}

/**
* elasticsearch 기반 category 검색
*
* @param query text 검색어
* @param searchType 검색 유형
* @param pageNo 페이지 번호 (기본값: 0)
* @param pageSize 페이지 크기 (기본값: 10)
* @param categoryId 카테고리 검색을 위한 카테고리번호
* @param sortBy 정렬 기준 (기본값: "_socre")
* @return HTTP 상태 및 도서 목록 응답
* @author parkjonggyeong18(박종경)
*/
@GetMapping("/category/{categoryId}/search")
public ResponseEntity<BaseResponse<PageResponse<ElasticResponse>>> getCategorySearchPage(
@RequestParam(value = "query") String query,
@RequestParam(value = "searchType") String searchType,
@RequestParam(value = "pageNo", defaultValue = "0", required = false) int pageNo,
@RequestParam(value = "pageSize", defaultValue = "10", required = false) int pageSize,
@PathVariable(value = "categoryId", required = false) BigDecimal categoryId,
@RequestParam(value = "sortBy", defaultValue = "_score", required = false) String sortBy) {
if (query == null) {
return ResponseEntity.badRequest().body(new BaseResponse<>());
}

Pageable pageable = PageRequest.of(pageNo, pageSize, Sort.by(sortBy));
PageResponse<ElasticResponse> searchList =
elasticCategoryService.search(query, searchType, categoryId, pageable);

BaseResponse<PageResponse<ElasticResponse>> responseBody = new BaseResponse<>();
return searchList.getContent().isEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,4 @@ public SearchHits<ElasticDocument> elasticPageable(QueryBuilder queryBuilder, Pa
}


}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// File: src/main/java/com/example/project/util/Constants.java

package com.t3t.bookstoreapi.elastic.util;

public class Constants {
Expand All @@ -15,4 +13,4 @@ public class Constants {
public static final String CATEGORY_ID = "category_id";


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,9 @@ void getSearchPage_ReturnsResults_WithCategoryId() throws Exception {

when(elasticCategoryService.search(anyString(), anyString(), any(BigDecimal.class), any(Pageable.class))).thenReturn(pageResponse);

mockMvc.perform(MockMvcRequestBuilders.get("/search")
mockMvc.perform(MockMvcRequestBuilders.get("/category/{categoryId}/search","123")
.param("query", "책")
.param("searchType", "book_name")
.param("categoryId", "123")
.param("pageNo", "0")
.param("pageSize", "10")
.param("sortBy", "_score")
Expand All @@ -149,16 +148,23 @@ void getSearchPage_NoResults_WithCategoryId() throws Exception {

when(elasticCategoryService.search(anyString(), anyString(), any(BigDecimal.class), any(Pageable.class))).thenReturn(pageResponse);

mockMvc.perform(MockMvcRequestBuilders.get("/search")
mockMvc.perform(MockMvcRequestBuilders.get("/category/{categoryId}/search","123")
.param("query", "없는책")
.param("searchType", "book_name")
.param("categoryId", "123")
.param("pageNo", "0")
.param("pageSize", "10")
.param("sortBy", "_score")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNoContent())
.andExpect(jsonPath("$.message").value("검색한 도서가 없습니다."));
}
@Test
@DisplayName("검색어가 없는 예외 테스트 - with categoryId")
void getSearchPage_BadRequest_WithCategoryId() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/category/{categoryId}/search","123")
.param("query", "")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest());
}

}

0 comments on commit 34b30b6

Please sign in to comment.