|
| 1 | +package targeter.aim.domain.challenge.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.challenge.dto.WeeklyCommentDto; |
| 10 | +import targeter.aim.domain.challenge.entity.WeeklyComment; |
| 11 | +import targeter.aim.domain.file.dto.FileDto; |
| 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.challenge.entity.QWeeklyComment.weeklyComment; |
| 20 | +import static targeter.aim.domain.challenge.entity.QWeeklyProgress.weeklyProgress; |
| 21 | +import static targeter.aim.domain.file.entity.QChallengeCommentAttachedFile.challengeCommentAttachedFile; |
| 22 | +import static targeter.aim.domain.file.entity.QChallengeCommentImage.challengeCommentImage; |
| 23 | +import static targeter.aim.domain.file.entity.QProfileImage.profileImage; |
| 24 | +import static targeter.aim.domain.user.entity.QTier.tier; |
| 25 | +import static targeter.aim.domain.user.entity.QUser.user; |
| 26 | + |
| 27 | +@Repository |
| 28 | +@RequiredArgsConstructor |
| 29 | +public class WeeklyCommentQueryRepository { |
| 30 | + |
| 31 | + private final JPAQueryFactory queryFactory; |
| 32 | + |
| 33 | + public Page<WeeklyCommentDto.WeeklyCommentResponse> paginateByChallengeIdAndWeeksId( |
| 34 | + Long challengeId, |
| 35 | + Long weeksId, |
| 36 | + Pageable pageable |
| 37 | + ) { |
| 38 | + List<WeeklyComment> parentComments = queryFactory |
| 39 | + .selectFrom(weeklyComment) |
| 40 | + .join(weeklyComment.weeklyProgress, weeklyProgress) |
| 41 | + .leftJoin(weeklyComment.user, user).fetchJoin() |
| 42 | + .leftJoin(user.tier, tier).fetchJoin() |
| 43 | + .leftJoin(user.profileImage, profileImage).fetchJoin() |
| 44 | + .where( |
| 45 | + weeklyProgress.challenge.id.eq(challengeId), |
| 46 | + weeklyProgress.id.eq(weeksId), |
| 47 | + weeklyComment.parentComment.isNull() |
| 48 | + ) |
| 49 | + .orderBy(weeklyComment.createdAt.asc()) |
| 50 | + .offset(pageable.getOffset()) |
| 51 | + .limit(pageable.getPageSize()) |
| 52 | + .fetch(); |
| 53 | + |
| 54 | + Long total = queryFactory |
| 55 | + .select(weeklyComment.count()) |
| 56 | + .from(weeklyComment) |
| 57 | + .join(weeklyComment.weeklyProgress, weeklyProgress) |
| 58 | + .where( |
| 59 | + weeklyProgress.challenge.id.eq(challengeId), |
| 60 | + weeklyProgress.id.eq(weeksId), |
| 61 | + weeklyComment.parentComment.isNull() |
| 62 | + ) |
| 63 | + .fetchOne(); |
| 64 | + |
| 65 | + if (parentComments.isEmpty()) { |
| 66 | + return new PageImpl<>(Collections.emptyList(), pageable, total != null ? total : 0); |
| 67 | + } |
| 68 | + |
| 69 | + List<Long> parentIds = parentComments.stream() |
| 70 | + .map(WeeklyComment::getId) |
| 71 | + .toList(); |
| 72 | + |
| 73 | + List<WeeklyComment> childComments = queryFactory |
| 74 | + .selectFrom(weeklyComment) |
| 75 | + .leftJoin(weeklyComment.user, user).fetchJoin() |
| 76 | + .leftJoin(user.tier, tier).fetchJoin() |
| 77 | + .leftJoin(user.profileImage, profileImage).fetchJoin() |
| 78 | + .where(weeklyComment.parentComment.id.in(parentIds)) |
| 79 | + .orderBy(weeklyComment.createdAt.asc()) |
| 80 | + .fetch(); |
| 81 | + |
| 82 | + List<Long> allCommentIds = Stream.concat(parentComments.stream(), childComments.stream()) |
| 83 | + .map(WeeklyComment::getId) |
| 84 | + .distinct() |
| 85 | + .toList(); |
| 86 | + |
| 87 | + Map<Long, List<FileDto.FileResponse>> imageMap = fetchImages(allCommentIds); |
| 88 | + Map<Long, List<FileDto.FileResponse>> fileMap = fetchFiles(allCommentIds); |
| 89 | + |
| 90 | + List<WeeklyCommentDto.WeeklyCommentResponse> content = assembleHierarchy( |
| 91 | + parentComments, |
| 92 | + childComments, |
| 93 | + imageMap, |
| 94 | + fileMap |
| 95 | + ); |
| 96 | + |
| 97 | + return new PageImpl<>(content, pageable, total != null ? total : 0); |
| 98 | + } |
| 99 | + |
| 100 | + private List<WeeklyCommentDto.WeeklyCommentResponse> assembleHierarchy( |
| 101 | + List<WeeklyComment> parents, |
| 102 | + List<WeeklyComment> children, |
| 103 | + Map<Long, List<FileDto.FileResponse>> imageMap, |
| 104 | + Map<Long, List<FileDto.FileResponse>> fileMap |
| 105 | + ) { |
| 106 | + Map<Long, List<WeeklyComment>> childrenMap = children.stream() |
| 107 | + .collect(Collectors.groupingBy(c -> c.getParentComment().getId())); |
| 108 | + |
| 109 | + return parents.stream() |
| 110 | + .map(parent -> { |
| 111 | + WeeklyCommentDto.WeeklyCommentResponse parentDto = mapToDto(parent, imageMap, fileMap); |
| 112 | + |
| 113 | + List<WeeklyComment> myChildren = childrenMap.getOrDefault(parent.getId(), Collections.emptyList()); |
| 114 | + |
| 115 | + List<WeeklyCommentDto.WeeklyCommentResponse> childDtos = myChildren.stream() |
| 116 | + .map(child -> mapToDto(child, imageMap, fileMap)) |
| 117 | + .toList(); |
| 118 | + |
| 119 | + parentDto.setChildrenComments(childDtos); |
| 120 | + return parentDto; |
| 121 | + }) |
| 122 | + .toList(); |
| 123 | + } |
| 124 | + |
| 125 | + private WeeklyCommentDto.WeeklyCommentResponse mapToDto( |
| 126 | + WeeklyComment c, |
| 127 | + Map<Long, List<FileDto.FileResponse>> imageMap, |
| 128 | + Map<Long, List<FileDto.FileResponse>> fileMap |
| 129 | + ) { |
| 130 | + List<FileDto.FileResponse> images = imageMap.getOrDefault(c.getId(), Collections.emptyList()); |
| 131 | + List<FileDto.FileResponse> files = fileMap.getOrDefault(c.getId(), Collections.emptyList()); |
| 132 | + |
| 133 | + return WeeklyCommentDto.WeeklyCommentResponse.builder() |
| 134 | + .commentId(c.getId()) |
| 135 | + .depth(c.getDepth()) |
| 136 | + .writerInfo(WeeklyCommentDto.UserResponse.from(c.getUser())) |
| 137 | + .content(c.getContent()) |
| 138 | + .attachedImages(images) |
| 139 | + .attachedFiles(files) |
| 140 | + .createdAt(c.getCreatedAt()) |
| 141 | + .childrenComments(Collections.emptyList()) |
| 142 | + .build(); |
| 143 | + } |
| 144 | + |
| 145 | + private Map<Long, List<FileDto.FileResponse>> fetchImages(List<Long> commentIds) { |
| 146 | + return queryFactory |
| 147 | + .selectFrom(challengeCommentImage) |
| 148 | + .where(challengeCommentImage.weeklyComment.id.in(commentIds)) |
| 149 | + .fetch() |
| 150 | + .stream() |
| 151 | + .collect(Collectors.groupingBy( |
| 152 | + img -> img.getWeeklyComment().getId(), |
| 153 | + Collectors.mapping(FileDto.FileResponse::from, Collectors.toList()) |
| 154 | + )); |
| 155 | + } |
| 156 | + |
| 157 | + private Map<Long, List<FileDto.FileResponse>> fetchFiles(List<Long> commentIds) { |
| 158 | + return queryFactory |
| 159 | + .selectFrom(challengeCommentAttachedFile) |
| 160 | + .where(challengeCommentAttachedFile.weeklyComment.id.in(commentIds)) |
| 161 | + .fetch() |
| 162 | + .stream() |
| 163 | + .collect(Collectors.groupingBy( |
| 164 | + file -> file.getWeeklyComment().getId(), |
| 165 | + Collectors.mapping(FileDto.FileResponse::from, Collectors.toList()) |
| 166 | + )); |
| 167 | + } |
| 168 | +} |
0 commit comments