berryberrybin / board-project

0 stars 0 forks source link

로그인 유저의 좋아요 체크 기능 리팩토링 #21

Open berryberrybin opened 1 year ago

berryberrybin commented 1 year ago

기능

berryberrybin commented 1 year ago

Post의 로그인 유저 좋아요 체크 기능

PostInfo

PostInfoMapper

@Mapper
public interface PostInfoMapper {
    PostInfoMapper INSTANCE = Mappers.getMapper(PostInfoMapper.class);

    @Mapping(target = "userId", source = "post.user.id")
    @Mapping(target = "postId", source = "post.id")
    @Mapping(target = "boardName", source = "post.board.name")
    @Mapping(target = "commentCount", expression = "java(post.getComments().size())")
    @Mapping(target = "tags", expression = "java(post.getTagNames())")
    PostInfo of(Post post, boolean viewerHasLiked);

    default List<CommentInfo> of(List<Comment> comments) {
        Map<Comment, CommentInfo> commentMap = new LinkedHashMap<>();

        for (Comment comment : comments) {
            if (comment.getParentComment() == null) {
                commentMap.put(comment, of(comment));
                continue;
            }
            Comment parentComment = comment.getParentComment();
            if(commentMap.containsKey(parentComment)) {
                commentMap.get(parentComment).getChildComments().add(of(comment));
            }
        }

        return new ArrayList<>(commentMap.values());
    }

    @Mapping(target = "commentId", source = "comment.id")
    @Mapping(target = "nickname", source = "comment.user.nickname")
    @Mapping(target = "parentCommentId", source = "comment.parentComment.id")
    CommentInfo of(Comment comment);
}

PostQueryService

berryberrybin commented 1 year ago

Comment의 로그인 유저 좋아요 체크 기능

CommentInfo

@Getter
@Setter
@EqualsAndHashCode
@NoArgsConstructor
public class CommentInfo {
    private UUID commentId;
    private String content;
    private LocalDateTime createdAt;
    private Boolean isDeleted;
    private String nickname;
    private int commentLikeCount;
    private UUID parentCommentId;
    private Boolean viewerHasLiked; // 추가
    private List<CommentInfo> childComments;

    public void blindDescription(String content) {
        this.content = content;
    }

}

CommentQueryService

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class CommentQueryService {

    private final UserRepository userRepository;
    private final PostRepository postRepository;
    private final CommentRepository commentRepository;
    private final CommentLikeRepository commentLikeRepository;

    public CommentInfo retrieveComment(CommentQuery.RetrieveCommentByCommentId retrieveCommentByCommentId) {

        UUID loginUserId = retrieveCommentByCommentId.getLoginUserId();
        findUser(loginUserId);

        UUID commentId = retrieveCommentByCommentId.getCommentId();
        Comment comment = findComment(commentId);

        CommentInfo commentInfo = CommentInfoMapper.INSTANCE.CommentToCommentInfo(comment);

        // if (commentInfo.getIsDeleted()) {
        //  commentInfo.blindDescription("삭제된 내용입니다");
        // }
        //
        // if (commentInfo.getChildComments() != null) {
        //  for (CommentInfo childCommentInfo : commentInfo.getChildComments()) {
        //      if (childCommentInfo.getIsDeleted()) {
        //          childCommentInfo.blindDescription("삭제된 내용입니다");
        //      }
        //  }
        // }

        putCommentInfoExtra(commentInfo,loginUserId); // 수정된 코드 

        return commentInfo;
    }

        // 재귀를 통해 무한댓글이더라도 (1)삭제된 댓글 내용 처리 및 (2) 로그인 유저의 좋아요 눌렀는지 여부 확인 할 수 있도록 변경함
    private void putCommentInfoExtra(CommentInfo commentInfo, UUID loginUserId){
        commentInfo.setViewerHasLiked(commentLikeRepository.existsByUserIdAndCommentId(loginUserId, commentInfo.getCommentId()));

        if (commentInfo.getIsDeleted()) {
            commentInfo.blindDescription("삭제된 내용입니다");
        }
        if(commentInfo.getChildComments() != null ){
            for(CommentInfo childCommentInfo : commentInfo.getChildComments()){
                putCommentInfoExtra(childCommentInfo, loginUserId);
            }
        }
    }
    private User findUser(UUID userId) {
        return userRepository.findById(userId)
            .orElseThrow(() -> new BusinessException(ErrorCode.NOT_FOUND, "user.byId", List.of(userId.toString())));
    }

    private Comment findComment(UUID commentId){
        return commentRepository.findById(commentId)
            .orElseThrow(
                () -> new BusinessException(ErrorCode.NOT_FOUND, "comment.byId", List.of(commentId.toString())));
    }

}