Skip to content

Commit

Permalink
#163 refactor: null 값 대신 optional 을 return 하도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
CDBchan committed Oct 6, 2023
1 parent e693b4d commit 348b3f8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 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 @@ -107,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 @@ -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<ChatRoomMessageResponse> mapToChatRoomMessage(ChatRoom chatRoom, Long memberId) {
Optional<ChatMessage> 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) {
Expand Down

0 comments on commit 348b3f8

Please sign in to comment.