Skip to content

Commit

Permalink
Feat: 책 상세 API 접근 시 DB에 없는 책도 가능한 기능 추가 (#68)
Browse files Browse the repository at this point in the history
* #53 - feat: 형식은 올바르지만 실제로는 없는 isbn13 입력 시 예외처리

* #53 - feat: 책 상세 API 중 데이터베이스에 존재하지 않을 때 에러를 반환하지 않고 외부 API 통신을 통해 보여주도록 변경

관련된 테스트 코드 수정
DB에 없는 책일 경우 bookId 를 0으로 반환

* #53 - feat: 책 상세 조회 시 DB 조회 -> Redis 조회 -> API 통신으로 데이터를 얻어오게 변경

* #53 - refactor: 변경된 메소드의 메소드명 수정 누락분 변경
  • Loading branch information
morenow98 authored Feb 4, 2024
1 parent b007f2f commit 3486266
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
25 changes: 22 additions & 3 deletions src/main/java/cotato/bookitlist/book/dto/BookDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,28 @@ public record BookDto(
) {

public static BookDto from(Book entity) {
return new BookDto(entity.getId(), entity.getTitle(), entity.getAuthor(), entity.getPublisher(), entity.getPubDate(), entity.getDescription(), entity.getLink(), entity.getIsbn13(), entity.getPrice(), entity.getCover());

return new BookDto(entity.getId(),
entity.getTitle(),
entity.getAuthor(),
entity.getPublisher(),
entity.getPubDate(),
entity.getDescription(),
entity.getLink(),
entity.getIsbn13(),
entity.getPrice(),
entity.getCover());
}


public static BookDto from(BookApiDto bookApiDto) {
return new BookDto(0L,
bookApiDto.title(),
bookApiDto.author(),
bookApiDto.publisher(),
bookApiDto.pubDate(),
bookApiDto.description(),
bookApiDto.link(),
bookApiDto.isbn13(),
bookApiDto.price(),
bookApiDto.cover());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public BookApiListResponse findListByKeyWordAndApi(String keyword, int start) {
public BookApiDto findByIsbn13(String isbn13) {
JSONObject json = new JSONObject(aladinComponent.findByIsbn13(aladinKey, isbn13, "ISBN13", "JS", 20131101));

if (json.has("errorMessage")) {
throw new IllegalArgumentException("존재하지 않는 isbn13 입니다.");
}

JSONObject item = json.getJSONArray("item").getJSONObject(0);

BookApiDto bookApiDto = BookApiDto.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ public BookApiDto getExternal(String isbn13) {

public BookDto getBookByIsbn13(String isbn13) {
return bookRepository.findByIsbn13(isbn13).map(BookDto::from)
.orElseThrow(() -> new EntityNotFoundException("등록되지 않은 isbn13입니다."));
.orElseGet(() -> bookApiCacheService.findBookApiCacheByIsbn13(isbn13)
.map(BookApiCache::getBookApiDto)
.map(BookDto::from)
.orElseGet(() -> BookDto.from(getExternal(isbn13)))
);
}

public Page<Book> search(String keyword, Pageable pageable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,17 +195,24 @@ void givenIsbn13_whenSearchingBook_thenReturnBookResponse() throws Exception {
}

@Test
@DisplayName("[DB] 등록되지 않은 isbn13이 주어지면 에러를 응답한다.")
void givenUnRegisteredIsbn13_whenSearchingBook_thenReturnErrorResponse() throws Exception {
@DisplayName("[API] 등록되지 않은 올바른 형식의 isbn13이 주어지면 API 통신을 통해 응답한다.")
void givenUnRegisteredIsbn13_whenSearchingBook_thenReturnBookResponseFromApi() throws Exception {
//given
String isbn13 = "9788966262281";

//when&then
mockMvc.perform(get("/books")
.param("isbn13", isbn13)
)
.andExpect(status().isNotFound())
.andExpect(jsonPath("$.message").value("등록되지 않은 isbn13입니다."))
.andExpect(jsonPath("$.title").exists())
.andExpect(jsonPath("$.author").exists())
.andExpect(jsonPath("$.publisher").exists())
.andExpect(jsonPath("$.pubDate").exists())
.andExpect(jsonPath("$.description").exists())
.andExpect(jsonPath("$.link").exists())
.andExpect(jsonPath("$.isbn13").exists())
.andExpect(jsonPath("$.price").exists())
.andExpect(jsonPath("$.cover").exists())
;
}

Expand Down

0 comments on commit 3486266

Please sign in to comment.