Skip to content

Commit 396ccdb

Browse files
authored
Merge pull request #55 from BusanVibe/dev
채팅 조회 API cursor-id 수정
2 parents d5cf613 + 04b437f commit 396ccdb

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/main/kotlin/busanVibe/busan/domain/chat/service/ChatMongoService.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ class ChatMongoService(
108108

109109
fun getChatHistory(cursorId: String?, pageSize: Int = 15): ChatMessageResponseDTO.ListDto {
110110

111+
// 요청값 검증
112+
// pageSize
111113
if(pageSize < 0){
112114
throw GeneralException(ErrorStatus.INVALID_PAGE_SIZE_MINUS)
113115
}else if (pageSize > 30) {
@@ -118,12 +120,16 @@ class ChatMongoService(
118120
val currentUser = AuthService().getCurrentUser()
119121

120122
// 조회 -> List<ChatMessage> 변수 선언 및 초기화
121-
val chatHistory: List<ChatMessage> = if (cursorId != null) { // 처음이면 cursorId 없이 조회
122-
chatMongoRepository.findByIdLessThanAndTypeOrderByTimeDesc(cursorId, MessageType.CHAT, Pageable.ofSize(pageSize))
123-
} else { // 처음 아니면 cursorId로 조회
123+
// 채팅 기록 조회
124+
val chatHistory: List<ChatMessage> = if (cursorId.isNullOrBlank() || cursorId == "null") {
125+
// cursorId가 없으면: 최신 메시지 조회 (처음 불러올 때)
124126
chatMongoRepository.findAllByTypeOrderByTimeDesc(MessageType.CHAT, Pageable.ofSize(pageSize))
127+
} else {
128+
// cursorId가 있으면: 이전 메시지 이어서 조회 (cursorId보다 작은 메시지)
129+
chatMongoRepository.findByIdLessThanAndTypeOrderByTimeDesc(cursorId, MessageType.CHAT, Pageable.ofSize(pageSize))
125130
}
126131

132+
127133
// userId List 저장. 바로 뒤에 userId로 User 정보들을 먼저 찾고, 그 뒤에 DTO 변환
128134
val userIdList: List<Long> = chatHistory.mapNotNull { it -> it.userId }.toList()
129135

0 commit comments

Comments
 (0)