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 910e48144..bbd9c0611 100644 --- a/backend/src/main/java/com/funeat/review/application/ReviewService.java +++ b/backend/src/main/java/com/funeat/review/application/ReviewService.java @@ -21,6 +21,7 @@ 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; @@ -93,13 +94,18 @@ public void likeReview(final Long reviewId, final Long memberId, final ReviewFav final Review findReview = reviewRepository.findById(reviewId) .orElseThrow(IllegalArgumentException::new); - final ReviewFavorite reviewFavorite = ReviewFavorite.createReviewFavoriteByMemberAndReview(findMember, - findReview, request.getFavorite()); + final Optional findReviewFavorite = reviewFavoriteRepository.findByMemberAndReview(findMember, + findReview); - final ReviewFavorite findReviewFavorite = reviewFavoriteRepository.findByMemberAndReview(findMember, findReview) - .orElse(reviewFavoriteRepository.save(reviewFavorite)); + if (findReviewFavorite.isPresent()) { + findReviewFavorite.get().updateChecked(request.getFavorite()); + return; + } - findReviewFavorite.updateChecked(request.getFavorite()); + final ReviewFavorite newReviewFavorite = ReviewFavorite.createReviewFavoriteByMemberAndReview(findMember, + findReview, request.getFavorite()); + final ReviewFavorite savedReviewFavorite = reviewFavoriteRepository.save(newReviewFavorite); + savedReviewFavorite.updateChecked(request.getFavorite()); } public SortingReviewsResponse sortingReviews(final Long productId,