Skip to content

Commit

Permalink
refactor: 분기처리 orElseGet 으로 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
wugawuga committed Aug 3, 2023
1 parent 6b982fe commit e27c350
Showing 1 changed file with 9 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ReviewFavorite> 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)
Expand Down

0 comments on commit e27c350

Please sign in to comment.