Skip to content

Commit

Permalink
[OING-381] refact: Comment, VoiceComment 및 댓글 조회 로직 리팩터링 (#279)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Ji-soo708 authored Dec 27, 2024
1 parent 28ee9ec commit bf35c20
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,8 +16,11 @@ public List<NotificationResponse> getNotifications(String loginMemberId) {
new NotificationResponse(
"01HGW2N7EHJVJ4CJ999RRS2E97",
"https://..",
NotificationStyle.NONE,
"우리 가족 모두가 생존신고를 완료했어요",
"우리 가족 모두가 생존신고를 완료했어요",
"https://..",
"https://..",
ZonedDateTime.now()
)
);
Expand Down
68 changes: 15 additions & 53 deletions post/src/main/java/com/oing/controller/CommentController.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Slf4j
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public PostCommentResponseV2 createPostVoiceComment(String postId,
CommentType.VOICE,
postId,
loginMemberId,
null,
savedVoiceComment.getAudioUrl(),
ZonedDateTime.now()
);
Expand Down
20 changes: 20 additions & 0 deletions post/src/main/java/com/oing/domain/BaseComment.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
7 changes: 6 additions & 1 deletion post/src/main/java/com/oing/domain/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,4 +32,9 @@ public class Comment extends BaseAuditEntity {
public void setContent(String content) {
this.content = content;
}

@Override
public String getContent() {
return content;
}
}
7 changes: 6 additions & 1 deletion post/src/main/java/com/oing/domain/VoiceComment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,4 +32,9 @@ public class VoiceComment extends BaseAuditEntity {
public void setAudioUrl(String audioUrl) {
this.audioUrl = audioUrl;
}

@Override
public String getContent() {
return audioUrl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
Expand Down

0 comments on commit bf35c20

Please sign in to comment.