From 4f75192d113c169fbc259fa8d545e3507b11579a Mon Sep 17 00:00:00 2001 From: Profile-exe Date: Thu, 25 Jul 2024 23:17:42 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EB=A7=A4=EC=B9=AD=20=EC=BF=BC=EB=A6=AC?= =?UTF-8?q?=20left=20join=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MatchingRepositoryCustomImpl.java | 82 ++++++++----------- 1 file changed, 35 insertions(+), 47 deletions(-) diff --git a/src/main/java/econo/buddybridge/matching/repository/MatchingRepositoryCustomImpl.java b/src/main/java/econo/buddybridge/matching/repository/MatchingRepositoryCustomImpl.java index e604f05..bbdd058 100644 --- a/src/main/java/econo/buddybridge/matching/repository/MatchingRepositoryCustomImpl.java +++ b/src/main/java/econo/buddybridge/matching/repository/MatchingRepositoryCustomImpl.java @@ -1,78 +1,66 @@ package econo.buddybridge.matching.repository; +import static econo.buddybridge.chat.chatmessage.entity.QChatMessage.chatMessage; +import static econo.buddybridge.matching.entity.QMatching.matching; +import static econo.buddybridge.member.entity.QMember.member; + import com.querydsl.core.types.dsl.BooleanExpression; +import com.querydsl.core.types.dsl.CaseBuilder; import com.querydsl.jpa.impl.JPAQueryFactory; -import econo.buddybridge.chat.chatmessage.entity.ChatMessage; import econo.buddybridge.matching.dto.MatchingCustomPage; import econo.buddybridge.matching.dto.MatchingResDto; -import econo.buddybridge.matching.dto.ReceiverDto; -import econo.buddybridge.matching.entity.Matching; +import econo.buddybridge.matching.dto.QMatchingResDto; +import econo.buddybridge.matching.dto.QReceiverDto; import econo.buddybridge.matching.entity.MatchingStatus; -import econo.buddybridge.member.entity.Member; -import java.util.stream.Collectors; +import java.time.LocalDateTime; +import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Repository; -import java.time.LocalDateTime; -import java.util.List; - -import static econo.buddybridge.chat.chatmessage.entity.QChatMessage.chatMessage; -import static econo.buddybridge.matching.entity.QMatching.matching; -import static econo.buddybridge.member.entity.QMember.member; - @Repository @RequiredArgsConstructor public class MatchingRepositoryCustomImpl implements MatchingRepositoryCustom { private final JPAQueryFactory queryFactory; - // TODO : 전체 매칭 리스트를 가져와 - // 가져오는데 커서기반 페이지네이션을 적용 - // cursor가 있는지 없는지에 따라 - // matchingStatus가 있는지 없는지 따라 - // 채팅의 마지막 생성 시간을 가져와서 정렬 @Override public MatchingCustomPage findMatchings(Long memberId, Integer size, LocalDateTime cursor, MatchingStatus matchingStatus, Pageable page) { int pageSize = page.getPageSize(); - List matchings = queryFactory - .selectFrom(matching) + List matchingResDtos = queryFactory + .select(new QMatchingResDto( + matching.id, + matching.post.postType, + matching.post.id, + chatMessage.content, + chatMessage.createdAt, + chatMessage.messageType, + matching.matchingStatus, + new QReceiverDto( + member.id, + member.nickname, + member.profileImageUrl + ) + )) + .from(matching) + .leftJoin(chatMessage).on(chatMessage.matching.id.eq(matching.id)) + .leftJoin(member).on( + member.id.eq( + new CaseBuilder() + .when(matching.taker.id.eq(memberId)).then(matching.giver.id) + .otherwise(matching.taker.id) + ) + ) .where( matching.taker.id.eq(memberId).or(matching.giver.id.eq(memberId)), buildCursorExpression(cursor), buildMatchingStatusExpression(matchingStatus) ) + .orderBy(chatMessage.createdAt.desc()) .limit(size + 1) .fetch(); - List matchingResDtos = matchings.stream() - .map(matching -> { - ChatMessage lastMessage = queryFactory - .selectFrom(chatMessage) - .where(chatMessage.matching.id.eq(matching.getId())) - .orderBy(chatMessage.createdAt.desc()) - .limit(1) - .fetchOne(); - - Long receiverId = memberId.equals(matching.getTaker().getId()) ? matching.getGiver().getId() : matching.getTaker().getId(); - Member receiver = queryFactory - .selectFrom(member) - .where(member.id.eq(receiverId)) - .fetchOne(); - - return new MatchingResDto( - matching.getId(), - matching.getPost().getPostType(), - matching.getPost().getId(), - lastMessage.getContent(), - lastMessage.getCreatedAt(), - lastMessage.getMessageType(), - matching.getMatchingStatus(), - new ReceiverDto(receiver) - ); - }).collect(Collectors.toList()); - boolean nextPage = false; if (matchingResDtos.size() > pageSize) { matchingResDtos.removeLast(); @@ -91,4 +79,4 @@ private BooleanExpression buildCursorExpression(LocalDateTime cursor) { private BooleanExpression buildMatchingStatusExpression(MatchingStatus matchingStatus) { return matchingStatus == null ? null : matching.matchingStatus.eq(matchingStatus); } -} +} \ No newline at end of file