Open berryberrybin opened 1 year ago
private Boolean viewerHasLiked
: Boolean 타입의 필드 추가
@Setter
@Getter
@ToString
@EqualsAndHashCode
@AllArgsConstructor
public class PostInfo {
private UUID userId; // 글쓴이 아이디
private UUID postId; // 게시글 아이디
private String boardName;
private String title;
private String content;
private Integer likeCount;
private Boolean viewerHasLiked; // 좋아요 눌렀는지 여부 - 추가
private Integer viewCount;
private Integer commentCount;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
private List<String> tags;
private List<CommentInfo> comments;
}
PostInfo of(Post post );
➔ PostInfo of(Post post, boolean viewerHasLiked);
변경@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);
}
로그인유저가 해당 게시글을 좋아요 눌렀는지 확인하는 코드 추가
@Slf4j
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class PostQueryService {
private final PostRepository postRepository;
private final BoardRepository boardRepository;
private final PostLikeRepository postLikeRepository;
public PostInfo retrievePost(PostQuery.RetrievePostByPostId retrievePostQuery) {
UUID postId = retrievePostQuery.getPostId();
Post post = postRepository.findById(postId)
.orElseThrow(() -> new BusinessException(ErrorCode.NOT_FOUND, "post.byId", List.of(postId.toString())));
// 변경 코드
UUID loginUserId = retrievePostQuery.getLoginUserId();
boolean viewerHasLiked = postLikeRepository.existsByUserIdAndPostId(loginUserId, postId);
return PostInfoMapper.INSTANCE.of(post, viewerHasLiked);
}
}
List<CommentInfo> childComments;
를 갖고 있기 때문에 Post와 같이 viewerHasLiked
1개의 boolean 값을 넘겨서 해결 할 수가 없음@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;
}
}
@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())));
}
}
기능