1
1
package com .flint .flint .community .service ;
2
2
3
- import com . flint . flint . community . domain . post . PostComment ;
4
- import com .flint .flint .community .dto .response .PostCommentGetResponse ;
3
+
4
+ import com .flint .flint .community .dto .response .PostCommentResponse ;
5
5
import com .flint .flint .community .repository .post_comment .PostCommentRepositoryCustom ;
6
6
import lombok .RequiredArgsConstructor ;
7
7
import org .springframework .stereotype .Service ;
17
17
@ RequiredArgsConstructor
18
18
public class PostCommentGetService {
19
19
20
+ private final static int PRIORITY_LIKE_CRITERIA = 3 ;
20
21
private final PostCommentRepositoryCustom postCommentRepositoryCustom ;
21
22
22
- @ Transactional
23
- public List <PostCommentGetResponse > getPostComment (long postId ) {
24
- List <PostComment > postCommentList = postCommentRepositoryCustom .findByPostId (postId );
23
+ @ Transactional (readOnly = true )
24
+ public List <PostCommentResponse > getPostComment (long postId ) {
25
+ List <PostCommentResponse > commentResponses = postCommentRepositoryCustom .findByPostId (postId );
26
+
27
+ // 공감수에 따라 댓글 분류
28
+ List <PostCommentResponse > highLikesParentComments = new ArrayList <>();
29
+ List <PostCommentResponse > lowLikesParentComments = new ArrayList <>();
30
+ List <PostCommentResponse > highLikesChildComments = new ArrayList <>();
31
+ List <PostCommentResponse > lowLikesChildComments = new ArrayList <>();
32
+
33
+ for (PostCommentResponse comment : commentResponses ) {
34
+ if (comment .getParentCommentId () == null ) {
35
+ if (comment .getLikeCount () != null && comment .getLikeCount () >= PRIORITY_LIKE_CRITERIA ) {
36
+ highLikesParentComments .add (comment );
37
+ } else {
38
+ lowLikesParentComments .add (comment );
39
+ }
40
+ } else {
41
+ if (comment .getLikeCount () != null && comment .getLikeCount () >= PRIORITY_LIKE_CRITERIA ) {
42
+ highLikesChildComments .add (comment );
43
+ } else {
44
+ lowLikesChildComments .add (comment );
45
+ }
46
+ }
47
+ }
48
+
49
+ // 공감수가 높은 댓글을 공감수 내림차순으로 정렬
50
+ highLikesParentComments .sort (Comparator .comparing (PostCommentResponse ::getLikeCount ).reversed ());
51
+ highLikesChildComments .sort (Comparator .comparing (PostCommentResponse ::getLikeCount ).reversed ());
52
+
53
+ // 공감수가 낮은 댓글을 생성 시간 오름차순으로 정렬
54
+ lowLikesParentComments .sort (Comparator .comparing (PostCommentResponse ::getCreatedAt ).reversed ());
55
+ lowLikesChildComments .sort (Comparator .comparing (PostCommentResponse ::getCreatedAt ).reversed ());
25
56
26
- List <PostCommentGetResponse > responseList = new ArrayList <>();
27
- Map <Long , PostCommentGetResponse > responseHashMap = new HashMap <>();
28
57
29
- postCommentList .forEach (postComment -> {
30
- PostCommentGetResponse postCommentGetResponse = PostCommentGetResponse .convertCommentToDTO (postComment );
31
- responseHashMap .put (postCommentGetResponse .getPostCommentId (), postCommentGetResponse );
58
+ // 최종 결과 리스트 생성
59
+ List <PostCommentResponse > sortedComments = new ArrayList <>();
60
+ sortedComments .addAll (highLikesParentComments );
61
+ sortedComments .addAll (lowLikesParentComments );
62
+ sortedComments .addAll (highLikesChildComments );
63
+ sortedComments .addAll (lowLikesChildComments );
32
64
33
- // 대댓글인 경우 부모 댓글의 replies에 추가
34
- Optional .ofNullable (postComment .getParentComment ())
35
- .map (parent -> responseHashMap .get (parent .getId ()))
36
- .ifPresent (parentResponse -> parentResponse .getReplies ().add (postCommentGetResponse ));
65
+ // 대댓글 구조 재구성
66
+ Map <Long , PostCommentResponse > commentMap = new HashMap <>();
67
+ List <PostCommentResponse > parentCommentResponses = new ArrayList <>();
37
68
38
- // 댓글인 경우 ResponseList에 추가
39
- if (postComment .getParentComment () == null ) {
40
- responseList .add (postCommentGetResponse );
69
+ for (PostCommentResponse commentResponse : sortedComments ) {
70
+ commentMap .put (commentResponse .getPostCommentId (), commentResponse );
71
+ if (commentResponse .getParentCommentId () == null ) {
72
+ parentCommentResponses .add (commentResponse );
73
+ } else {
74
+ if (commentMap .containsKey (commentResponse .getParentCommentId ())) {
75
+ commentMap .get (commentResponse .getParentCommentId ()).getReplies ().add (commentResponse );
76
+ }
41
77
}
42
- });
43
- return responseList ;
78
+ }
79
+ return parentCommentResponses ;
44
80
}
45
- }
81
+ }
0 commit comments