Skip to content

Commit

Permalink
Feat: 책 검색시 DB 검색을 제외하고 알라딘 api 활용으로 대체하기 (#77)
Browse files Browse the repository at this point in the history
* #60 - feat: 책 검색 응답 시 BookApiDto 를 BookApiResponse 로 변환 후 응답

* #60 - feat: 기존 외부 검색 API를 검색 API로 전환하고 기존 검색 API는 Deprecated 처리
  • Loading branch information
morenow98 authored Feb 6, 2024
1 parent 2713518 commit 55d538d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class BookController {

private final BookService bookService;

@GetMapping("/external/search")
@GetMapping("/search")
public ResponseEntity<BookApiListResponse> searchExternal(
@RequestParam String keyword,
@RequestParam Integer start) {
Expand All @@ -36,10 +36,11 @@ public ResponseEntity<BookApiListResponse> searchExternal(
public ResponseEntity<BookApiResponse> getExternal(
@IsValidIsbn @RequestParam String isbn13
) {
return ResponseEntity.ok(BookApiResponse.fromBookApiDto(bookService.getExternal(isbn13)));
return ResponseEntity.ok(BookApiResponse.from(bookService.getExternal(isbn13)));
}

@GetMapping("/search")
@Deprecated
@GetMapping("/deprecated/search")
public ResponseEntity<BookListResponse> search(
@RequestParam String keyword,
@Parameter(hidden = true) @PageableDefault(sort = "pubDate", direction = Sort.Direction.DESC) Pageable pageable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package cotato.bookitlist.book.dto.response;

import cotato.bookitlist.book.dto.BookApiDto;

import java.util.List;

public record BookApiListResponse(
int totalResults,
int startIndex,
int itemsPerPage,
List<BookApiDto> bookApiList
List<BookApiResponse> bookApiList
) {

public static BookApiListResponse of(int totalResults, int startIndex, int itemsPerPage, List<BookApiDto> bookApiDto) {
return new BookApiListResponse(totalResults, startIndex, itemsPerPage, bookApiDto);
public static BookApiListResponse of(int totalResults, int startIndex, int itemsPerPage, List<BookApiResponse> bookApiList) {
return new BookApiListResponse(totalResults, startIndex, itemsPerPage, bookApiList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,21 @@ public record BookApiResponse(
String link,
String isbn13,
Integer price,
String cover
String cover,
boolean isBook
) {

public static BookApiResponse fromBookApiDto(BookApiDto dto) {
return new BookApiResponse(dto.title(), dto.author(), dto.publisher(), dto.pubDate(), dto.description(), dto.link(), dto.isbn13(), dto.price(), dto.cover());
public static BookApiResponse from(BookApiDto dto) {
return new BookApiResponse(
dto.title(),
dto.author(),
dto.publisher(),
dto.pubDate(),
dto.description(),
dto.link(),
dto.isbn13(),
dto.price(),
dto.cover(),
!dto.isbn13().isBlank());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cotato.bookitlist.book.dto.BookApiDto;
import cotato.bookitlist.book.dto.response.BookApiListResponse;
import cotato.bookitlist.book.dto.response.BookApiResponse;
import lombok.RequiredArgsConstructor;
import org.json.JSONArray;
import org.json.JSONObject;
Expand Down Expand Up @@ -52,7 +53,8 @@ public BookApiListResponse findListByKeyWordAndApi(String keyword, int start) {
bookApiCacheService.saveBookApiCache(bookApiDto);
}

return BookApiListResponse.of(totalResults, startIndex, itemsPerPage, bookApiDtoList);
return BookApiListResponse.of(totalResults, startIndex, itemsPerPage,
bookApiDtoList.stream().map(BookApiResponse::from).toList());
}

public BookApiDto findByIsbn13(String isbn13) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public BookDto getBookByIsbn13(String isbn13) {
);
}

@Deprecated
public Page<Book> search(String keyword, Pageable pageable) {
return bookRepository.findAllByKeyword(keyword, pageable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ class BookControllerTest {
ObjectMapper objectMapper;

@Test
@DisplayName("[Api] keyword가 주어지면 응답을 준다.")
void givenKeyword_whenExternalSearching_thenReturnBookApiResponse() throws Exception {
@DisplayName("[Api] keyword 가 주어지면 응답을 준다.")
void givenKeyword_whenSearching_thenReturnBookApiResponse() throws Exception {
//given
String keyword = "aladdin";

//when&then
mockMvc.perform(get("/books/external/search")
mockMvc.perform(get("/books/search")
.param("keyword", keyword)
.param("start", "1")
)
Expand All @@ -50,23 +50,23 @@ void givenKeyword_whenExternalSearching_thenReturnBookApiResponse() throws Excep

@Test
@DisplayName("[Api] start만 주어지면 에러를 반환한다.")
void givenStart_whenExternalSearching_thenReturnErrorResponse() throws Exception {
void givenStart_whenSearching_thenReturnErrorResponse() throws Exception {
//given

//when&then
mockMvc.perform(get("/books/external/search")
mockMvc.perform(get("/books/search")
.param("start", "1"))
.andExpect(status().isBadRequest())
;
}

@Test
@DisplayName("[Api] 아무것도 주어지지 않으면 에러를 반환한다.")
void givenNothing_whenExternalSearching_thenReturnErrorResponse() throws Exception {
void givenNothing_whenSearching_thenReturnErrorResponse() throws Exception {
//given

//when&then
mockMvc.perform(get("/books/external/search")
mockMvc.perform(get("/books/search")
)
.andExpect(status().isBadRequest())
;
Expand Down Expand Up @@ -216,25 +216,6 @@ void givenUnRegisteredIsbn13_whenSearchingBook_thenReturnBookResponseFromApi() t
;
}

@Test
@DisplayName("[DB] keyword이 주어지면 응답을 준다.")
void givenKeyword_whenSearchingBook_thenReturnBookResponse() throws Exception {
//given
String keyword = "aladdin";

//when&then
mockMvc.perform(get("/books/search")
.param("keyword", keyword)
)
.andExpect(status().isOk())
.andExpect(jsonPath("$.totalResults").value(3))
.andExpect(jsonPath("$.startIndex").value(0))
.andExpect(jsonPath("$.itemsPerPage").value(3))
.andExpect(jsonPath("$.bookList").exists())
.andDo(print())
;
}

@Test
@DisplayName("[DB] id를 이용해 책을 찾는다.")
void givenBookId_whenGettingBook_thenReturnBookResponse() throws Exception {
Expand Down

0 comments on commit 55d538d

Please sign in to comment.