Skip to content

Commit

Permalink
Merge pull request #40 from Leets-Official/fix/#39/댓글-게시글-리스폰스-수정
Browse files Browse the repository at this point in the history
[fix] 댓글 게시글 리스폰스 수정
  • Loading branch information
dyk-im authored Nov 13, 2024
2 parents c751d01 + b119205 commit 075fee6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,32 +98,4 @@ public static ArticleResponseDto from(Article article, boolean isOwner, boolean
.createdAt(article.getCreatedAt())
.build();
}

public static ArticleResponseDto fromWithoutComments(Article article, boolean isOwner, boolean isLiked,
ArticleStatsDto stats) {
return ArticleResponseDto.builder()
.articleId(article.getArticleId())
.content(article.getContent())
.deletedStatus(article.getDeletedStatus())
.userName(article.getWriter().getUserName())
.customId(article.getWriter().getCustomId())
.hashtags(article.getHashtags() != null ? article.getHashtags()
.stream()
.map(Hashtag::getContent)
.collect(Collectors.toList()) : null)
.rePostId(article.getRePost() != null ? article.getRePost().getArticleId() : null)
.mediaUrls(article.getMediaList() != null ? article.getMediaList()
.stream()
.map(ArticleMedia::getFilePath)
.collect(Collectors.toList()) : null)
.comments(null) // 전체 조회 시 댓글 정보 제외
.viewCnt(article.getViewCnt())
.rePostCnt(stats.getRepostCnt())
.likeCnt(stats.getLikeCnt())
.commentCnt(stats.getCommentCnt())
.owner(isOwner)
.isLiked(isLiked)
.createdAt(article.getCreatedAt())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,31 @@ public List<ArticleResponseDto> getArticles(HttpServletRequest request, LocalDat
articleRepository.findRecentArticles(pageable) : // 처음 로드 시
articleRepository.findRecentArticles(cursor, pageable);

List<Comment> comments = articles.stream()
.flatMap(article -> commentRepository.findAllByArticleAndNotDeleted(article).stream())
.collect(Collectors.toList());

Map<UUID, CommentStatsDto> replyStatsMap = comments.stream()
.collect(Collectors.toMap(
Comment::getCommentId,
reply -> {
long likeCount = commentLikeRepository.countLikesByComment(reply);
long replyCount = commentRepository.countRepliesByComment(reply);
return CommentStatsDto.from(likeCount, replyCount);
}
));

return articles
.stream()
.map(article -> {
boolean isOwner = article.getWriter().getUserId().equals(user.getUserId());
ArticleStatsDto stats = findArticleStats(article);
boolean isLiked = articleLikeRepository.existsByArticle_ArticleIdAndUser_UserIdAndDeletedStatus(
article.getArticleId(), user.getUserId(), DeletedStatus.NOT_DELETED);
ArticleStatsDto stats = findArticleStats(article);
return ArticleResponseDto.fromWithoutComments(article, isOwner, isLiked, stats);
return ArticleResponseDto.from(article, isOwner, isLiked, stats, replyStatsMap);
})
.collect(Collectors.toList());
}

//리포스트 작성 (인용 x, 단순)
public ArticleCreateResponseDto rePostArticle(HttpServletRequest request, UUID articleId) {
User writer = getUser(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
@NoArgsConstructor
public class CommentResponseDto {
private UUID commentId;
private Long writerId;
private String customId;
private String userName;
private String content;
private DeletedStatus deletedStatus;
private UUID rePostId;
Expand All @@ -28,10 +29,11 @@ public class CommentResponseDto {
private List<CommentResponseDto> comments;

@Builder
private CommentResponseDto(UUID commentId, Long writerId, String content, DeletedStatus deletedStatus,
private CommentResponseDto(UUID commentId, String userName, String customId, String content, DeletedStatus deletedStatus,
UUID rePostId, int viewCnt, long likeCnt, long commentCnt, boolean owner, List<CommentResponseDto> comments) {
this.commentId = commentId;
this.writerId = writerId;
this.userName = userName;
this.customId = customId;
this.content = content;
this.deletedStatus = deletedStatus;
this.rePostId = rePostId;
Expand All @@ -46,7 +48,8 @@ public static CommentResponseDto from(Comment comment, boolean isOwner, CommentS
if (depth <= 0 || comment == null) { // 깊이 제한 또는 null일 때 호출 중단
return CommentResponseDto.builder()
.commentId(comment.getCommentId())
.writerId(comment.getWriter().getUserId())
.userName(comment.getArticle().getWriter().getUserName())
.customId(comment.getArticle().getWriter().getCustomId())
.content(comment.getContent())
.deletedStatus(comment.getDeletedStatus())
.viewCnt(comment.getViewCnt())
Expand All @@ -59,7 +62,8 @@ public static CommentResponseDto from(Comment comment, boolean isOwner, CommentS

return CommentResponseDto.builder()
.commentId(comment.getCommentId())
.writerId(comment.getWriter().getUserId())
.userName(comment.getArticle().getWriter().getUserName())
.customId(comment.getArticle().getWriter().getCustomId())
.content(comment.getContent())
.deletedStatus(comment.getDeletedStatus())
.viewCnt(comment.getViewCnt())
Expand All @@ -77,3 +81,4 @@ public static CommentResponseDto from(Comment comment, boolean isOwner, CommentS
.build();
}
}

0 comments on commit 075fee6

Please sign in to comment.