From bf35c201b7d0921bbca92201fcbcf46689016eba Mon Sep 17 00:00:00 2001 From: Jisu Lim <69844138+Ji-soo708@users.noreply.github.com> Date: Fri, 27 Dec 2024 17:21:30 +0900 Subject: [PATCH] =?UTF-8?q?[OING-381]=20refact:=20Comment,=20VoiceComment?= =?UTF-8?q?=20=EB=B0=8F=20=EB=8C=93=EA=B8=80=20=EC=A1=B0=ED=9A=8C=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EB=A6=AC=ED=8C=A9=ED=84=B0=EB=A7=81=20(#2?= =?UTF-8?q?79)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: add BaseComment * refact: refactor Comment and VoiceComment entity * refact: refactor getPostComments logic * fix: add missing field's value * refact: delete audioUrl field in Response --- .../controller/NotificationController.java | 4 ++ .../oing/controller/CommentController.java | 68 ++++--------------- .../controller/VoiceCommentController.java | 1 - .../java/com/oing/domain/BaseComment.java | 20 ++++++ .../main/java/com/oing/domain/Comment.java | 7 +- .../java/com/oing/domain/VoiceComment.java | 7 +- .../dto/response/PostCommentResponseV2.java | 5 +- 7 files changed, 52 insertions(+), 60 deletions(-) create mode 100644 post/src/main/java/com/oing/domain/BaseComment.java diff --git a/gateway/src/main/java/com/oing/controller/NotificationController.java b/gateway/src/main/java/com/oing/controller/NotificationController.java index dbde408b..e2ee48ec 100644 --- a/gateway/src/main/java/com/oing/controller/NotificationController.java +++ b/gateway/src/main/java/com/oing/controller/NotificationController.java @@ -1,5 +1,6 @@ package com.oing.controller; +import com.oing.domain.NotificationStyle; import com.oing.dto.response.NotificationResponse; import com.oing.restapi.NotificationApi; import org.springframework.stereotype.Controller; @@ -15,8 +16,11 @@ public List<NotificationResponse> getNotifications(String loginMemberId) { new NotificationResponse( "01HGW2N7EHJVJ4CJ999RRS2E97", "https://..", + NotificationStyle.NONE, "우리 가족 모두가 생존신고를 완료했어요", "우리 가족 모두가 생존신고를 완료했어요", + "https://..", + "https://..", ZonedDateTime.now() ) ); diff --git a/post/src/main/java/com/oing/controller/CommentController.java b/post/src/main/java/com/oing/controller/CommentController.java index 9a4137a3..2acb2386 100644 --- a/post/src/main/java/com/oing/controller/CommentController.java +++ b/post/src/main/java/com/oing/controller/CommentController.java @@ -21,7 +21,6 @@ import java.time.ZoneId; import java.util.ArrayList; -import java.util.Comparator; import java.util.List; @Slf4j @@ -73,15 +72,16 @@ public PaginationResponse<PostCommentResponseV2> getPostComments(String postId, Post post = postService.getMemberPostById(postId); validateAuthorization(loginMemberId, post); - List<Comment> comments = commentService.getPostComments(postId); - List<VoiceComment> voiceComments = voiceCommentService.getPostVoiceComments(postId); - - // 댓글과 음성 댓글 통합 및 정렬 - List<PostCommentResponseV2> combinedComments = combineComments(comments, voiceComments); - combinedComments.sort(getCommentComparator(sort)); + List<BaseComment> comments = new ArrayList<>(); + comments.addAll(commentService.getPostComments(postId)); + comments.addAll(voiceCommentService.getPostVoiceComments(postId)); + comments.sort(BaseComment.getComparator(sort)); // 페이징 처리 - return paginateComments(combinedComments, page, size); + List<PostCommentResponseV2> response = comments.stream() + .map(comment -> mapToPostCommentResponse(comment, postId)) + .toList(); + return paginateComments(response, page, size); } private void validateAuthorization(String loginMemberId, Post post) { @@ -91,55 +91,17 @@ private void validateAuthorization(String loginMemberId, Post post) { } } - private List<PostCommentResponseV2> combineComments(List<Comment> comments, List<VoiceComment> voiceComments) { - List<PostCommentResponseV2> textComments = comments.stream() - .map(this::mapToTextComment) - .toList(); - List<PostCommentResponseV2> voiceCommentResponses = voiceComments.stream() - .map(this::mapToVoiceComment) - .toList(); - - List<PostCommentResponseV2> combinedComments = new ArrayList<>(); - combinedComments.addAll(textComments); - combinedComments.addAll(voiceCommentResponses); - - return combinedComments; - } - - private PostCommentResponseV2 mapToTextComment(Comment comment) { + private PostCommentResponseV2 mapToPostCommentResponse(BaseComment baseComment, String postId) { return new PostCommentResponseV2( - comment.getId(), - CommentType.TEXT, - comment.getPost().getId(), - comment.getMemberId(), - comment.getContent(), - null, - comment.getCreatedAt().atZone(ZoneId.systemDefault()) + baseComment.getId(), + baseComment instanceof Comment ? CommentType.TEXT : CommentType.VOICE, + postId, + baseComment.getMemberId(), + baseComment.getContent(), + baseComment.getCreatedAt().atZone(ZoneId.systemDefault()) ); } - private PostCommentResponseV2 mapToVoiceComment(VoiceComment voiceComment) { - return new PostCommentResponseV2( - voiceComment.getId(), - CommentType.VOICE, - voiceComment.getPost().getId(), - voiceComment.getMemberId(), - null, - voiceComment.getAudioUrl(), - voiceComment.getCreatedAt().atZone(ZoneId.systemDefault()) - ); - } - - private Comparator<PostCommentResponseV2> getCommentComparator(String sort) { - return (c1, c2) -> { - if (sort == null || sort.equalsIgnoreCase("ASC")) { - return c1.commentId().compareTo(c2.commentId()); - } else { - return c2.commentId().compareTo(c1.commentId()); - } - }; - } - private PaginationResponse<PostCommentResponseV2> paginateComments(List<PostCommentResponseV2> comments, Integer page, Integer size) { Paginator<PostCommentResponseV2> paginator = new Paginator<>(comments, size); PaginationDTO<PostCommentResponseV2> paginationDTO = paginator.getPage(page); diff --git a/post/src/main/java/com/oing/controller/VoiceCommentController.java b/post/src/main/java/com/oing/controller/VoiceCommentController.java index 62706200..4531141e 100644 --- a/post/src/main/java/com/oing/controller/VoiceCommentController.java +++ b/post/src/main/java/com/oing/controller/VoiceCommentController.java @@ -46,7 +46,6 @@ public PostCommentResponseV2 createPostVoiceComment(String postId, CommentType.VOICE, postId, loginMemberId, - null, savedVoiceComment.getAudioUrl(), ZonedDateTime.now() ); diff --git a/post/src/main/java/com/oing/domain/BaseComment.java b/post/src/main/java/com/oing/domain/BaseComment.java new file mode 100644 index 00000000..8183ce3b --- /dev/null +++ b/post/src/main/java/com/oing/domain/BaseComment.java @@ -0,0 +1,20 @@ +package com.oing.domain; + +import jakarta.persistence.MappedSuperclass; + +import java.util.Comparator; + +@MappedSuperclass +public abstract class BaseComment extends BaseAuditEntity { + public abstract String getContent(); + public abstract String getMemberId(); + public abstract String getId(); + + public static Comparator<BaseComment> getComparator(String sort) { + if (sort == null || sort.equalsIgnoreCase("ASC")) { + return Comparator.comparing(BaseComment::getCreatedAt); + } else { + return Comparator.comparing(BaseComment::getCreatedAt).reversed(); + } + } +} diff --git a/post/src/main/java/com/oing/domain/Comment.java b/post/src/main/java/com/oing/domain/Comment.java index 225ec42d..4c52036a 100644 --- a/post/src/main/java/com/oing/domain/Comment.java +++ b/post/src/main/java/com/oing/domain/Comment.java @@ -14,7 +14,7 @@ }) @Entity(name = "comment") @EntityListeners(CommentEntityListener.class) -public class Comment extends BaseAuditEntity { +public class Comment extends BaseComment { @Id @Column(name = "comment_id", columnDefinition = "CHAR(26)", nullable = false) private String id; @@ -32,4 +32,9 @@ public class Comment extends BaseAuditEntity { public void setContent(String content) { this.content = content; } + + @Override + public String getContent() { + return content; + } } diff --git a/post/src/main/java/com/oing/domain/VoiceComment.java b/post/src/main/java/com/oing/domain/VoiceComment.java index 0c26496f..ae7be3d3 100644 --- a/post/src/main/java/com/oing/domain/VoiceComment.java +++ b/post/src/main/java/com/oing/domain/VoiceComment.java @@ -14,7 +14,7 @@ }) @Entity(name = "voice_comment") @EntityListeners(VoiceCommentEntityListener.class) -public class VoiceComment extends BaseAuditEntity { +public class VoiceComment extends BaseComment { @Id @Column(name = "voice_comment_id", columnDefinition = "CHAR(26)", nullable = false) private String id; @@ -32,4 +32,9 @@ public class VoiceComment extends BaseAuditEntity { public void setAudioUrl(String audioUrl) { this.audioUrl = audioUrl; } + + @Override + public String getContent() { + return audioUrl; + } } diff --git a/post/src/main/java/com/oing/dto/response/PostCommentResponseV2.java b/post/src/main/java/com/oing/dto/response/PostCommentResponseV2.java index 3b02a51a..5fd0a6ab 100644 --- a/post/src/main/java/com/oing/dto/response/PostCommentResponseV2.java +++ b/post/src/main/java/com/oing/dto/response/PostCommentResponseV2.java @@ -18,12 +18,9 @@ public record PostCommentResponseV2( @Schema(description = "음성 댓글 작성 사용자 ID", example = "01HGW2N7EHJVJ4CJ999RRS2E97") String memberId, - @Schema(description = "피드 게시물 내용", example = "정말 환상적인 하루였네요!") + @Schema(description = "댓글 내용 / 음성 파일 주소", example = "정말 환상적인 하루였네요!") String comment, - @Schema(description = "음성 댓글 오디오 URL", example = "https://..") - String audioUrl, - @Schema(description = "댓글 작성 시간", example = "2023-12-23T01:53:21.577347+09:00") ZonedDateTime createdAt ) {