-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: bookshelf service with component
- Loading branch information
Showing
12 changed files
with
281 additions
and
251 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
src/main/java/toy/bookchat/bookchat/domain/book/service/BookReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package toy.bookchat.bookchat.domain.book.service; | ||
|
||
import java.time.LocalDate; | ||
import org.springframework.stereotype.Component; | ||
import toy.bookchat.bookchat.domain.book.Book; | ||
import toy.bookchat.bookchat.domain.book.repository.BookRepository; | ||
|
||
@Component | ||
public class BookReader { | ||
|
||
private final BookRepository bookRepository; | ||
|
||
public BookReader(BookRepository bookRepository) { | ||
this.bookRepository = bookRepository; | ||
} | ||
|
||
public Book readBook(String isbn, LocalDate publishAt, Book book) { | ||
return bookRepository.findByIsbnAndPublishAt(isbn, publishAt) | ||
.orElseGet(() -> bookRepository.save(book)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/java/toy/bookchat/bookchat/domain/user/service/UserReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package toy.bookchat.bookchat.domain.user.service; | ||
|
||
import org.springframework.stereotype.Component; | ||
import toy.bookchat.bookchat.domain.user.User; | ||
import toy.bookchat.bookchat.domain.user.repository.UserRepository; | ||
import toy.bookchat.bookchat.exception.notfound.user.UserNotFoundException; | ||
|
||
@Component | ||
public class UserReader { | ||
|
||
private final UserRepository userRepository; | ||
|
||
public UserReader(UserRepository userRepository) { | ||
this.userRepository = userRepository; | ||
} | ||
|
||
public User readUser(Long userId) { | ||
return userRepository.findById(userId).orElseThrow(UserNotFoundException::new); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/test/java/toy/bookchat/bookchat/domain/book/service/BookReaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package toy.bookchat.bookchat.domain.book.service; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.BDDMockito.given; | ||
|
||
import java.time.LocalDate; | ||
import java.util.Optional; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import toy.bookchat.bookchat.domain.book.Book; | ||
import toy.bookchat.bookchat.domain.book.repository.BookRepository; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class BookReaderTest { | ||
|
||
@Mock | ||
private BookRepository bookRepository; | ||
@InjectMocks | ||
private BookReader bookReader; | ||
|
||
@Test | ||
void isbn_출판일과_일치하는_책이있다면_조회한다() throws Exception { | ||
Book book = Book.builder().build(); | ||
given(bookRepository.findByIsbnAndPublishAt(any(), any())).willReturn(Optional.of(book)); | ||
Book readBook = bookReader.readBook("G5J5X8U", LocalDate.now(), null); | ||
|
||
assertThat(readBook).isEqualTo(book); | ||
} | ||
|
||
@Test | ||
void isbn_출판일과_일치하는_책이없다면_신규등록후_반환한다() throws Exception { | ||
Book book = Book.builder().build(); | ||
given(bookRepository.save(any())).willReturn(book); | ||
Book readBook = bookReader.readBook("G5J5X8U", LocalDate.now(), null); | ||
|
||
assertThat(readBook).isEqualTo(book); | ||
} | ||
} |
Oops, something went wrong.