From 348b3f8a2cde9efd8eaa72c9aedd97b8cb594aed Mon Sep 17 00:00:00 2001 From: CDBchan Date: Fri, 6 Oct 2023 14:57:51 +0900 Subject: [PATCH] =?UTF-8?q?#163=20refactor:=20null=20=EA=B0=92=20=EB=8C=80?= =?UTF-8?q?=EC=8B=A0=20optional=20=EC=9D=84=20return=20=ED=95=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat/dto/response/ChatRoomMessageResponse.java | 8 ++++++++ .../secondhand/domain/chat/service/ChatService.java | 9 +++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/secondhand/src/main/java/com/codesquad/secondhand/domain/chat/dto/response/ChatRoomMessageResponse.java b/secondhand/src/main/java/com/codesquad/secondhand/domain/chat/dto/response/ChatRoomMessageResponse.java index a5cbe032a..d5e497631 100644 --- a/secondhand/src/main/java/com/codesquad/secondhand/domain/chat/dto/response/ChatRoomMessageResponse.java +++ b/secondhand/src/main/java/com/codesquad/secondhand/domain/chat/dto/response/ChatRoomMessageResponse.java @@ -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()) 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 8c0038f8a..f4fac62c7 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 @@ -107,7 +107,8 @@ public List 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()); @@ -123,15 +124,15 @@ private ChatRoomOpponentResponse mapToChatRoomOpponent(ChatRoom chatRoom, Long m return ChatRoomOpponentResponse.of(member.getNickname(), member.getProfileImg()); } - private ChatRoomMessageResponse mapToChatRoomMessage(ChatRoom chatRoom, Long memberId) { + private Optional mapToChatRoomMessage(ChatRoom chatRoom, Long memberId) { Optional optionalMessage = chatQueryService.findLastMessage(chatRoom); if (!optionalMessage.isPresent()) { - return null; + 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) {