Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix : 그룹 캡슐 상세 조회 & 요약 조회 쿼리 개선 #401

Merged
merged 5 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package site.timecapsulearchive.core.domain.capsule.group_capsule.repository;

import static com.querydsl.core.group.GroupBy.groupBy;
import static com.querydsl.core.group.GroupBy.list;
import static site.timecapsulearchive.core.domain.capsule.entity.QCapsule.capsule;
import static site.timecapsulearchive.core.domain.capsule.entity.QGroupCapsuleOpen.groupCapsuleOpen;
import static site.timecapsulearchive.core.domain.capsule.entity.QImage.image;
Expand All @@ -26,6 +28,7 @@
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.GroupCapsuleSummaryDto;
import site.timecapsulearchive.core.domain.capsule.group_capsule.data.dto.MyGroupCapsuleDto;
import site.timecapsulearchive.core.domain.group.data.dto.GroupMemberSummaryDto;
import site.timecapsulearchive.core.domain.member.entity.QMember;

@Repository
@RequiredArgsConstructor
Expand All @@ -36,43 +39,50 @@ public class GroupCapsuleQueryRepository {
public Optional<GroupCapsuleDetailDto> findGroupCapsuleDetailDtoByCapsuleId(
final Long capsuleId
) {
final CapsuleDetailDto capsuleDetailDto = jpaQueryFactory
.select(
Projections.constructor(
CapsuleDetailDto.class,
capsule.id,
capsuleSkin.imageUrl,
capsule.dueDate,
member.nickname,
member.profileUrl,
capsule.createdAt,
capsule.point,
capsule.address.fullRoadAddressName,
capsule.address.roadName,
capsule.title,
capsule.content,
groupConcatDistinct(image.imageUrl),
groupConcatDistinct(video.videoUrl),
capsule.isOpened,
capsule.type
)
)
.from(capsule)
.join(member).on(capsule.member.id.eq(member.id))
final QMember owner = new QMember("owner");
final QMember groupMember = new QMember("groupMember");

return Optional.ofNullable(jpaQueryFactory
.selectFrom(capsule)
.join(owner).on(capsule.member.id.eq(owner.id))
.join(capsuleSkin).on(capsule.capsuleSkin.id.eq(capsuleSkin.id))
.leftJoin(image).on(capsule.id.eq(image.capsule.id))
.leftJoin(video).on(capsule.id.eq(video.capsule.id))
.join(groupCapsuleOpen).on(groupCapsuleOpen.capsule.id.eq(capsuleId))
.join(groupMember).on(groupMember.id.eq(groupCapsuleOpen.member.id))
.groupBy(groupCapsuleOpen.id)
.where(groupCapsuleOpen.capsule.id.eq(capsuleId))
.where(capsule.id.eq(capsuleId).and(capsule.type.eq(CapsuleType.GROUP)))
.fetchFirst();

if (capsuleDetailDto.capsuleId() == null) {
return Optional.empty();
}

final List<GroupMemberSummaryDto> memberSummaryDtos = getGroupMemberSummaryDtos(
capsuleId);

return Optional.of(new GroupCapsuleDetailDto(capsuleDetailDto, memberSummaryDtos));
.transform(
groupBy(capsule.id).as(
Projections.constructor(
GroupCapsuleDetailDto.class,
Projections.constructor(
CapsuleDetailDto.class,
capsule.id,
capsuleSkin.imageUrl,
capsule.dueDate,
owner.nickname,
owner.profileUrl,
capsule.createdAt,
capsule.point,
capsule.address.fullRoadAddressName,
capsule.address.roadName,
capsule.title,
capsule.content,
groupConcatDistinct(image.imageUrl),
groupConcatDistinct(video.videoUrl),
capsule.isOpened,
capsule.type
),
list(Projections.constructor(GroupMemberSummaryDto.class,
groupMember.nickname,
groupMember.profileUrl,
groupCapsuleOpen.isOpened)
)
)
)
).get(capsuleId));
}

private StringExpression groupConcatDistinct(final StringExpression expression) {
Expand All @@ -81,53 +91,44 @@ private StringExpression groupConcatDistinct(final StringExpression expression)

public Optional<GroupCapsuleSummaryDto> findGroupCapsuleSummaryDtoByCapsuleId(
final Long capsuleId) {
final CapsuleSummaryDto capsuleSummaryDto = jpaQueryFactory
.select(
Projections.constructor(
CapsuleSummaryDto.class,
member.nickname,
member.profileUrl,
capsuleSkin.imageUrl,
capsule.title,
capsule.dueDate,
capsule.point,
capsule.address.fullRoadAddressName,
capsule.address.roadName,
capsule.isOpened,
capsule.createdAt
)
)
.from(capsule)
.join(member).on(capsule.member.id.eq(member.id))
.join(capsuleSkin).on(capsule.capsuleSkin.id.eq(capsuleSkin.id))
final QMember owner = new QMember("owner");
final QMember groupMember = new QMember("groupMember");

return Optional.ofNullable(jpaQueryFactory
.selectFrom(capsule)
.join(owner).on(capsule.member.id.eq(owner.id))
.join(capsuleSkin).on(capsule.capsuleSkin.id.eq(capsuleSkin.id)).join(groupCapsuleOpen)
.on(groupCapsuleOpen.capsule.id.eq(capsuleId))
.join(groupMember).on(groupMember.id.eq(groupCapsuleOpen.member.id))
.groupBy(groupCapsuleOpen.id)
.where(groupCapsuleOpen.capsule.id.eq(capsuleId))
.where(capsule.id.eq(capsuleId).and(capsule.type.eq(CapsuleType.GROUP)))
.groupBy(capsule.id)
.fetchOne();
.transform(
groupBy(capsule.id).as(
Projections.constructor(
GroupCapsuleSummaryDto.class,
Projections.constructor(
CapsuleSummaryDto.class,
owner.nickname,
owner.profileUrl,
capsuleSkin.imageUrl,
capsule.title,
capsule.dueDate,
capsule.point,
capsule.address.fullRoadAddressName,
capsule.address.roadName,
capsule.isOpened,
capsule.createdAt
),
list(Projections.constructor(GroupMemberSummaryDto.class,
groupMember.nickname,
groupMember.profileUrl,
groupCapsuleOpen.isOpened)
)
)

if (capsuleSummaryDto == null) {
return Optional.empty();
}

final List<GroupMemberSummaryDto> memberSummaryDtos = getGroupMemberSummaryDtos(
capsuleId);

return Optional.of(new GroupCapsuleSummaryDto(capsuleSummaryDto, memberSummaryDtos));
}

private List<GroupMemberSummaryDto> getGroupMemberSummaryDtos(Long capsuleId) {
return jpaQueryFactory
.select(
Projections.constructor(
GroupMemberSummaryDto.class,
member.nickname,
member.profileUrl,
groupCapsuleOpen.isOpened
)
)
.from(groupCapsuleOpen)
.join(member).on(member.id.eq(groupCapsuleOpen.member.id))
.where(groupCapsuleOpen.capsule.id.eq(capsuleId))
.fetch();
).get(capsuleId));
}

public Slice<MyGroupCapsuleDto> findMyGroupCapsuleSlice(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,7 @@ public Slice<GroupSummaryDto> findGroupsSlice(
public Optional<GroupDetailDto> findGroupDetailByGroupId(final Long groupId) {
return Optional.ofNullable(
jpaQueryFactory
.select(
group.groupName,
group.groupDescription,
group.groupProfileUrl,
group.createdAt,
member.id,
member.profileUrl,
member.nickname,
member.tag,
memberGroup.isOwner
)
.from(group)
.selectFrom(group)
.join(memberGroup).on(memberGroup.group.id.eq(group.id))
.join(member).on(member.id.eq(memberGroup.member.id))
.where(group.id.eq(groupId))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public String getS3PreSignedUrlForGet(final String fileName) {
* @return 구분자 ,로 구분된 각각의 서명된 이미지 url
*/
public List<String> getS3PreSignedUrlsForGet(final String fileNames) {
if (fileNames == null) {
if (fileNames == null || fileNames.isEmpty()) {
return Collections.emptyList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private static void setFieldValue(Object instance, String fieldName, Object valu
* @param group 그룹
* @return {@code List<MemberGroup>} 테스트 픽스처들
*/
public static List<MemberGroup> membersGroup(List<Member> members, Group group) {
public static List<MemberGroup> memberGroups(List<Member> members, Group group) {
return members.stream()
.map(m -> memberGroup(m, group, Boolean.FALSE))
.toList();
Expand Down
Loading
Loading