Skip to content

Commit

Permalink
refactor: 게스트가 별점 단일 조회 시 예외가 발생하지 않도록 수정 (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
pdohyung committed May 30, 2024
1 parent 056ca04 commit 5afa03b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ public RatingCreateResponse creatingRating(Long userId, RatingCreateServiceReque

public RatingGetOneResponse getRating(Long userId, String isbn) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND));
.orElse(null);

Book book = bookRepository.findById(isbn)
.orElseThrow(() -> new BusinessException(ErrorCode.BOOK_NOT_FOUND));

Rating rating = ratingRepository.findRatingByUserAndBook(user, book)
.orElse(null);

if (rating == null) {
if (rating == null || user == null) {
return RatingGetOneResponse.of(null, null, book.getIsbn());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,21 @@ void getRatingWithEmpty() {
assertThat(result.getIsbn()).isEqualTo(book.getIsbn());
}

@DisplayName("게스트가 책의 별점을 조회한다.")
@Test
void getRatingWithGuest() {
//given
Book book = bookRepository.save(createBook("제목1", "내용1", "1234"));

//when
RatingGetOneResponse result = ratingService.getRating(-1L, book.getIsbn());

//then
assertThat(result.getId()).isNull();
assertThat(result.getRating()).isNull();
assertThat(result.getIsbn()).isEqualTo(book.getIsbn());
}

@DisplayName("유저가 별점을 수정한다.")
@Test
void updateRating() {
Expand Down

0 comments on commit 5afa03b

Please sign in to comment.