diff --git a/tecobrary-core-api/src/main/java/com/woowacourse/tecobrary/web/wishbook/exception/WishBookAlreadyExistException.java b/tecobrary-core-api/src/main/java/com/woowacourse/tecobrary/web/wishbook/exception/WishBookAlreadyExistException.java index a2f07d9..e931782 100644 --- a/tecobrary-core-api/src/main/java/com/woowacourse/tecobrary/web/wishbook/exception/WishBookAlreadyExistException.java +++ b/tecobrary-core-api/src/main/java/com/woowacourse/tecobrary/web/wishbook/exception/WishBookAlreadyExistException.java @@ -5,12 +5,10 @@ @Getter public class WishBookAlreadyExistException extends RuntimeException { - private static final String DUPLICATED_WISH_BOOK_ISBN_EXCEPTION_MESSAGE = "이미 희망도서에 등록되어 있습니다."; - private final String isbn; - public WishBookAlreadyExistException(String isbn) { - super(DUPLICATED_WISH_BOOK_ISBN_EXCEPTION_MESSAGE); + public WishBookAlreadyExistException(String isbn, String message) { + super(message); this.isbn = isbn; } } diff --git a/tecobrary-core-api/src/main/java/com/woowacourse/tecobrary/web/wishbook/service/WishBookFacade.java b/tecobrary-core-api/src/main/java/com/woowacourse/tecobrary/web/wishbook/service/WishBookFacade.java index f728732..c8724c2 100644 --- a/tecobrary-core-api/src/main/java/com/woowacourse/tecobrary/web/wishbook/service/WishBookFacade.java +++ b/tecobrary-core-api/src/main/java/com/woowacourse/tecobrary/web/wishbook/service/WishBookFacade.java @@ -1,5 +1,6 @@ package com.woowacourse.tecobrary.web.wishbook.service; +import com.woowacourse.tecobrary.domain.librarybook.repository.LibraryBookRepository; import com.woowacourse.tecobrary.domain.user.entity.User; import com.woowacourse.tecobrary.domain.user.repository.UserRepository; import com.woowacourse.tecobrary.web.tecorvis.api.SlackBotService; @@ -18,7 +19,11 @@ @Service public class WishBookFacade { + private static final String DUPLICATED_WISH_BOOK_ISBN_EXCEPTION_MESSAGE = "이미 희망도서에 등록되어 있습니다."; + private static final String ALREADY_ENROLLED_BOOK_EXCEPTION_MESSAGE = "이미 등록된 장서 입니다."; + private final WishBookRepository wishBookRepository; + private final LibraryBookRepository libraryBookRepository; private final UserRepository userRepository; private final WishBookConverter wishBookConverter; private final SlackBotService slackBotService; @@ -30,6 +35,10 @@ public WishBookInfoDto createWishBook(final WishBookInfoDto wishBookInfoDto) { User user = userRepository.findById(wishBookInfoDto.getUserId()) .orElseThrow(() -> new WishBookRequestUserNotFoundException(wishBookInfoDto.getUserId())); + if (libraryBookRepository.existsByIsbn(wishBookInfoDto.getIsbn())) { + throw new WishBookAlreadyExistException(wishBookInfoDto.getIsbn(), ALREADY_ENROLLED_BOOK_EXCEPTION_MESSAGE); + } + WishBook wishBook = wishBookRepository.save(WishBook.builder() .title(wishBookInfoDto.getTitle()) .image(wishBookInfoDto.getImage()) @@ -53,7 +62,7 @@ public WishBookInfoDto createWishBook(final WishBookInfoDto wishBookInfoDto) { private void checkDuplicated(final WishBookInfoDto wishBookInfoDto) { if (wishBookRepository.existsByIsbn(wishBookInfoDto.getIsbn())) { - throw new WishBookAlreadyExistException(wishBookInfoDto.getIsbn()); + throw new WishBookAlreadyExistException(wishBookInfoDto.getIsbn(), DUPLICATED_WISH_BOOK_ISBN_EXCEPTION_MESSAGE); } } }