Skip to content

Commit 16b8a1b

Browse files
authored
Merge pull request #102 from Project-BookLog/fix/101/search
Fix/101/search
2 parents 2d8dd0d + 3755c89 commit 16b8a1b

File tree

4 files changed

+58
-7
lines changed

4 files changed

+58
-7
lines changed

booklog/src/main/java/com/example/booklog/domain/search/controller/SearchController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class SearchController {
5353
*/
5454
@GetMapping("/books")
5555
public BookSearchResponse searchBooks(
56-
@RequestParam String query,
56+
@RequestParam(required = false) String query,
5757
@RequestParam(defaultValue = "1") int page,
5858
@RequestParam(defaultValue = "10") int size,
5959
@RequestParam(defaultValue = "latest") String sort
@@ -73,7 +73,7 @@ public BookSearchResponse searchBooks(
7373
*/
7474
@GetMapping("/authors")
7575
public AuthorSearchResponse searchAuthors(
76-
@RequestParam String query,
76+
@RequestParam(required = false) String query,
7777
@RequestParam(defaultValue = "1") int page,
7878
@RequestParam(defaultValue = "10") int size
7979
) {
@@ -175,7 +175,7 @@ public RecommendationSearchResponse getRecommendations() {
175175
*/
176176
@GetMapping
177177
public ResponseEntity<IntegratedSearchResponse> search(
178-
@RequestParam String query,
178+
@RequestParam(required = false) String query,
179179
@RequestParam(defaultValue = "latest") String sort
180180
) {
181181
IntegratedSearchResponse response = integratedSearchService.search(query, sort);

booklog/src/main/java/com/example/booklog/domain/search/service/BookSearchService.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import com.example.booklog.domain.library.books.repository.BooksRepository;
77
import com.example.booklog.domain.library.books.service.BookImportService;
88
import com.example.booklog.domain.search.dto.BookSortType;
9+
import com.example.booklog.global.common.apiPayload.code.status.ErrorStatus;
10+
import com.example.booklog.global.common.apiPayload.exception.GeneralException;
911
import lombok.RequiredArgsConstructor;
1012
import lombok.extern.slf4j.Slf4j;
1113
import org.springframework.data.domain.Page;
@@ -56,9 +58,7 @@ public BookSearchResponse searchBooks(String query, int page, int size, BookSort
5658
query, page, size, sortType.getValue());
5759

5860
// 입력 검증
59-
if (query == null || query.trim().isEmpty()) {
60-
return new BookSearchResponse(page, size, true, 0, List.of());
61-
}
61+
validateSearchInput(query, page, size);
6262

6363
// 정렬 기준 생성
6464
Sort sort = createSort(sortType);
@@ -157,5 +157,27 @@ private BookSearchItemResponse convertToSearchItem(Books book) {
157157
book.getPublishedDate()
158158
);
159159
}
160-
}
161160

161+
/**
162+
* 검색 입력값 검증
163+
*
164+
* @throws GeneralException 검색어가 유효하지 않은 경우
165+
*/
166+
private void validateSearchInput(String query, int page, int size) {
167+
if (query == null || query.trim().isEmpty()) {
168+
throw new GeneralException(ErrorStatus.SEARCH_KEYWORD_REQUIRED);
169+
}
170+
171+
if (query.trim().length() > 100) {
172+
throw new GeneralException(ErrorStatus.SEARCH_KEYWORD_TOO_LONG);
173+
}
174+
175+
if (page < 1) {
176+
throw new GeneralException(ErrorStatus.PAGE_NUMBER_INVALID);
177+
}
178+
179+
if (size < 1 || size > 100) {
180+
throw new GeneralException(ErrorStatus.PAGE_SIZE_INVALID);
181+
}
182+
}
183+
}

booklog/src/main/java/com/example/booklog/global/common/apiPayload/ApiResponse.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.example.booklog.global.auth.exception.AuthErrorCode;
44
import com.example.booklog.global.auth.exception.AuthSuccessCode;
5+
import com.example.booklog.global.common.apiPayload.code.BaseErrorCode;
56
import com.example.booklog.global.common.apiPayload.code.generalStatus.GeneralErrorCode;
67
import com.fasterxml.jackson.annotation.JsonInclude;
78
import io.swagger.v3.oas.annotations.media.Schema;
@@ -59,4 +60,14 @@ public static <T> ApiResponse<T> onFailure(GeneralErrorCode errorCode, T data) {
5960
public static <T> ApiResponse<T> onFailure(GeneralErrorCode errorCode) {
6061
return new ApiResponse<>(false, errorCode.getCode(), errorCode.getMessage(), null);
6162
}
63+
64+
// 실패 응답 (BaseErrorCode 사용 - ErrorStatus 등)
65+
public static <T> ApiResponse<T> onFailure(BaseErrorCode errorCode, T data) {
66+
return new ApiResponse<>(false, errorCode.getCode(), errorCode.getMessage(), data);
67+
}
68+
69+
// 실패 응답 (BaseErrorCode 사용, 데이터 없이)
70+
public static <T> ApiResponse<T> onFailure(BaseErrorCode errorCode) {
71+
return new ApiResponse<>(false, errorCode.getCode(), errorCode.getMessage(), null);
72+
}
6273
}

booklog/src/main/java/com/example/booklog/global/common/apiPayload/handler/GlobalExceptionHandler.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import com.example.booklog.global.auth.exception.AuthErrorCode;
44
import com.example.booklog.global.auth.exception.AuthException;
55
import com.example.booklog.global.common.apiPayload.ApiResponse;
6+
import com.example.booklog.global.common.apiPayload.code.BaseErrorCode;
67
import com.example.booklog.global.common.apiPayload.code.generalStatus.GeneralErrorCode;
8+
import com.example.booklog.global.common.apiPayload.exception.GeneralException;
79
import lombok.extern.slf4j.Slf4j;
810
import org.springframework.http.HttpStatus;
911
import org.springframework.http.ResponseEntity;
@@ -19,6 +21,22 @@
1921
@RestControllerAdvice
2022
public class GlobalExceptionHandler {
2123

24+
/**
25+
* GeneralException 처리 (프로젝트 공통 예외)
26+
*/
27+
@ExceptionHandler(GeneralException.class)
28+
public ResponseEntity<ApiResponse<Void>> handleGeneralException(GeneralException e) {
29+
log.error("GeneralException: {}", e.getCode().getMessage(), e);
30+
BaseErrorCode errorCode = e.getCode();
31+
32+
ApiResponse<Void> response = ApiResponse.onFailure(errorCode);
33+
34+
// ErrorCode의 HttpStatus 사용
35+
HttpStatus status = errorCode.getHttpStatus();
36+
37+
return ResponseEntity.status(status).body(response);
38+
}
39+
2240
/**
2341
* AuthException 처리
2442
*/

0 commit comments

Comments
 (0)