-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
채팅방 목록 조회 시 경매 이미지 개수만큼 데이터가 중복되는 현상 해결 및 쿼리 최적화 (#411)
* fix: 이미지 개수만큼 채팅방이 중복 조회 되는 문제 해결 * rename: dto 이름 변경 * refactor: 채팅방 아이디로 채팅방 조회할 때 쿼리 개선 * style: 나중에 하기로 한 것 todo 추가
- Loading branch information
1 parent
531ac1b
commit 1e0c612
Showing
22 changed files
with
382 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...a/com/ddang/ddang/chat/infrastructure/persistence/QuerydslChatRoomAndImageRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.ddang.ddang.chat.infrastructure.persistence; | ||
|
||
import com.ddang.ddang.chat.infrastructure.persistence.dto.ChatRoomAndImageDto; | ||
|
||
import java.util.Optional; | ||
|
||
public interface QuerydslChatRoomAndImageRepository { | ||
|
||
Optional<ChatRoomAndImageDto> findChatRoomById(final Long chatRoomId); | ||
} |
49 changes: 49 additions & 0 deletions
49
...m/ddang/ddang/chat/infrastructure/persistence/QuerydslChatRoomAndImageRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.ddang.ddang.chat.infrastructure.persistence; | ||
|
||
import com.ddang.ddang.chat.infrastructure.persistence.dto.ChatRoomAndImageDto; | ||
import com.ddang.ddang.chat.infrastructure.persistence.dto.ChatRoomAndImageQueryProjectionDto; | ||
import com.ddang.ddang.chat.infrastructure.persistence.dto.QChatRoomAndImageQueryProjectionDto; | ||
import com.querydsl.jpa.JPAExpressions; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.Optional; | ||
|
||
import static com.ddang.ddang.auction.domain.QAuction.auction; | ||
import static com.ddang.ddang.chat.domain.QChatRoom.chatRoom; | ||
import static com.ddang.ddang.image.domain.QAuctionImage.auctionImage; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class QuerydslChatRoomAndImageRepositoryImpl implements QuerydslChatRoomAndImageRepository { | ||
|
||
private final JPAQueryFactory queryFactory; | ||
|
||
@Override | ||
public Optional<ChatRoomAndImageDto> findChatRoomById(final Long chatRoomId) { | ||
|
||
final ChatRoomAndImageQueryProjectionDto chatRoomAndImageQueryProjectionDto = | ||
queryFactory.select(new QChatRoomAndImageQueryProjectionDto(chatRoom, auctionImage)) | ||
.from(chatRoom) | ||
.leftJoin(chatRoom.buyer).fetchJoin() | ||
.leftJoin(chatRoom.auction, auction).fetchJoin() | ||
.leftJoin(auction.seller).fetchJoin() | ||
.leftJoin(auctionImage).on(auctionImage.id.eq( | ||
JPAExpressions | ||
.select(auctionImage.id.min()) | ||
.from(auctionImage) | ||
.where(auctionImage.auction.id.eq(auction.id)) | ||
.groupBy(auctionImage.auction.id) | ||
)).fetchJoin() | ||
.leftJoin(auction.lastBid).fetchJoin() | ||
.where(chatRoom.id.eq(chatRoomId)) | ||
.fetchOne(); | ||
|
||
if (chatRoomAndImageQueryProjectionDto == null) { | ||
return Optional.empty(); | ||
} | ||
|
||
return Optional.of(chatRoomAndImageQueryProjectionDto.toDto()); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...g/ddang/chat/infrastructure/persistence/QuerydslChatRoomAndMessageAndImageRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.ddang.ddang.chat.infrastructure.persistence; | ||
|
||
import com.ddang.ddang.chat.infrastructure.persistence.dto.ChatRoomAndMessageAndImageDto; | ||
|
||
import java.util.List; | ||
|
||
public interface QuerydslChatRoomAndMessageAndImageRepository { | ||
|
||
List<ChatRoomAndMessageAndImageDto> findAllChatRoomInfoByUserIdOrderByLastMessage(final Long userId); | ||
} |
70 changes: 70 additions & 0 deletions
70
...ang/chat/infrastructure/persistence/QuerydslChatRoomAndMessageAndImageRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.ddang.ddang.chat.infrastructure.persistence; | ||
|
||
import com.ddang.ddang.chat.infrastructure.persistence.dto.ChatRoomAndMessageAndImageDto; | ||
import com.ddang.ddang.chat.infrastructure.persistence.dto.ChatRoomAndMessageAndImageQueryProjectionDto; | ||
import com.ddang.ddang.chat.infrastructure.persistence.dto.QChatRoomAndMessageAndImageQueryProjectionDto; | ||
import com.querydsl.core.types.dsl.BooleanExpression; | ||
import com.querydsl.jpa.JPAExpressions; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
import static com.ddang.ddang.auction.domain.QAuction.auction; | ||
import static com.ddang.ddang.chat.domain.QChatRoom.chatRoom; | ||
import static com.ddang.ddang.chat.domain.QMessage.message; | ||
import static com.ddang.ddang.image.domain.QAuctionImage.auctionImage; | ||
import static java.util.Comparator.comparing; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class QuerydslChatRoomAndMessageAndImageRepositoryImpl implements QuerydslChatRoomAndMessageAndImageRepository { | ||
|
||
private final JPAQueryFactory queryFactory; | ||
|
||
@Override | ||
public List<ChatRoomAndMessageAndImageDto> findAllChatRoomInfoByUserIdOrderByLastMessage(final Long userId) { | ||
final List<ChatRoomAndMessageAndImageQueryProjectionDto> unsortedDtos = | ||
queryFactory.select(new QChatRoomAndMessageAndImageQueryProjectionDto(chatRoom, message, auctionImage)) | ||
.from(chatRoom) | ||
.leftJoin(chatRoom.buyer).fetchJoin() | ||
.leftJoin(chatRoom.auction, auction).fetchJoin() | ||
.leftJoin(auction.seller).fetchJoin() | ||
.leftJoin(auctionImage).on(auctionImage.id.eq( | ||
JPAExpressions | ||
.select(auctionImage.id.min()) | ||
.from(auctionImage) | ||
.where(auctionImage.auction.id.eq(auction.id)) | ||
.groupBy(auctionImage.auction.id) | ||
)).fetchJoin() | ||
.leftJoin(auction.lastBid).fetchJoin() | ||
.leftJoin(message).on(message.id.eq( | ||
JPAExpressions | ||
.select(message.id.max()) | ||
.from(message) | ||
.where(message.chatRoom.id.eq(chatRoom.id)) | ||
.groupBy(message.chatRoom.id) | ||
)).fetchJoin() | ||
.where(isSellerOrWinner(userId)) | ||
.fetch(); | ||
|
||
return sortByLastMessageIdDesc(unsortedDtos); | ||
} | ||
|
||
private List<ChatRoomAndMessageAndImageDto> sortByLastMessageIdDesc( | ||
final List<ChatRoomAndMessageAndImageQueryProjectionDto> unsortedDtos | ||
) { | ||
return unsortedDtos.stream() | ||
.sorted(comparing( | ||
(ChatRoomAndMessageAndImageQueryProjectionDto unsortedDto) -> unsortedDto.message().getId() | ||
).reversed() | ||
).map(ChatRoomAndMessageAndImageQueryProjectionDto::toSortedDto) | ||
.toList(); | ||
} | ||
|
||
private BooleanExpression isSellerOrWinner(final Long userId) { | ||
return (auction.seller.id.eq(userId)) | ||
.or(chatRoom.buyer.id.eq(userId)); | ||
} | ||
} |
10 changes: 0 additions & 10 deletions
10
...com/ddang/ddang/chat/infrastructure/persistence/QuerydslChatRoomAndMessageRepository.java
This file was deleted.
Oops, something went wrong.
62 changes: 0 additions & 62 deletions
62
...ddang/ddang/chat/infrastructure/persistence/QuerydslChatRoomAndMessageRepositoryImpl.java
This file was deleted.
Oops, something went wrong.
4 changes: 0 additions & 4 deletions
4
...main/java/com/ddang/ddang/chat/infrastructure/persistence/QuerydslChatRoomRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,8 @@ | ||
package com.ddang.ddang.chat.infrastructure.persistence; | ||
|
||
import com.ddang.ddang.chat.domain.ChatRoom; | ||
|
||
import java.util.Optional; | ||
|
||
public interface QuerydslChatRoomRepository { | ||
|
||
Optional<ChatRoom> findChatRoomById(final Long chatRoomId); | ||
|
||
Optional<Long> findChatRoomIdByAuctionId(final Long auctionId); | ||
} |
Oops, something went wrong.