diff --git a/secondhand/src/main/java/com/codesquad/secondhand/domain/chat/repository/ChatMessageQueryRepository.java b/secondhand/src/main/java/com/codesquad/secondhand/domain/chat/repository/ChatMessageQueryRepository.java index 36cb33e1d..df0e89354 100644 --- a/secondhand/src/main/java/com/codesquad/secondhand/domain/chat/repository/ChatMessageQueryRepository.java +++ b/secondhand/src/main/java/com/codesquad/secondhand/domain/chat/repository/ChatMessageQueryRepository.java @@ -2,6 +2,8 @@ import static com.codesquad.secondhand.domain.chat.entity.QChatMessage.*; +import java.util.Optional; + import org.springframework.stereotype.Repository; import com.codesquad.secondhand.domain.chat.entity.ChatMessage; @@ -15,12 +17,12 @@ public class ChatMessageQueryRepository { private final JPAQueryFactory query; - public ChatMessage findLatestMessage(ChatRoom chatRoom) { + public Optional findLatestMessage(ChatRoom chatRoom) { ChatMessage latestMessage = query .selectFrom(chatMessage) .where(chatMessage.chatRoom.eq(chatRoom)) .orderBy(chatMessage.sendAt.desc()) .fetchFirst(); - return latestMessage; + return Optional.ofNullable(latestMessage); } } diff --git a/secondhand/src/main/java/com/codesquad/secondhand/domain/chat/service/ChatQueryService.java b/secondhand/src/main/java/com/codesquad/secondhand/domain/chat/service/ChatQueryService.java index 11446f879..991965e47 100644 --- a/secondhand/src/main/java/com/codesquad/secondhand/domain/chat/service/ChatQueryService.java +++ b/secondhand/src/main/java/com/codesquad/secondhand/domain/chat/service/ChatQueryService.java @@ -1,6 +1,7 @@ package com.codesquad.secondhand.domain.chat.service; import java.util.List; +import java.util.Optional; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -60,7 +61,7 @@ public Long getUnReadCount(Member opponent, ChatRoom chatRoom) { return chatMessageJpaRepository.countUnreadMessages(opponent, chatRoom); } - public ChatMessage findLastMessage(ChatRoom chatRoom) { + public Optional findLastMessage(ChatRoom chatRoom) { return chatMessageQueryRepository.findLatestMessage(chatRoom); } diff --git a/secondhand/src/main/java/com/codesquad/secondhand/domain/chat/service/ChatService.java b/secondhand/src/main/java/com/codesquad/secondhand/domain/chat/service/ChatService.java index c6f6ae5ac..8c0038f8a 100644 --- a/secondhand/src/main/java/com/codesquad/secondhand/domain/chat/service/ChatService.java +++ b/secondhand/src/main/java/com/codesquad/secondhand/domain/chat/service/ChatService.java @@ -1,6 +1,7 @@ package com.codesquad.secondhand.domain.chat.service; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; import org.springframework.stereotype.Service; @@ -123,7 +124,11 @@ private ChatRoomOpponentResponse mapToChatRoomOpponent(ChatRoom chatRoom, Long m } private ChatRoomMessageResponse mapToChatRoomMessage(ChatRoom chatRoom, Long memberId) { - ChatMessage message = chatQueryService.findLastMessage(chatRoom); + Optional optionalMessage = chatQueryService.findLastMessage(chatRoom); + if (!optionalMessage.isPresent()) { + return null; + } + ChatMessage message = optionalMessage.get(); Member member = findOpponentMember(chatRoom, memberId); Long count = chatQueryService.getUnReadCount(member, chatRoom); return ChatRoomMessageResponse.of(message, count);