Skip to content

Commit

Permalink
#163 refactor: 채팅방목록 조회시 메세지가 없다면 null 을반환하도록 한다. (#164)
Browse files Browse the repository at this point in the history
* #163 refactor: 채팅방목록 조회시 메세지가 없다면 null 을반환하도록 한다.

* #163 refactor: null 값 대신 optional 을 return 하도록 수정
  • Loading branch information
CDBchan authored Oct 6, 2023
1 parent 097d5ac commit 0b3bf10
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,22 @@

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Builder
@NoArgsConstructor
public class ChatRoomMessageResponse {
private LocalDateTime sendAt;
private String message;
private Long unreadMessageCount;

public ChatRoomMessageResponse(LocalDateTime sendAt, String message, Long unreadMessageCount) {
this.sendAt = sendAt;
this.message = message;
this.unreadMessageCount = unreadMessageCount;
}

public static ChatRoomMessageResponse of(ChatMessage chatMessage, Long count) {
return ChatRoomMessageResponse.builder()
.sendAt(chatMessage.getSendAt())
Expand Down
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 @@ -106,7 +107,8 @@ public List<ChatRoomListResponse> getChatRoomList(Long memberId) {
.map(chatRoom -> {
ChatRoomProductResponse productResponse = mapToChatRoomProduct(chatRoom);
ChatRoomOpponentResponse opponentResponse = mapToChatRoomOpponent(chatRoom, memberId);
ChatRoomMessageResponse chatRoomMessageResponse = mapToChatRoomMessage(chatRoom, memberId);
ChatRoomMessageResponse chatRoomMessageResponse = mapToChatRoomMessage(chatRoom, memberId)
.orElseGet(() -> new ChatRoomMessageResponse());
return ChatRoomListResponse.of(chatRoom.getId(), productResponse, opponentResponse,
chatRoomMessageResponse);
}).collect(Collectors.toList());
Expand All @@ -122,11 +124,15 @@ private ChatRoomOpponentResponse mapToChatRoomOpponent(ChatRoom chatRoom, Long m
return ChatRoomOpponentResponse.of(member.getNickname(), member.getProfileImg());
}

private ChatRoomMessageResponse mapToChatRoomMessage(ChatRoom chatRoom, Long memberId) {
ChatMessage message = chatQueryService.findLastMessage(chatRoom);
private Optional<ChatRoomMessageResponse> mapToChatRoomMessage(ChatRoom chatRoom, Long memberId) {
Optional<ChatMessage> optionalMessage = chatQueryService.findLastMessage(chatRoom);
if (!optionalMessage.isPresent()) {
return Optional.empty();
}
ChatMessage message = optionalMessage.get();
Member member = findOpponentMember(chatRoom, memberId);
Long count = chatQueryService.getUnReadCount(member, chatRoom);
return ChatRoomMessageResponse.of(message, count);
return Optional.of(ChatRoomMessageResponse.of(message, count));
}

private Member findOpponentMember(ChatRoom chatRoom, Long memberId) {
Expand Down

0 comments on commit 0b3bf10

Please sign in to comment.