Skip to content

Commit

Permalink
fix : likeCommentIfNew는 Optional을 리턴하도록 수정한다.
Browse files Browse the repository at this point in the history
  • Loading branch information
60jong committed Feb 29, 2024
1 parent 15004e0 commit a29355b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

import static life.offonoff.ab.application.service.common.LengthInfo.COMMENT_CONTENT;

@RequiredArgsConstructor
Expand Down Expand Up @@ -138,18 +140,19 @@ public CommentReactionResponse likeCommentForMember(final Long memberId, final L
private CommentReactionResponse doLike(Member liker, Comment comment) {

liker.cancelHateIfExists(comment);
LikedComment likedComment = liker.likeCommentIfNew(comment);
Optional<LikedComment> optionalLikedComment = liker.likeCommentIfNew(comment);

publishCommentLikedEventIfNotNull(likedComment);
publishCommentLikedEventIfPresent(optionalLikedComment);

return new CommentReactionResponse(comment.getLikeCount(), comment.getHateCount(), true, false);
}

private void publishCommentLikedEventIfNotNull(LikedComment likedComment) {
if (likedComment == null) {
return;
private void publishCommentLikedEventIfPresent(Optional<LikedComment> optional) {
if (optional.isPresent()) {
LikedComment likedComment = optional.get();

eventPublisher.publishEvent(new CommentLikedEvent(likedComment);
}
eventPublisher.publishEvent(new CommentLikedEvent(likedComment));
}

private CommentReactionResponse cancelLike(Member liker, Comment comment) {
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/life/offonoff/ab/domain/member/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand Down Expand Up @@ -113,16 +114,17 @@ public void addComment(Comment comment) {
comments.add(comment);
}

public LikedComment likeCommentIfNew(Comment comment) {
public Optional<LikedComment> likeCommentIfNew(Comment comment) {
if (likeAlready(comment)) {
return null;
return Optional.empty();
}

LikedComment likedComment = new LikedComment(this, comment);
comment.increaseLikeCount();

likedComments.add(likedComment);
return likedComment;

return Optional.of(likedComment);
}

public void hateCommentIfNew(Comment comment) {
Expand Down

0 comments on commit a29355b

Please sign in to comment.