Skip to content

Commit

Permalink
Merge pull request #26 from jisung-in/refector/22-refector-book-creat…
Browse files Browse the repository at this point in the history
…e-api

[Refactor] Book 등록 API 수정
  • Loading branch information
jwooo authored Mar 23, 2024
2 parents aa5a04f + b26843e commit 6827d03
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ public class BookCreateRequest {
@NotBlank(message = "책 내용 입력은 필수 입니다.")
private String contents;

@NotBlank(message = "책 경로 입력은 필수 입니다.")
private String url;

@NotBlank(message = "책 isbn 입력은 필수 입니다.")
private String isbn;

Expand All @@ -42,11 +39,10 @@ public class BookCreateRequest {
private String thumbnail;

@Builder
private BookCreateRequest(String title, String contents, String url, String isbn, String dateTime, String[] authors,
private BookCreateRequest(String title, String contents, String isbn, String dateTime, String[] authors,
String publisher, String thumbnail) {
this.title = title;
this.contents = contents;
this.url = url;
this.isbn = isbn;
this.dateTime = dateTime;
this.authors = authors;
Expand All @@ -58,7 +54,6 @@ public BookCreateServiceRequest toServiceRequest() {
return BookCreateServiceRequest.builder()
.title(title)
.contents(contents)
.url(url)
.isbn(isbn)
.dateTime(convertToLocalDateTime(dateTime))
.authors(convertToString(authors))
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/jisungin/application/book/BookService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.jisungin.domain.review.repository.ReviewRepository;
import com.jisungin.exception.BusinessException;
import com.jisungin.exception.ErrorCode;
import com.jisungin.infra.crawler.Crawler;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -16,6 +17,7 @@
@RequiredArgsConstructor
public class BookService {

private final Crawler crawler;
private final BookRepository bookRepository;
private final ReviewRepository reviewRepository;

Expand All @@ -34,7 +36,9 @@ public BookResponse createBook(BookCreateServiceRequest request) {
throw new BusinessException(ErrorCode.BOOK_ALREADY_EXIST);
}

return BookResponse.of(bookRepository.save(request.toEntity()), 0.0);
request.addCrawlingData(crawler.crawlBook(request.getIsbn()));

return BookResponse.of(bookRepository.save(request.toEntity()));
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jisungin.application.book.request;

import com.jisungin.domain.book.Book;
import com.jisungin.infra.crawler.CrawlingBook;
import java.time.LocalDateTime;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -12,22 +13,22 @@ public class BookCreateServiceRequest {

private String title;
private String contents;
private String url;
private String isbn;
private String authors;
private String publisher;
private String imageUrl;
private String thumbnail;
private LocalDateTime dateTime;

@Builder
private BookCreateServiceRequest(String title, String contents, String url, String isbn, String authors,
String publisher, String thumbnail, LocalDateTime dateTime) {
private BookCreateServiceRequest(String title, String contents, String isbn, String authors, String publisher,
String imageUrl, String thumbnail, LocalDateTime dateTime) {
this.title = title;
this.contents = contents;
this.url = url;
this.isbn = isbn;
this.authors = authors;
this.publisher = publisher;
this.imageUrl = imageUrl;
this.thumbnail = thumbnail;
this.dateTime = dateTime;
}
Expand All @@ -36,13 +37,18 @@ public Book toEntity() {
return Book.builder()
.title(title)
.content(contents)
.url(url)
.isbn(isbn)
.dateTime(dateTime)
.authors(authors)
.publisher(publisher)
.thumbnail(thumbnail)
.imageUrl(imageUrl)
.build();
}

public void addCrawlingData(CrawlingBook crawlingBook) {
this.imageUrl = crawlingBook.getImageUrl();
this.contents = crawlingBook.isBlankContent() ? this.contents : crawlingBook.getContent();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,40 @@ public class BookResponse {
private String content;
private String isbn;
private String publisher;
private String url;
private String imageUrl;
private String thumbnail;
private String[] authors;
private Double ratingAverage;
private LocalDateTime dateTime;

@Builder
private BookResponse(String title, String content, String isbn, String publisher, String url, String thumbnail,
private BookResponse(String title, String content, String isbn, String publisher, String thumbnail, String imageUrl,
String authors, Double ratingAverage, LocalDateTime dateTime) {
this.title = title;
this.content = content;
this.isbn = isbn;
this.publisher = publisher;
this.url = url;
this.imageUrl = imageUrl;
this.thumbnail = thumbnail;
this.authors = convertAuthorsToString(authors);
this.ratingAverage = parseRatingAverage(ratingAverage);
this.dateTime = dateTime;
}

public static BookResponse of(Book book) {
return BookResponse.builder()
.title(book.getTitle())
.content(book.getContent())
.authors(book.getAuthors())
.isbn(book.getIsbn())
.publisher(book.getPublisher())
.dateTime(book.getDateTime())
.thumbnail(book.getThumbnail())
.imageUrl(book.getImageUrl())
.ratingAverage(0.0)
.build();
}

public static BookResponse of(Book book, Double ratingAverage) {
return BookResponse.builder()
.title(book.getTitle())
Expand All @@ -42,8 +56,8 @@ public static BookResponse of(Book book, Double ratingAverage) {
.isbn(book.getIsbn())
.publisher(book.getPublisher())
.dateTime(book.getDateTime())
.url(book.getUrl())
.thumbnail(book.getThumbnail())
.imageUrl(book.getImageUrl())
.ratingAverage(ratingAverage)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public TalkRoomResponse createTalkRoom(TalkRoomCreateServiceRequest request, Str
readingStatus.stream().map(status -> TalkRoomRole.roleCreate(talkRoom, status))
.forEach(talkRoomRoleRepository::save);

return TalkRoomResponse.of(user.getName(), talkRoom, readingStatus, book.getUrl(), book.getTitle());
return TalkRoomResponse.of(user.getName(), talkRoom, readingStatus, book.getImageUrl(), book.getTitle());
}

public PageResponse<TalkRoomQueryResponse> getTalkRooms(TalkRoomSearchServiceRequest search) {
Expand All @@ -79,7 +79,7 @@ public TalkRoomResponse editTalkRoom(TalkRoomEditServiceRequest request, String
readingStatus.stream().map(status -> TalkRoomRole.roleCreate(talkRoom, status))
.forEach(talkRoomRoleRepository::save);

return TalkRoomResponse.of(user.getName(), talkRoom, readingStatus, talkRoom.getBook().getUrl(),
return TalkRoomResponse.of(user.getName(), talkRoom, readingStatus, talkRoom.getBook().getImageUrl(),
talkRoom.getBook().getTitle());
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/jisungin/domain/book/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public class Book extends BaseEntity {
@Column(name = "book_publisher")
private String publisher;

@Column(name = "book_url")
private String url;
@Column(name = "book_image")
private String imageUrl;

@Column(name = "book_thumbnail")
private String thumbnail;
Expand All @@ -44,14 +44,14 @@ public class Book extends BaseEntity {


@Builder
private Book(String isbn, String title, String content, String authors, String publisher, String url,
private Book(String isbn, String title, String content, String authors, String publisher, String imageUrl,
String thumbnail, LocalDateTime dateTime) {
this.isbn = isbn;
this.title = title;
this.content = content;
this.authors = authors;
this.publisher = publisher;
this.url = url;
this.imageUrl = imageUrl;
this.thumbnail = thumbnail;
this.dateTime = dateTime;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private List<TalkRoomQueryResponse> findTalkRoom(TalkRoomSearchServiceRequest se
talkRoom.title,
talkRoom.content,
book.title,
book.url.as("bookImage")
book.imageUrl.as("bookImage")
))
.from(talkRoom)
.join(talkRoom.user, user)
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/jisungin/infra/crawler/CrawlingBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ public static CrawlingBook of(String imageUrl, String content) {
.build();
}

public boolean isBlankContent() {
return this.content.isBlank();
}

}
35 changes: 1 addition & 34 deletions src/test/java/com/jisungin/api/book/BookControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ public void createBook() throws Exception {
BookCreateRequest request = BookCreateRequest.builder()
.title("도서 정보")
.contents("도서 내용")
.url("도서 URL")
.isbn("도서 isbn")
.dateTime("2024-03-15T00:00:00.000+09:00")
.authors(new String[]{"도서 저자1", "도서 저자2"})
Expand Down Expand Up @@ -106,7 +105,6 @@ public void createBookWithDuplicateIsbn() throws Exception {
BookCreateRequest request = BookCreateRequest.builder()
.title("도서 정보")
.contents("도서 내용")
.url("도서 URL")
.isbn("도서 isbn")
.dateTime("2024-03-15T00:00:00.000+09:00")
.authors(new String[]{"도서 저자1", "도서 저자2"})
Expand All @@ -130,7 +128,6 @@ public void createBookWithNonTitle() throws Exception {
// given
BookCreateRequest request = BookCreateRequest.builder()
.contents("도서 내용")
.url("도서 URL")
.isbn("도서 isbn")
.dateTime("2024-03-15T00:00:00.000+09:00")
.authors(new String[]{"도서 저자1", "도서 저자2"})
Expand All @@ -154,7 +151,6 @@ public void createBookWithNonContents() throws Exception {
// given
BookCreateRequest request = BookCreateRequest.builder()
.title("도서 정보")
.url("도서 URL")
.isbn("도서 isbn")
.dateTime("2024-03-15T00:00:00.000+09:00")
.authors(new String[]{"도서 저자1", "도서 저자2"})
Expand All @@ -172,38 +168,13 @@ public void createBookWithNonContents() throws Exception {
.andExpect(jsonPath("$.message").value("책 내용 입력은 필수 입니다."));
}

@Test
@DisplayName("신규 도서 등록 시 책 경로는 필수이어야 한다.")
public void createBookWithNonUrl() throws Exception {
// given
BookCreateRequest request = BookCreateRequest.builder()
.title("도서 정보")
.contents("도서 내용")
.isbn("도서 isbn")
.dateTime("2024-03-15T00:00:00.000+09:00")
.authors(new String[]{"도서 저자1", "도서 저자2"})
.publisher("도서 출판사")
.thumbnail("도서 썸네일")
.build();

// when // then
mockMvc.perform(post("/v1/books")
.content(objectMapper.writeValueAsString(request))
.contentType(APPLICATION_JSON))
.andDo(print())
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code").value("400"))
.andExpect(jsonPath("$.message").value("책 경로 입력은 필수 입니다."));
}

@Test
@DisplayName("신규 도서 등록 시 책 isbn 입력은 필수이어야 한다.")
public void createBookWithNonIsbn() throws Exception {
// given
BookCreateRequest request = BookCreateRequest.builder()
.title("도서 정보")
.contents("도서 내용")
.url("도서 URL")
.dateTime("2024-03-15T00:00:00.000+09:00")
.authors(new String[]{"도서 저자1", "도서 저자2"})
.publisher("도서 출판사")
Expand All @@ -227,7 +198,6 @@ public void createBookWithNonDateTime() throws Exception {
BookCreateRequest request = BookCreateRequest.builder()
.title("도서 정보")
.contents("도서 내용")
.url("도서 URL")
.isbn("도서 isbn")
.authors(new String[]{"도서 저자1", "도서 저자2"})
.publisher("도서 출판사")
Expand All @@ -251,7 +221,6 @@ public void createBookWithAuthors() throws Exception {
BookCreateRequest request = BookCreateRequest.builder()
.title("도서 정보")
.contents("도서 내용")
.url("도서 URL")
.isbn("도서 isbn")
.dateTime("2024-03-15T00:00:00.000+09:00")
.publisher("도서 출판사")
Expand All @@ -275,7 +244,6 @@ public void createBookWithPublisher() throws Exception {
BookCreateRequest request = BookCreateRequest.builder()
.title("도서 정보")
.contents("도서 내용")
.url("도서 URL")
.isbn("도서 isbn")
.dateTime("2024-03-15T00:00:00.000+09:00")
.authors(new String[]{"도서 저자1", "도서 저자2"})
Expand All @@ -302,7 +270,6 @@ public void createBookWithThumbnail() throws Exception {
BookCreateRequest request = BookCreateRequest.builder()
.title("도서 정보")
.contents("도서 내용")
.url("도서 URL")
.isbn("도서 isbn")
.dateTime("2024-03-15T00:00:00.000+09:00")
.authors(new String[]{"도서 저자1", "도서 저자2"})
Expand All @@ -323,7 +290,7 @@ private static Book create() {
return Book.builder()
.title("도서 정보")
.content("도서 내용")
.url("도서 URL")
.imageUrl("도서 URL")
.isbn("도서 isbn")
.dateTime(LocalDateTime.now())
.authors("도서 저자1, 도서 저자2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ private static Book createBook() {
.isbn("11111")
.publisher("publisher")
.dateTime(LocalDateTime.now())
.url("www")
.imageUrl("www")
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ private static Book createBook() {
.isbn("11111")
.publisher("publisher")
.dateTime(LocalDateTime.now())
.url("www")
.imageUrl("www")
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private static Book createBook() {
.isbn("11111")
.publisher("publisher")
.dateTime(LocalDateTime.now())
.url("www")
.imageUrl("www")
.build();
}

Expand Down

0 comments on commit 6827d03

Please sign in to comment.