Skip to content

Commit

Permalink
Merge pull request #633 from tukcomCD2024/fix/group_detail-B-core-#632
Browse files Browse the repository at this point in the history
fix : 그룹 상세 조회 수정
  • Loading branch information
seokho-1116 authored Aug 14, 2024
2 parents 80cb154 + 8012113 commit 3c7a32d
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 71 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package site.timecapsulearchive.core.domain.friend.data.dto;

public record FriendRelationDto(
Boolean isFriend,
Boolean isFriendInviteToFriend,
Boolean isFriendInviteToMe
) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package site.timecapsulearchive.core.domain.friend.data.dto;

import java.util.Map;

public class FriendRelations {

private final Map<Long, FriendRelationDto> friendRelationMap;

public FriendRelations(Map<Long, FriendRelationDto> friendRelationMap) {
this.friendRelationMap = friendRelationMap;
}

public FriendRelationDto getFriendRelation(final Long friendId) {
return friendRelationMap.get(friendId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Slice;
import site.timecapsulearchive.core.domain.friend.data.dto.FriendRelations;
import site.timecapsulearchive.core.domain.friend.data.dto.FriendSummaryDto;
import site.timecapsulearchive.core.domain.friend.data.dto.SearchFriendSummaryDto;
import site.timecapsulearchive.core.domain.friend.data.dto.SearchFriendSummaryDtoByTag;
Expand Down Expand Up @@ -34,4 +35,6 @@ Slice<FriendSummaryDto> findFriends(
final FriendBeforeGroupInviteRequest request);

List<Long> findFriendIds(final List<Long> groupMemberIds, final Long memberId);

FriendRelations findFriendRelations(List<Long> memberIds, Long ownerId);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package site.timecapsulearchive.core.domain.friend.repository.member_friend;

import static com.querydsl.core.group.GroupBy.groupBy;
import static site.timecapsulearchive.core.domain.friend.entity.QMemberFriend.memberFriend;
import static site.timecapsulearchive.core.domain.member.entity.QMember.member;

Expand All @@ -17,6 +18,8 @@
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.SliceImpl;
import org.springframework.stereotype.Repository;
import site.timecapsulearchive.core.domain.friend.data.dto.FriendRelationDto;
import site.timecapsulearchive.core.domain.friend.data.dto.FriendRelations;
import site.timecapsulearchive.core.domain.friend.data.dto.FriendSummaryDto;
import site.timecapsulearchive.core.domain.friend.data.dto.SearchFriendSummaryDto;
import site.timecapsulearchive.core.domain.friend.data.dto.SearchFriendSummaryDtoByTag;
Expand Down Expand Up @@ -199,4 +202,44 @@ public List<Long> findFriendIds(final List<Long> groupMemberIds, final Long memb
memberFriend.friend.id.in(groupMemberIds).and(memberFriend.owner.id.eq(memberId)))
.fetch();
}

@Override
public FriendRelations findFriendRelations(final List<Long> memberIds,
final Long ownerId) {
final QFriendInvite friendInviteToFriend = new QFriendInvite(FRIEND_INVITE_TO_FRIEND_PATH);
final QFriendInvite friendInviteToMe = new QFriendInvite(FRIEND_INVITE_TO_ME_PATH);

return new FriendRelations(
jpaQueryFactory
.select(
member.id,
memberFriend.id.isNotNull(),
friendInviteToFriend.isNotNull(),
friendInviteToMe.isNotNull()
)
.from(member)
.leftJoin(memberFriend)
.on(memberFriend.friend.id.eq(member.id).and(memberFriend.owner.id.eq(ownerId)),
memberFriend.deletedAt.isNull())
.leftJoin(friendInviteToFriend)
.on(friendInviteToFriend.friend.id.eq(member.id)
.and(friendInviteToFriend.owner.id.eq(ownerId)),
friendInviteToFriend.deletedAt.isNull())
.leftJoin(friendInviteToMe)
.on(friendInviteToMe.owner.id.eq(member.id)
.and(friendInviteToMe.friend.id.eq(ownerId)),
friendInviteToMe.deletedAt.isNull())
.where(member.id.in(memberIds))
.transform(
groupBy(member.id).as(
Projections.constructor(
FriendRelationDto.class,
memberFriend.id.isNotNull(),
friendInviteToFriend.isNotNull(),
friendInviteToMe.isNotNull()
)
)
)
);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package site.timecapsulearchive.core.domain.group.data.dto;

import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import site.timecapsulearchive.core.domain.friend.data.dto.FriendRelations;
import site.timecapsulearchive.core.domain.group.data.response.GroupDetailResponse;
import site.timecapsulearchive.core.domain.group.data.response.GroupMemberWithRelationResponse;

Expand All @@ -19,12 +21,15 @@ public record GroupDetailTotalDto(
public static GroupDetailTotalDto as(
final GroupDetailDto groupDetailDto,
final Long groupCapsuleTotalCount,
final List<Long> friendIds
final FriendRelations friendRelations
) {
final List<GroupMemberWithRelationDto> membersWithRelation = groupDetailDto.members()
.stream()
.map(dto -> dto.toRelationDto(friendIds))
.toList();
List<GroupMemberWithRelationDto> membersWithRelation = Collections.emptyList();
if (friendRelations != null) {
membersWithRelation = groupDetailDto.members()
.stream()
.map(dto -> dto.toRelationDto(friendRelations.getFriendRelation(dto.memberId())))
.toList();
}

return new GroupDetailTotalDto(
groupDetailDto.groupName(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package site.timecapsulearchive.core.domain.group.data.dto;

import java.util.List;
import java.util.function.Function;
import site.timecapsulearchive.core.domain.friend.data.dto.FriendRelationDto;
import site.timecapsulearchive.core.domain.group.data.response.GroupMemberInfoResponse;

public record GroupMemberDto(
Expand All @@ -12,14 +12,16 @@ public record GroupMemberDto(
Boolean isOwner
) {

public GroupMemberWithRelationDto toRelationDto(final List<Long> friendIds) {
public GroupMemberWithRelationDto toRelationDto(final FriendRelationDto friendRelationDto) {
return GroupMemberWithRelationDto.builder()
.memberId(memberId)
.profileUrl(profileUrl)
.nickname(nickname)
.tag(tag)
.isOwner(isOwner)
.isFriend(friendIds.contains(memberId))
.isFriend(friendRelationDto.isFriend())
.isFriendInviteToFriend(friendRelationDto.isFriendInviteToFriend())
.isFriendInviteToMe(friendRelationDto.isFriendInviteToMe())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ public record GroupMemberWithRelationDto(
String nickname,
String tag,
Boolean isOwner,
Boolean isFriend
Boolean isFriend,
Boolean isFriendInviteToFriend,
Boolean isFriendInviteToMe
) {

public GroupMemberWithRelationResponse toResponse() {
Expand All @@ -21,6 +23,8 @@ public GroupMemberWithRelationResponse toResponse() {
.tag(tag)
.isOwner(isOwner)
.isFriend(isFriend)
.isFriendInviteToFriend(isFriendInviteToFriend)
.isFriendInviteToMe(isFriendInviteToMe)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ public record GroupMemberWithRelationResponse(
Boolean isOwner,

@Schema(description = "친구 여부")
Boolean isFriend
Boolean isFriend,

@Schema(description = "친구한테 친구 요청 유무")
Boolean isFriendInviteToFriend,

@Schema(description = "친구로부터 친구 요청 유무")
Boolean isFriendInviteToMe
) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Slice<GroupSummaryDto> findGroupSummaries(

List<Long> getTotalGroupMemberCount(final List<Long> groupIds);

Optional<GroupDetailDto> findGroupDetailByGroupIdAndMemberId(final Long groupId,
Optional<GroupDetailDto> findGroupDetailByGroupIdAndMemberIdExcludeMemberId(final Long groupId,
final Long memberId);

Optional<Long> getTotalGroupMemberCount(Long groupId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -85,54 +84,48 @@ public List<Long> getTotalGroupMemberCount(final List<Long> groupIds) {
.fetch();
}

/**
* 사용자를 제외한 그룹원 정보와 그룹의 상세정보를 반환한다.
*
* @param groupId 상세정보를 찾을 그룹 아이디
* @param memberId 사용자 아이디
* @return 그룹의 상세정보({@code memberId} 제외 그룹원)
*/
@Override
public Optional<GroupDetailDto> findGroupDetailByGroupIdAndMemberId(final Long groupId,
public Optional<GroupDetailDto> findGroupDetailByGroupIdAndMemberIdExcludeMemberId(
final Long groupId,
final Long memberId) {
GroupDetailDto groupDetailDtoIncludeMe =
jpaQueryFactory
.selectFrom(group)
.join(memberGroup).on(memberGroup.group.eq(group),
memberGroup.deletedAt.isNull())
.join(member).on(member.eq(memberGroup.member),
member.deletedAt.isNull())
.where(group.id.eq(groupId))
.transform(
groupBy(group.id).as(
Projections.constructor(
GroupDetailDto.class,
group.groupName,
group.groupDescription,
group.groupProfileUrl,
group.createdAt,
Expressions.asBoolean(Boolean.FALSE),
list(
Projections.constructor(
GroupMemberDto.class,
member.id,
member.profileUrl,
member.nickname,
member.tag,
memberGroup.isOwner
)
GroupDetailDto groupDetailDtoIncludeMe = jpaQueryFactory
.selectFrom(group)
.join(memberGroup).on(memberGroup.group.eq(group),
memberGroup.deletedAt.isNull())
.join(member).on(member.eq(memberGroup.member),
member.deletedAt.isNull())
.where(group.id.eq(groupId))
.transform(
groupBy(group.id).as(
Projections.constructor(
GroupDetailDto.class,
group.groupName,
group.groupDescription,
group.groupProfileUrl,
group.createdAt,
Expressions.asBoolean(Boolean.FALSE),
list(
Projections.constructor(
GroupMemberDto.class,
member.id,
member.profileUrl,
member.nickname,
member.tag,
memberGroup.isOwner
)
)
)
)
.get(groupId);
)
.get(groupId);

if (Objects.isNull(groupDetailDtoIncludeMe)) {
if (groupDetailDtoIncludeMe == null) {
return Optional.empty();
}

boolean isOwner = false;
List<GroupMemberDto> groupMemberDtosExcludeMe = new ArrayList<>();
List<GroupMemberDto> groupMemberDtosExcludeMe = new ArrayList<>(
groupDetailDtoIncludeMe.members().size());
for (GroupMemberDto dto : groupDetailDtoIncludeMe.members()) {
if (!dto.memberId().equals(memberId)) {
groupMemberDtosExcludeMe.add(dto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import site.timecapsulearchive.core.domain.capsule.group_capsule.repository.GroupCapsuleQueryRepository;
import site.timecapsulearchive.core.domain.friend.data.dto.FriendRelations;
import site.timecapsulearchive.core.domain.friend.repository.member_friend.MemberFriendRepository;
import site.timecapsulearchive.core.domain.group.data.dto.CompleteGroupSummaryDto;
import site.timecapsulearchive.core.domain.group.data.dto.GroupDetailDto;
Expand Down Expand Up @@ -62,17 +63,21 @@ public Slice<CompleteGroupSummaryDto> findGroupsSlice(
}

public GroupDetailTotalDto findGroupDetailByGroupId(final Long memberId, final Long groupId) {
final GroupDetailDto groupDetailDto = groupRepository.findGroupDetailByGroupIdAndMemberId(
final GroupDetailDto groupDetailDto = groupRepository.findGroupDetailByGroupIdAndMemberIdExcludeMemberId(
groupId, memberId).orElseThrow(GroupNotFoundException::new);

final Long groupCapsuleCount = groupCapsuleQueryRepository.findGroupCapsuleCount(groupId);

final List<Long> groupMemberIds = groupDetailDto.members().stream()
.map(GroupMemberDto::memberId)
.toList();
final List<Long> friendIds = memberFriendRepository.findFriendIds(groupMemberIds, memberId);

return GroupDetailTotalDto.as(groupDetailDto, groupCapsuleCount, friendIds);
FriendRelations friendRelations = null;
if (!groupMemberIds.isEmpty()) {
friendRelations = memberFriendRepository.findFriendRelations(groupMemberIds, memberId);
}

return GroupDetailTotalDto.as(groupDetailDto, groupCapsuleCount, friendRelations);
}

public List<Group> findAllOwnerGroupsByMemberId(Long memberId) {
Expand Down
Loading

0 comments on commit 3c7a32d

Please sign in to comment.