Skip to content

Commit

Permalink
#163 refactor: 채팅방목록 조회시 메세지가 없다면 null 을반환하도록 한다.
Browse files Browse the repository at this point in the history
  • Loading branch information
CDBchan committed Oct 6, 2023
1 parent 097d5ac commit e693b4d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,12 +17,12 @@
public class ChatMessageQueryRepository {
private final JPAQueryFactory query;

public ChatMessage findLatestMessage(ChatRoom chatRoom) {
public Optional<ChatMessage> findLatestMessage(ChatRoom chatRoom) {
ChatMessage latestMessage = query
.selectFrom(chatMessage)
.where(chatMessage.chatRoom.eq(chatRoom))
.orderBy(chatMessage.sendAt.desc())
.fetchFirst();
return latestMessage;
return Optional.ofNullable(latestMessage);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -60,7 +61,7 @@ public Long getUnReadCount(Member opponent, ChatRoom chatRoom) {
return chatMessageJpaRepository.countUnreadMessages(opponent, chatRoom);
}

public ChatMessage findLastMessage(ChatRoom chatRoom) {
public Optional<ChatMessage> findLastMessage(ChatRoom chatRoom) {
return chatMessageQueryRepository.findLatestMessage(chatRoom);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -123,7 +124,11 @@ private ChatRoomOpponentResponse mapToChatRoomOpponent(ChatRoom chatRoom, Long m
}

private ChatRoomMessageResponse mapToChatRoomMessage(ChatRoom chatRoom, Long memberId) {
ChatMessage message = chatQueryService.findLastMessage(chatRoom);
Optional<ChatMessage> 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);
Expand Down

0 comments on commit e693b4d

Please sign in to comment.