-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 커서 기반으로 채팅 내역 조회하는 커스텀 리파지토리 구현 * fix: 매칭 조회 쿼리에 fetch join 적용 * refactor: DTO 변환 메서드를 정적 팩토리 메서드로 구현 * refactor: 매칭 서비스 채팅 내역 조회 메서드 개선 - 첫 조회 시에만 알림 모두 읽기 적용해 부하 줄임 - 정적 팩토리 메서드로 코드 간략화 - fetch join으로 프록시 객체 초기화 오류 해결 - QueryDSL의 이점을 살린 커스텀 리파지토리의 단일 메서드로 로직 간략화 * refactor: 불필요한 메서드 제거 * style: 컨벤션 개선
- Loading branch information
1 parent
4c96d02
commit 495b4c2
Showing
11 changed files
with
116 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ public record ChatMessageReqDto( | |
String content, | ||
MessageType messageType | ||
) { | ||
|
||
} |
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
11 changes: 11 additions & 0 deletions
11
src/main/java/econo/buddybridge/chat/chatmessage/dto/ChatMessagesWithCursor.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,11 @@ | ||
package econo.buddybridge.chat.chatmessage.dto; | ||
|
||
import java.util.List; | ||
|
||
public record ChatMessagesWithCursor( | ||
List<ChatMessageResDto> chatMessages, | ||
Long cursor, | ||
boolean nextPage | ||
) { | ||
|
||
} |
12 changes: 2 additions & 10 deletions
12
src/main/java/econo/buddybridge/chat/chatmessage/repository/ChatMessageRepository.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,21 +1,13 @@ | ||
package econo.buddybridge.chat.chatmessage.repository; | ||
|
||
import econo.buddybridge.chat.chatmessage.entity.ChatMessage; | ||
import java.util.List; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.domain.Slice; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
|
||
import java.util.List; | ||
|
||
public interface ChatMessageRepository extends JpaRepository<ChatMessage,Long> { | ||
public interface ChatMessageRepository extends JpaRepository<ChatMessage, Long>, ChatMessageRepositoryCustom { | ||
|
||
@Query("SELECT cm FROM ChatMessage cm WHERE cm.matching.id = :matchingId ORDER BY cm.id DESC") | ||
List<ChatMessage> findLastMessageByMatchingId(Long matchingId, Pageable pageable); | ||
|
||
@Query("SELECT cm FROM ChatMessage cm WHERE cm.matching.id = :matchingId ORDER BY cm.id DESC") | ||
Slice<ChatMessage> findByMatchingId(Long matchingId, Pageable pageable); | ||
|
||
@Query("SELECT cm FROM ChatMessage cm WHERE cm.matching.id = :matchingId AND cm.id < :cursor ORDER BY cm.id DESC") | ||
Slice<ChatMessage> findByMatchingIdAndIdGreaterThan(Long matchingId, Long cursor, Pageable pageable); | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/econo/buddybridge/chat/chatmessage/repository/ChatMessageRepositoryCustom.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 econo.buddybridge.chat.chatmessage.repository; | ||
|
||
import econo.buddybridge.chat.chatmessage.dto.ChatMessagesWithCursor; | ||
import econo.buddybridge.matching.entity.Matching; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
public interface ChatMessageRepositoryCustom { | ||
|
||
ChatMessagesWithCursor findByMatching(Matching matching, Long cursor, Pageable page); | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/java/econo/buddybridge/chat/chatmessage/repository/ChatMessageRepositoryImpl.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,56 @@ | ||
package econo.buddybridge.chat.chatmessage.repository; | ||
|
||
import static econo.buddybridge.chat.chatmessage.entity.QChatMessage.chatMessage; | ||
|
||
import com.querydsl.core.types.Predicate; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import econo.buddybridge.chat.chatmessage.dto.ChatMessageResDto; | ||
import econo.buddybridge.chat.chatmessage.dto.ChatMessagesWithCursor; | ||
import econo.buddybridge.chat.chatmessage.dto.QChatMessageResDto; | ||
import econo.buddybridge.matching.entity.Matching; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
@RequiredArgsConstructor | ||
public class ChatMessageRepositoryImpl implements ChatMessageRepositoryCustom { | ||
|
||
private final JPAQueryFactory queryFactory; | ||
|
||
@Override | ||
public ChatMessagesWithCursor findByMatching(Matching matching, Long cursor, Pageable page) { | ||
int pageSize = page.getPageSize(); | ||
|
||
List<ChatMessageResDto> content = queryFactory | ||
.select(new QChatMessageResDto( | ||
chatMessage.id, | ||
chatMessage.sender.id, | ||
chatMessage.content, | ||
chatMessage.messageType, | ||
chatMessage.createdAt | ||
)) | ||
.from(chatMessage) | ||
.where(chatMessage.matching.eq(matching), buildCursorPredicate(cursor)) | ||
.orderBy(chatMessage.id.desc()) | ||
.limit(pageSize + 1) | ||
.fetch(); | ||
|
||
boolean nextPage = false; | ||
if (content.size() > pageSize) { | ||
content.removeLast(); | ||
nextPage = true; | ||
} | ||
|
||
Long nextCursor = nextPage ? content.getLast().messageId() : -1L; | ||
|
||
return new ChatMessagesWithCursor(content, nextCursor, nextPage); | ||
} | ||
|
||
private Predicate buildCursorPredicate(Long cursor) { | ||
if (cursor == null) { | ||
return null; | ||
} | ||
|
||
return chatMessage.id.lt(cursor); | ||
} | ||
} |
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
8 changes: 5 additions & 3 deletions
8
src/main/java/econo/buddybridge/matching/repository/MatchingRepository.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,10 +1,12 @@ | ||
package econo.buddybridge.matching.repository; | ||
|
||
import econo.buddybridge.matching.entity.Matching; | ||
import java.util.Optional; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
import org.springframework.data.jpa.repository.Query; | ||
|
||
public interface MatchingRepository extends JpaRepository<Matching, Long> { | ||
List<Matching> findByPostId(Long postId); | ||
|
||
@Query("SELECT m FROM Matching m JOIN FETCH m.post JOIN FETCH m.giver JOIN FETCH m.taker WHERE m.id = :matchingId") | ||
Optional<Matching> findByIdWithMembersAndPost(Long matchingId); | ||
} |
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