diff --git a/backend/src/main/java/com/funeat/review/application/ReviewService.java b/backend/src/main/java/com/funeat/review/application/ReviewService.java index bbd9c0611..aad5de8b5 100644 --- a/backend/src/main/java/com/funeat/review/application/ReviewService.java +++ b/backend/src/main/java/com/funeat/review/application/ReviewService.java @@ -21,7 +21,6 @@ import com.funeat.tag.persistence.TagRepository; import java.util.List; import java.util.Objects; -import java.util.Optional; import java.util.stream.Collectors; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; @@ -94,20 +93,19 @@ public void likeReview(final Long reviewId, final Long memberId, final ReviewFav final Review findReview = reviewRepository.findById(reviewId) .orElseThrow(IllegalArgumentException::new); - final Optional findReviewFavorite = reviewFavoriteRepository.findByMemberAndReview(findMember, - findReview); + final ReviewFavorite savedReviewFavorite = reviewFavoriteRepository.findByMemberAndReview(findMember, + findReview).orElseGet(() -> saveReviewFavorite(findMember, findReview, request.getFavorite())); - if (findReviewFavorite.isPresent()) { - findReviewFavorite.get().updateChecked(request.getFavorite()); - return; - } - - final ReviewFavorite newReviewFavorite = ReviewFavorite.createReviewFavoriteByMemberAndReview(findMember, - findReview, request.getFavorite()); - final ReviewFavorite savedReviewFavorite = reviewFavoriteRepository.save(newReviewFavorite); savedReviewFavorite.updateChecked(request.getFavorite()); } + private ReviewFavorite saveReviewFavorite(final Member member, final Review review, final Boolean favorite) { + final ReviewFavorite reviewFavorite = ReviewFavorite.createReviewFavoriteByMemberAndReview(member, review, + favorite); + + return reviewFavoriteRepository.save(reviewFavorite); + } + public SortingReviewsResponse sortingReviews(final Long productId, final Pageable pageable) { final Product product = productRepository.findById(productId)