From 145b77f84c6adbecb90f08ae9616c180144b96b1 Mon Sep 17 00:00:00 2001 From: sungjiwoon Date: Sat, 27 Jan 2024 14:42:00 +0900 Subject: [PATCH 1/2] =?UTF-8?q?fix=20:=20redis=20config=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat/global/config/RedisConfig.java | 3 ++ .../repository/ChatRoomRedisRepository.java | 10 ++-- .../ChatRoomRedisRepositoryTest.java | 48 +++++++++++++++++++ 3 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 src/test/java/com/catchroom/chat/message/repository/ChatRoomRedisRepositoryTest.java diff --git a/src/main/java/com/catchroom/chat/global/config/RedisConfig.java b/src/main/java/com/catchroom/chat/global/config/RedisConfig.java index 5d26a81..23e254b 100644 --- a/src/main/java/com/catchroom/chat/global/config/RedisConfig.java +++ b/src/main/java/com/catchroom/chat/global/config/RedisConfig.java @@ -86,6 +86,9 @@ public RedisTemplate redisTemplate(RedisConnectionFactory connec redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(String.class)); + redisTemplate.setHashKeySerializer(new StringRedisSerializer()); + redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class)); + return redisTemplate; } diff --git a/src/main/java/com/catchroom/chat/message/repository/ChatRoomRedisRepository.java b/src/main/java/com/catchroom/chat/message/repository/ChatRoomRedisRepository.java index a2fec8b..6631403 100644 --- a/src/main/java/com/catchroom/chat/message/repository/ChatRoomRedisRepository.java +++ b/src/main/java/com/catchroom/chat/message/repository/ChatRoomRedisRepository.java @@ -2,9 +2,7 @@ import com.catchroom.chat.chatroom.dto.ChatRoomListGetResponse; import com.catchroom.chat.message.dto.ChatMessageDto; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.annotation.Resource; import java.util.Arrays; @@ -38,6 +36,8 @@ public class ChatRoomRedisRepository { private final RedisTemplate redisTemplate; + private final ObjectMapper objectMapper; + @Resource(name = "redisTemplate") private HashOperations opsHashChatRoom; @@ -78,11 +78,11 @@ public void deleteChatRoom(Long userId, String roomId) { } public ChatRoomListGetResponse getChatRoom(Long userId, String roomId) { - return opsHashChatRoom.get(getChatRoomKey(userId), roomId); + return objectMapper.convertValue(opsHashChatRoom.get(getChatRoomKey(userId), roomId), ChatRoomListGetResponse.class); } public List getChatRoomList(Long userId) { - return opsHashChatRoom.values(getChatRoomKey(userId)); + return objectMapper.convertValue(opsHashChatRoom.values(getChatRoomKey(userId)), new TypeReference<>() {}); } @@ -91,7 +91,7 @@ public void setLastChatMessage(String roomId, ChatMessageDto chatMessageDto) { } public ChatMessageDto getLastMessage(String roomId) { - return opsHashLastChatMessage.get(CHAT_ROOM, roomId); + return objectMapper.convertValue(opsHashLastChatMessage.get(CHAT_ROOM, roomId), ChatMessageDto.class); } diff --git a/src/test/java/com/catchroom/chat/message/repository/ChatRoomRedisRepositoryTest.java b/src/test/java/com/catchroom/chat/message/repository/ChatRoomRedisRepositoryTest.java new file mode 100644 index 0000000..ba9df8a --- /dev/null +++ b/src/test/java/com/catchroom/chat/message/repository/ChatRoomRedisRepositoryTest.java @@ -0,0 +1,48 @@ +package com.catchroom.chat.message.repository; + +import static org.junit.jupiter.api.Assertions.*; + +import com.catchroom.chat.chatroom.dto.ChatRoomListGetResponse; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.util.List; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class ChatRoomRedisRepositoryTest { + + @Autowired + private ChatRoomRedisRepository chatRoomRedisRepository; + + @Autowired + ObjectMapper objectMapper; + + Logger log = (Logger) LoggerFactory.getLogger(ChatRoomRedisRepositoryTest.class); + + @Test + void 채팅방_값_저장() { + Long userId = 68L; + } + + @Test + void 채팅방_값이_제대로_저장됐을까() { + Long userId = 10000L; + List chatRoomListGetResponseList = chatRoomRedisRepository.getChatRoomList(userId); + for (ChatRoomListGetResponse chatRoomListGetResponse : chatRoomListGetResponseList) { + log.info( + chatRoomListGetResponse.toString() + ); + } + } + + @Test + void 채팅방_값이_제대로_불러와질까() { + Long userId = 10000L; + ChatRoomListGetResponse chatRoomListGetResponse = chatRoomRedisRepository.getChatRoom(userId, "c29cd80e-d289-49ff-ac91-f3b32dd4b633"); + log.info(chatRoomListGetResponse.toString()); + } +} \ No newline at end of file From 70fb8f0b5718f70407076095d33091d98a83f5b4 Mon Sep 17 00:00:00 2001 From: sungjiwoon Date: Sat, 27 Jan 2024 14:42:37 +0900 Subject: [PATCH 2/2] =?UTF-8?q?fix=20:=20isNego=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chatroom/dto/ChatRoomListGetResponse.java | 27 +++++++++++++++++-- .../chat/message/service/ChatService.java | 3 ++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/catchroom/chat/chatroom/dto/ChatRoomListGetResponse.java b/src/main/java/com/catchroom/chat/chatroom/dto/ChatRoomListGetResponse.java index 8be29f1..582c9c4 100644 --- a/src/main/java/com/catchroom/chat/chatroom/dto/ChatRoomListGetResponse.java +++ b/src/main/java/com/catchroom/chat/chatroom/dto/ChatRoomListGetResponse.java @@ -43,6 +43,8 @@ public class ChatRoomListGetResponse implements Serializable { private ChatMessageDto lastChatmessageDto; + private Boolean isNego; + public void updateChatMessageDto(ChatMessageDto chatMessageDto) { this.lastChatmessageDto = chatMessageDto; @@ -53,10 +55,31 @@ public void changePartnerInfo() { this.myNickName = partnerNickName; this.partnerNickName = tmp; - if (loginUserIdentity.equals(UserIdentity.SELLER)) { + if (this.loginUserIdentity.equals(UserIdentity.SELLER)) { this.loginUserIdentity = UserIdentity.BUYER; - } else if (loginUserIdentity.equals(UserIdentity.BUYER)) { + } else if (this.loginUserIdentity.equals(UserIdentity.BUYER)) { this.loginUserIdentity = UserIdentity.SELLER; } } + + @Override + public String toString() { + return "ChatRoomListGetResponse{" + + "chatRoomNumber='" + chatRoomNumber + '\'' + + ", buyerId=" + buyerId + + ", sellerId=" + sellerId + + ", productId=" + productId + + ", accommodationName='" + accommodationName + '\'' + + ", sellPrice=" + sellPrice + + ", loginUserIdentity=" + loginUserIdentity + + ", accommodationUrl='" + accommodationUrl + '\'' + + ", myNickName='" + myNickName + '\'' + + ", partnerNickName='" + partnerNickName + '\'' + + ", dealState=" + dealState + + ", buyerState=" + buyerState + + ", sellerState=" + sellerState + + ", lastChatmessageDto=" + lastChatmessageDto + + ", isNego=" + isNego + + '}'; + } } diff --git a/src/main/java/com/catchroom/chat/message/service/ChatService.java b/src/main/java/com/catchroom/chat/message/service/ChatService.java index 2e49e19..f04a795 100644 --- a/src/main/java/com/catchroom/chat/message/service/ChatService.java +++ b/src/main/java/com/catchroom/chat/message/service/ChatService.java @@ -124,11 +124,12 @@ private void setNewChatRoomInfo(ChatMessageDto chatMessage, ChatRoomListGetRespo newChatRoomListResponse.changePartnerInfo(); //닉네임 체인지 chatRoomRedisRepository.setChatRoom(newChatRoomListResponse.getBuyerId(), chatMessage.getRoomId(), newChatRoomListResponse); - } else { + } else if (newChatRoomListResponse.getLoginUserIdentity().equals(UserIdentity.BUYER)){ if (!chatMessage.getType().equals(MessageType.DELETE)) { chatRoomRedisRepository.setChatRoom(newChatRoomListResponse.getBuyerId(), chatMessage.getRoomId(), newChatRoomListResponse); } + newChatRoomListResponse.changePartnerInfo(); //닉네임 체인지 chatRoomRedisRepository.setChatRoom(newChatRoomListResponse.getSellerId(), chatMessage.getRoomId(), newChatRoomListResponse); }