Skip to content

Commit 20e8bc3

Browse files
committed
feat(community): 댓글-대댓글 구조로 댓글 조회 service 및 usecase 추가
1 parent 92e9847 commit 20e8bc3

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.somemore.community.service.comment;
2+
3+
import com.somemore.community.domain.CommunityComment;
4+
import com.somemore.community.dto.response.CommunityCommentResponseDto;
5+
import com.somemore.community.repository.comment.CommunityCommentRepository;
6+
import com.somemore.community.repository.mapper.CommunityCommentView;
7+
import com.somemore.community.usecase.comment.CommunityCommentQueryUseCase;
8+
import lombok.RequiredArgsConstructor;
9+
import org.springframework.stereotype.Service;
10+
import org.springframework.transaction.annotation.Transactional;
11+
12+
import java.util.*;
13+
14+
@RequiredArgsConstructor
15+
@Transactional(readOnly = true)
16+
@Service
17+
public class CommunityCommentQueryService implements CommunityCommentQueryUseCase {
18+
19+
private final CommunityCommentRepository communityCommentRepository;
20+
21+
@Override
22+
public List<CommunityCommentResponseDto> getCommunityCommentsByBoardId(Long boardId) {
23+
List<CommunityCommentView> allComments = communityCommentRepository.findCommentsByBoardId(boardId);
24+
List<CommunityCommentView> filteredComments = filterValidComments(allComments);
25+
return createCommentHierarchy(filteredComments);
26+
}
27+
28+
private List<CommunityCommentView> filterValidComments(List<CommunityCommentView> comments) {
29+
List<Long> parentCommentIds = findParentCommentIds(comments);
30+
31+
return comments.stream()
32+
.flatMap(comment -> processDeletedComment(parentCommentIds, comment).stream())
33+
.toList();
34+
}
35+
36+
private List<Long> findParentCommentIds(List<CommunityCommentView> comments) {
37+
return comments.stream()
38+
.filter(comment -> !comment.communityComment().getDeleted())
39+
.map(comment -> comment.communityComment().getParentCommentId())
40+
.filter(Objects::nonNull)
41+
.toList();
42+
}
43+
44+
private Optional<CommunityCommentView> processDeletedComment(List<Long> parentCommentIds, CommunityCommentView commentView) {
45+
CommunityComment comment = commentView.communityComment();
46+
47+
if (comment.getDeleted()) {
48+
if (parentCommentIds.contains(comment.getId())) {
49+
return Optional.of(commentView.replaceWriterNickname(commentView));
50+
}
51+
return Optional.empty();
52+
}
53+
54+
return Optional.of(commentView);
55+
}
56+
57+
private List<CommunityCommentResponseDto> createCommentHierarchy(List<CommunityCommentView> comments) {
58+
59+
Map<Long, CommunityCommentResponseDto> commentMap = new HashMap<>();
60+
List<CommunityCommentResponseDto> rootComments = new ArrayList<>();
61+
62+
for (CommunityCommentView comment : comments) {
63+
CommunityCommentResponseDto dto = CommunityCommentResponseDto.fromView(comment);
64+
commentMap.put(dto.id(), dto);
65+
66+
Long parentCommentId = comment.communityComment().getParentCommentId();
67+
68+
if (parentCommentId == null) {
69+
rootComments.add(dto);
70+
} else {
71+
commentMap.get(parentCommentId).addReply(dto);
72+
}
73+
}
74+
75+
return rootComments;
76+
}
77+
}
78+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.somemore.community.usecase.comment;
2+
3+
import com.somemore.community.dto.response.CommunityCommentResponseDto;
4+
5+
import java.util.List;
6+
7+
public interface CommunityCommentQueryUseCase {
8+
List<CommunityCommentResponseDto> getCommunityCommentsByBoardId(Long boardId);
9+
}

0 commit comments

Comments
 (0)