|
| 1 | +package targeter.aim.domain.post.repository; |
| 2 | + |
| 3 | +import com.querydsl.jpa.impl.JPAQueryFactory; |
| 4 | +import lombok.RequiredArgsConstructor; |
| 5 | +import org.springframework.data.domain.Page; |
| 6 | +import org.springframework.data.domain.PageImpl; |
| 7 | +import org.springframework.data.domain.Pageable; |
| 8 | +import org.springframework.stereotype.Repository; |
| 9 | +import targeter.aim.domain.file.dto.FileDto; |
| 10 | +import targeter.aim.domain.post.dto.CommentDto; |
| 11 | +import targeter.aim.domain.post.entity.Comment; |
| 12 | + |
| 13 | +import java.util.Collections; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.stream.Collectors; |
| 17 | +import java.util.stream.Stream; |
| 18 | + |
| 19 | +import static targeter.aim.domain.file.entity.QCommentAttachedFile.commentAttachedFile; |
| 20 | +import static targeter.aim.domain.file.entity.QCommentImage.commentImage; |
| 21 | +import static targeter.aim.domain.file.entity.QProfileImage.profileImage; |
| 22 | +import static targeter.aim.domain.post.entity.QComment.comment; |
| 23 | +import static targeter.aim.domain.user.entity.QTier.tier; |
| 24 | +import static targeter.aim.domain.user.entity.QUser.user; |
| 25 | + |
| 26 | +@Repository |
| 27 | +@RequiredArgsConstructor |
| 28 | +public class CommentQueryRepository { |
| 29 | + |
| 30 | + private final JPAQueryFactory queryFactory; |
| 31 | + |
| 32 | + public Page<CommentDto.CommentResponse> paginateByPostId( |
| 33 | + Long postId, |
| 34 | + Pageable pageable) { |
| 35 | + |
| 36 | + List<Comment> parentComments = queryFactory |
| 37 | + .selectFrom(comment) |
| 38 | + .leftJoin(comment.user, user).fetchJoin() |
| 39 | + .leftJoin(user.tier, tier).fetchJoin() |
| 40 | + .leftJoin(user.profileImage, profileImage).fetchJoin() |
| 41 | + .where( |
| 42 | + comment.post.id.eq(postId), |
| 43 | + comment.parent.isNull() |
| 44 | + ) |
| 45 | + .orderBy(comment.createdAt.asc()) |
| 46 | + .offset(pageable.getOffset()) |
| 47 | + .limit(pageable.getPageSize()) |
| 48 | + .fetch(); |
| 49 | + |
| 50 | + Long total = queryFactory |
| 51 | + .select(comment.count()) |
| 52 | + .from(comment) |
| 53 | + .where( |
| 54 | + comment.post.id.eq(postId), |
| 55 | + comment.parent.isNull() |
| 56 | + ) |
| 57 | + .fetchOne(); |
| 58 | + |
| 59 | + if (parentComments.isEmpty()) { |
| 60 | + return new PageImpl<>(Collections.emptyList(), pageable, total != null ? total : 0); |
| 61 | + } |
| 62 | + |
| 63 | + List<Long> parentIds = parentComments.stream() |
| 64 | + .map(Comment::getId) |
| 65 | + .toList(); |
| 66 | + |
| 67 | + List<Comment> childComments = queryFactory |
| 68 | + .selectFrom(comment) |
| 69 | + .leftJoin(comment.user, user).fetchJoin() |
| 70 | + .leftJoin(user.tier, tier).fetchJoin() |
| 71 | + .leftJoin(user.profileImage, profileImage).fetchJoin() |
| 72 | + .where(comment.parent.id.in(parentIds)) |
| 73 | + .orderBy(comment.createdAt.asc()) |
| 74 | + .fetch(); |
| 75 | + |
| 76 | + List<Long> allCommentIds = Stream.concat(parentComments.stream(), childComments.stream()) |
| 77 | + .map(Comment::getId) |
| 78 | + .distinct() |
| 79 | + .toList(); |
| 80 | + |
| 81 | + Map<Long, List<FileDto.FileResponse>> imageMap = fetchImages(allCommentIds); |
| 82 | + Map<Long, List<FileDto.FileResponse>> fileMap = fetchFiles(allCommentIds); |
| 83 | + |
| 84 | + List<CommentDto.CommentResponse> content = assembleCommentHierarchy(parentComments, childComments, imageMap, fileMap); |
| 85 | + |
| 86 | + return new PageImpl<>(content, pageable, total != null ? total : 0); |
| 87 | + } |
| 88 | + |
| 89 | + private List<CommentDto.CommentResponse> assembleCommentHierarchy( |
| 90 | + List<Comment> parents, |
| 91 | + List<Comment> children, |
| 92 | + Map<Long, List<FileDto.FileResponse>> imageMap, |
| 93 | + Map<Long, List<FileDto.FileResponse>> fileMap |
| 94 | + ) { |
| 95 | + Map<Long, List<Comment>> childrenMap = children.stream() |
| 96 | + .collect(Collectors.groupingBy(c -> c.getParent().getId())); |
| 97 | + |
| 98 | + return parents.stream() |
| 99 | + .map(parent -> { |
| 100 | + CommentDto.CommentResponse parentDto = mapToDto(parent, imageMap, fileMap); |
| 101 | + |
| 102 | + List<Comment> myChildren = childrenMap.getOrDefault(parent.getId(), Collections.emptyList()); |
| 103 | + |
| 104 | + List<CommentDto.CommentResponse> childDtos = myChildren.stream() |
| 105 | + .map(child -> mapToDto(child, imageMap, fileMap)) |
| 106 | + .toList(); |
| 107 | + |
| 108 | + parentDto.setChildrenComments(childDtos); |
| 109 | + |
| 110 | + return parentDto; |
| 111 | + }) |
| 112 | + .toList(); |
| 113 | + } |
| 114 | + |
| 115 | + private CommentDto.CommentResponse mapToDto( |
| 116 | + Comment c, |
| 117 | + Map<Long, List<FileDto.FileResponse>> imageMap, |
| 118 | + Map<Long, List<FileDto.FileResponse>> fileMap |
| 119 | + ) { |
| 120 | + // 기존 엔티티 내의 컬렉션을 쓰지 않고, 배치 조회한 Map에서 가져옵니다 (N+1 방지) |
| 121 | + List<FileDto.FileResponse> images = imageMap.getOrDefault(c.getId(), Collections.emptyList()); |
| 122 | + List<FileDto.FileResponse> files = fileMap.getOrDefault(c.getId(), Collections.emptyList()); |
| 123 | + |
| 124 | + // 빌더 패턴을 사용하여 DTO 생성 (기존 from 메서드 로직 참고하여 재구성) |
| 125 | + return CommentDto.CommentResponse.builder() |
| 126 | + .commentId(c.getId()) |
| 127 | + .depth(c.getDepth()) |
| 128 | + .writerInfo(CommentDto.UserResponse.from(c.getUser())) |
| 129 | + .content(c.getContents()) |
| 130 | + .attachedImages(images) |
| 131 | + .attachedFiles(files) |
| 132 | + .createdAt(c.getCreatedAt()) |
| 133 | + .childrenComments(Collections.emptyList()) // 초기화 |
| 134 | + .build(); |
| 135 | + } |
| 136 | + |
| 137 | + private Map<Long, List<FileDto.FileResponse>> fetchImages(List<Long> commentIds) { |
| 138 | + return queryFactory |
| 139 | + .selectFrom(commentImage) |
| 140 | + .where(commentImage.comment.id.in(commentIds)) |
| 141 | + .fetch() |
| 142 | + .stream() |
| 143 | + .collect(Collectors.groupingBy( |
| 144 | + img -> img.getComment().getId(), |
| 145 | + Collectors.mapping(FileDto.FileResponse::from, Collectors.toList()) |
| 146 | + )); |
| 147 | + } |
| 148 | + |
| 149 | + private Map<Long, List<FileDto.FileResponse>> fetchFiles(List<Long> commentIds) { |
| 150 | + return queryFactory |
| 151 | + .selectFrom(commentAttachedFile) |
| 152 | + .where(commentAttachedFile.comment.id.in(commentIds)) |
| 153 | + .fetch() |
| 154 | + .stream() |
| 155 | + .collect(Collectors.groupingBy( |
| 156 | + file -> file.getComment().getId(), |
| 157 | + Collectors.mapping(FileDto.FileResponse::from, Collectors.toList()) |
| 158 | + )); |
| 159 | + } |
| 160 | +} |
0 commit comments