From f188e8b37d9fbd9b6af0d49677e5536912915248 Mon Sep 17 00:00:00 2001 From: Dongyun Kim Date: Thu, 14 Nov 2024 03:17:25 +0900 Subject: [PATCH 1/2] refactor: #39 edit --- .../article/dto/ArticleResponseDto.java | 29 ------------------- .../article/service/ArticleService.java | 19 +++++++++--- .../comment/dto/CommentResponseDto.java | 15 ++++++---- 3 files changed, 25 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/leets/xcellentbe/domain/article/dto/ArticleResponseDto.java b/src/main/java/com/leets/xcellentbe/domain/article/dto/ArticleResponseDto.java index 8749a2b..d44366a 100644 --- a/src/main/java/com/leets/xcellentbe/domain/article/dto/ArticleResponseDto.java +++ b/src/main/java/com/leets/xcellentbe/domain/article/dto/ArticleResponseDto.java @@ -98,32 +98,3 @@ 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(); - } -} diff --git a/src/main/java/com/leets/xcellentbe/domain/article/service/ArticleService.java b/src/main/java/com/leets/xcellentbe/domain/article/service/ArticleService.java index 6b8e06c..850cd66 100644 --- a/src/main/java/com/leets/xcellentbe/domain/article/service/ArticleService.java +++ b/src/main/java/com/leets/xcellentbe/domain/article/service/ArticleService.java @@ -188,18 +188,29 @@ public List getArticles(HttpServletRequest request, LocalDat articleRepository.findRecentArticles(pageable) : // 처음 로드 시 articleRepository.findRecentArticles(cursor, pageable); + List comments = articles.stream() + .flatMap(article -> commentRepository.findAllByArticleAndNotDeleted(article).stream()) + .collect(Collectors.toList()); + + Map 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()); - 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, stats, replyStatsMap); }) .collect(Collectors.toList()); } - //리포스트 작성 (인용 x, 단순) public ArticleCreateResponseDto rePostArticle(HttpServletRequest request, UUID articleId) { User writer = getUser(request); diff --git a/src/main/java/com/leets/xcellentbe/domain/comment/dto/CommentResponseDto.java b/src/main/java/com/leets/xcellentbe/domain/comment/dto/CommentResponseDto.java index a19e418..a145b0e 100644 --- a/src/main/java/com/leets/xcellentbe/domain/comment/dto/CommentResponseDto.java +++ b/src/main/java/com/leets/xcellentbe/domain/comment/dto/CommentResponseDto.java @@ -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; @@ -28,10 +29,11 @@ public class CommentResponseDto { private List 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 comments) { this.commentId = commentId; - this.writerId = writerId; + this.userName = userName; + this.customId = customId; this.content = content; this.deletedStatus = deletedStatus; this.rePostId = rePostId; @@ -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()) @@ -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()) @@ -77,3 +81,4 @@ public static CommentResponseDto from(Comment comment, boolean isOwner, CommentS .build(); } } + From b119205c5a94b36f68413b36d0e652d45044e9a4 Mon Sep 17 00:00:00 2001 From: Dongyun Kim Date: Thu, 14 Nov 2024 03:27:24 +0900 Subject: [PATCH 2/2] refactor: #39 edit --- .../xcellentbe/domain/article/dto/ArticleResponseDto.java | 1 + .../xcellentbe/domain/article/service/ArticleService.java | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/leets/xcellentbe/domain/article/dto/ArticleResponseDto.java b/src/main/java/com/leets/xcellentbe/domain/article/dto/ArticleResponseDto.java index d44366a..45f3d6d 100644 --- a/src/main/java/com/leets/xcellentbe/domain/article/dto/ArticleResponseDto.java +++ b/src/main/java/com/leets/xcellentbe/domain/article/dto/ArticleResponseDto.java @@ -98,3 +98,4 @@ public static ArticleResponseDto from(Article article, boolean isOwner, boolean .createdAt(article.getCreatedAt()) .build(); } +} diff --git a/src/main/java/com/leets/xcellentbe/domain/article/service/ArticleService.java b/src/main/java/com/leets/xcellentbe/domain/article/service/ArticleService.java index 850cd66..1aa1e81 100644 --- a/src/main/java/com/leets/xcellentbe/domain/article/service/ArticleService.java +++ b/src/main/java/com/leets/xcellentbe/domain/article/service/ArticleService.java @@ -207,7 +207,9 @@ public List getArticles(HttpServletRequest request, LocalDat .map(article -> { boolean isOwner = article.getWriter().getUserId().equals(user.getUserId()); ArticleStatsDto stats = findArticleStats(article); - return ArticleResponseDto.from(article, isOwner, stats, replyStatsMap); + boolean isLiked = articleLikeRepository.existsByArticle_ArticleIdAndUser_UserIdAndDeletedStatus( + article.getArticleId(), user.getUserId(), DeletedStatus.NOT_DELETED); + return ArticleResponseDto.from(article, isOwner, isLiked, stats, replyStatsMap); }) .collect(Collectors.toList()); }