diff --git a/src/main/java/com/catchroom/chat/chatroom/controller/ChatRoomController.java b/src/main/java/com/catchroom/chat/chatroom/controller/ChatRoomController.java index 3c30f54..1ceead1 100644 --- a/src/main/java/com/catchroom/chat/chatroom/controller/ChatRoomController.java +++ b/src/main/java/com/catchroom/chat/chatroom/controller/ChatRoomController.java @@ -11,7 +11,6 @@ @RequestMapping("/v1/chat/room") @RequiredArgsConstructor @CrossOrigin(origins = "https://localhost:3000", allowedHeaders = "*") -@Slf4j public class ChatRoomController { private final ChatRoomService chatRoomService; @@ -19,7 +18,6 @@ public class ChatRoomController { public List getChatRoomList( @RequestHeader("Authorization") String accessToken ) { - log.error("accesstoken = " + accessToken); return chatRoomService.getChatRoomList(accessToken); } diff --git a/src/main/java/com/catchroom/chat/chatroom/service/ChatRoomService.java b/src/main/java/com/catchroom/chat/chatroom/service/ChatRoomService.java index e046336..1e2f02b 100644 --- a/src/main/java/com/catchroom/chat/chatroom/service/ChatRoomService.java +++ b/src/main/java/com/catchroom/chat/chatroom/service/ChatRoomService.java @@ -1,6 +1,7 @@ package com.catchroom.chat.chatroom.service; import com.catchroom.chat.chatroom.dto.ChatRoomListGetResponse; +import com.catchroom.chat.chatroom.entity.ChatRoom; import com.catchroom.chat.feign.client.MainFeignClient; import com.catchroom.chat.message.dto.ChatMessageDto; import com.catchroom.chat.message.entity.ChatMessage; @@ -19,14 +20,24 @@ public class ChatRoomService { public List getChatRoomList(String accessToken) { - List chatRoomListGetResponseList = mainFeignClient.getChatRoomList(accessToken); - for (ChatRoomListGetResponse chatRoomListGetResponse : chatRoomListGetResponseList) { - chatRoomListGetResponse.updateChatMessageDto(chatRoomRepository.getLastMessage(chatRoomListGetResponse.getChatRoomNumber())); -// if (chatRoomRepository.getLastMessage(chatRoomListGetResponse.getChatRoomNumber()) == null) { -// ChatMessage lastChatMessage = chatMessageRepository.findAllByRoomId(chatRoomListGetResponse.getChatRoomNumber()).get(0); -// chatRoomListGetResponse.updateChatMessageDto(ChatMessageDto.fromEntity(lastChatMessage)); -// } + List chatRoomList = mainFeignClient.getChatRoomList(accessToken); + for (ChatRoomListGetResponse chatRoomListGetResponse : chatRoomList) { + recallLastMessage(chatRoomListGetResponse); + } + return chatRoomList; + } + + private void recallLastMessage(ChatRoomListGetResponse chatRoomListGetResponse) { + String chatRoomNumber = chatRoomListGetResponse.getChatRoomNumber(); + //최신 메세지가 레디스에 있는 경우 + if (chatRoomRepository.getLastMessage(chatRoomNumber) != null) { + chatRoomListGetResponse.updateChatMessageDto(chatRoomRepository.getLastMessage(chatRoomNumber)); + } + //최신 메세지가 레디스에 없고 몽고디비에는 있는 경우 + else if (chatRoomRepository.getLastMessage(chatRoomNumber) == null && + !chatMessageRepository.findAllByRoomId(chatRoomNumber).isEmpty()) { + ChatMessage lastChatMessageMongo = chatMessageRepository.findAllByRoomId(chatRoomNumber).get(0); + chatRoomListGetResponse.updateChatMessageDto(ChatMessageDto.fromEntity(lastChatMessageMongo)); } - return chatRoomListGetResponseList; } }