From 743e239b084813e162aee70ab940d6cee2ed791e Mon Sep 17 00:00:00 2001 From: eckrin Date: Tue, 21 Nov 2023 13:18:53 +0900 Subject: [PATCH 1/4] =?UTF-8?q?fix:=20=EB=A9=94=EC=84=B8=EC=A7=80=20?= =?UTF-8?q?=EC=A0=84=EC=86=A1=20=EC=8B=9C=EA=B0=81=EC=A0=95=EB=B3=B4=20?= =?UTF-8?q?=EC=A0=80=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/kusitms/jipbap/chat/domain/entity/Message.java | 3 ++- .../java/com/kusitms/jipbap/chat/service/MessageService.java | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/kusitms/jipbap/chat/domain/entity/Message.java b/src/main/java/com/kusitms/jipbap/chat/domain/entity/Message.java index 36ded5e..5956ec0 100644 --- a/src/main/java/com/kusitms/jipbap/chat/domain/entity/Message.java +++ b/src/main/java/com/kusitms/jipbap/chat/domain/entity/Message.java @@ -29,9 +29,10 @@ public class Message extends DateEntity { private Room room; // 대화 저장 - public Message(String senderName, Room room, String message) { + public Message(String senderName, Room room, String message, String sentTime) { this.senderName = senderName; this.room = room; this.message = message; + this.sentTime = sentTime; } } \ No newline at end of file diff --git a/src/main/java/com/kusitms/jipbap/chat/service/MessageService.java b/src/main/java/com/kusitms/jipbap/chat/service/MessageService.java index 84721b2..2370fc2 100644 --- a/src/main/java/com/kusitms/jipbap/chat/service/MessageService.java +++ b/src/main/java/com/kusitms/jipbap/chat/service/MessageService.java @@ -35,7 +35,7 @@ public void saveMessage(MessageDto messageDto) { ); // DB 저장 - Message message = new Message(messageDto.getSenderName(), room, messageDto.getMessage()); + Message message = new Message(messageDto.getSenderName(), room, messageDto.getMessage(), messageDto.getSentTime()); messageRepository.save(message); // 1. 직렬화 From 802b6015697071c19a1c3907b3a6b41cee8efc1e Mon Sep 17 00:00:00 2001 From: eckrin Date: Tue, 21 Nov 2023 13:32:44 +0900 Subject: [PATCH 2/4] =?UTF-8?q?fix:=20=EB=A9=94=EC=84=B8=EC=A7=80=20?= =?UTF-8?q?=EC=A0=84=EC=86=A1=EC=8B=9C=EA=B0=81=20=EC=B2=98=EB=A6=AC=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/kusitms/jipbap/chat/domain/dto/MessageDto.java | 1 - .../com/kusitms/jipbap/chat/domain/entity/Message.java | 5 ++++- .../com/kusitms/jipbap/chat/service/MessageService.java | 8 ++++++-- .../java/com/kusitms/jipbap/chat/service/RoomService.java | 5 +++++ 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/kusitms/jipbap/chat/domain/dto/MessageDto.java b/src/main/java/com/kusitms/jipbap/chat/domain/dto/MessageDto.java index 1965576..443b5bf 100644 --- a/src/main/java/com/kusitms/jipbap/chat/domain/dto/MessageDto.java +++ b/src/main/java/com/kusitms/jipbap/chat/domain/dto/MessageDto.java @@ -15,7 +15,6 @@ public class MessageDto { private String senderName; private String roomId; private String message; - private String sentTime; // 대화 조회 public MessageDto(Message message) { diff --git a/src/main/java/com/kusitms/jipbap/chat/domain/entity/Message.java b/src/main/java/com/kusitms/jipbap/chat/domain/entity/Message.java index 5956ec0..e46e770 100644 --- a/src/main/java/com/kusitms/jipbap/chat/domain/entity/Message.java +++ b/src/main/java/com/kusitms/jipbap/chat/domain/entity/Message.java @@ -29,10 +29,13 @@ public class Message extends DateEntity { private Room room; // 대화 저장 - public Message(String senderName, Room room, String message, String sentTime) { + public Message(String senderName, Room room, String message) { this.senderName = senderName; this.room = room; this.message = message; + } + + public void updateSentTime(String sentTime) { this.sentTime = sentTime; } } \ No newline at end of file diff --git a/src/main/java/com/kusitms/jipbap/chat/service/MessageService.java b/src/main/java/com/kusitms/jipbap/chat/service/MessageService.java index 2370fc2..0e14f05 100644 --- a/src/main/java/com/kusitms/jipbap/chat/service/MessageService.java +++ b/src/main/java/com/kusitms/jipbap/chat/service/MessageService.java @@ -14,7 +14,9 @@ import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; @@ -29,14 +31,15 @@ public class MessageService { private final ObjectMapper objectMapper; // 메세지 저장 + @Transactional public void saveMessage(MessageDto messageDto) { Room room = roomRepository.findByRoomId(messageDto.getRoomId()).orElseThrow( ()->new RoomNotExistsException("채팅방이 더 이상 존재하지 않습니다.") ); // DB 저장 - Message message = new Message(messageDto.getSenderName(), room, messageDto.getMessage(), messageDto.getSentTime()); - messageRepository.save(message); + Message message = messageRepository.save(new Message(messageDto.getSenderName(), room, messageDto.getMessage())); + message.updateSentTime(message.getCreatedAt().toString()); // 1. 직렬화 redisTemplateMessage.setValueSerializer(new GenericJackson2JsonRedisSerializer(objectMapper)); @@ -49,6 +52,7 @@ public void saveMessage(MessageDto messageDto) { } // 6. 대화 조회 - Redis & DB (TLB 캐시전략 유사) + @Transactional public List loadMessage(String roomId) { List messageList = new ArrayList<>(); diff --git a/src/main/java/com/kusitms/jipbap/chat/service/RoomService.java b/src/main/java/com/kusitms/jipbap/chat/service/RoomService.java index 0f1039c..7462882 100644 --- a/src/main/java/com/kusitms/jipbap/chat/service/RoomService.java +++ b/src/main/java/com/kusitms/jipbap/chat/service/RoomService.java @@ -22,6 +22,7 @@ import org.springframework.data.redis.listener.ChannelTopic; import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.HashMap; @@ -60,6 +61,7 @@ private void init() { } // 채팅방 생성 + @Transactional public MessageResponseDto createRoom(MessageRequestDto dto, String email) { User sender = userRepository.findByEmail(email).orElseThrow(()->new InvalidEmailException("회원정보가 존재하지 않습니다.")); // User receiver = userRepository.findById(dto.getReceiverId()).orElseThrow(()->new InvalidEmailException("수신 회원정보가 존재하지 않습니다.")); @@ -82,6 +84,7 @@ public MessageResponseDto createRoom(MessageRequestDto dto, String email) { } // 7. 사용자 관련 채팅방 전체 조회 (생성, 수신) + @Transactional public List findAllRoomByUser(String email) { User user = userRepository.findByEmail(email).orElseThrow(()->new InvalidEmailException("회원정보가 존재하지 않습니다.")); List messageRooms = roomRepository.findByUserOrReceiverName(user, user.getUsername()); // sender & receiver 모두 해당 채팅방 조회 가능 (1:1 대화) @@ -130,6 +133,7 @@ public List findAllRoomByUser(String email) { } // 사용자 관련 채팅방 선택 조회 + @Transactional public RoomDto findRoom(String roomId, String email) { Room room = roomRepository.findByRoomId(roomId).orElseThrow( ()->new RoomNotExistsException("채팅방 정보를 찾을 수 없습니다.") @@ -163,6 +167,7 @@ public RoomDto findRoom(String roomId, String email) { } // 10. 채팅방 삭제 + @Transactional public void deleteRoom(Long id, String email) { User user = userRepository.findByEmail(email).orElseThrow(()->new InvalidEmailException("회원정보가 존재하지 않습니다.")); From 494e99a0516745956c99a3c99a1dff7a4d8f397e Mon Sep 17 00:00:00 2001 From: eckrin Date: Tue, 21 Nov 2023 13:37:52 +0900 Subject: [PATCH 3/4] =?UTF-8?q?fix:=20=EC=B1=84=ED=8C=85=EB=B0=A9=20?= =?UTF-8?q?=EB=A6=AC=EC=8A=A4=ED=8A=B8=20=EC=A1=B0=ED=9A=8C=20=EC=8B=9C=20?= =?UTF-8?q?=EB=A7=88=EC=A7=80=EB=A7=89=20=EC=B1=84=ED=8C=85=EC=8B=9C?= =?UTF-8?q?=EC=A0=90=20=EC=88=9C=EC=9C=BC=EB=A1=9C=20=EC=A0=95=EB=A0=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kusitms/jipbap/chat/domain/dto/MessageResponseDto.java | 2 +- .../com/kusitms/jipbap/chat/repository/MessageRepository.java | 1 + .../java/com/kusitms/jipbap/chat/service/RoomService.java | 4 ++-- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/kusitms/jipbap/chat/domain/dto/MessageResponseDto.java b/src/main/java/com/kusitms/jipbap/chat/domain/dto/MessageResponseDto.java index 903da35..93621f2 100644 --- a/src/main/java/com/kusitms/jipbap/chat/domain/dto/MessageResponseDto.java +++ b/src/main/java/com/kusitms/jipbap/chat/domain/dto/MessageResponseDto.java @@ -17,7 +17,7 @@ public class MessageResponseDto { private String roomId; private String receiver; private String message; - private String createdAt; + private String createdAt; // (채팅방 조회시) 메세지 마지막 전송 시각 // 채팅방 생성 public MessageResponseDto(Room room) { diff --git a/src/main/java/com/kusitms/jipbap/chat/repository/MessageRepository.java b/src/main/java/com/kusitms/jipbap/chat/repository/MessageRepository.java index 52251d4..3132073 100644 --- a/src/main/java/com/kusitms/jipbap/chat/repository/MessageRepository.java +++ b/src/main/java/com/kusitms/jipbap/chat/repository/MessageRepository.java @@ -11,4 +11,5 @@ public interface MessageRepository extends JpaRepository { List findTop100ByRoomOrderById(Room room); Message findTopByRoomOrderById(Room room); + Message findTopByRoomOrderBySentTimeDesc(Room room); } \ No newline at end of file diff --git a/src/main/java/com/kusitms/jipbap/chat/service/RoomService.java b/src/main/java/com/kusitms/jipbap/chat/service/RoomService.java index 7462882..99366e9 100644 --- a/src/main/java/com/kusitms/jipbap/chat/service/RoomService.java +++ b/src/main/java/com/kusitms/jipbap/chat/service/RoomService.java @@ -102,7 +102,7 @@ public List findAllRoomByUser(String email) { messageRoom.getReceiverName()); // 8. 가장 최신 메시지 & 생성 시간 조회 - Message latestMessage = messageRepository.findTopByRoomOrderById(messageRoom); + Message latestMessage = messageRepository.findTopByRoomOrderBySentTimeDesc(messageRoom); if (latestMessage != null) { messageRoomDto.setLatestMessageCreatedAt(latestMessage.getSentTime()); messageRoomDto.setLatestMessageContent(latestMessage.getMessage()); @@ -119,7 +119,7 @@ public List findAllRoomByUser(String email) { messageRoom.getReceiverName()); // 가장 최신 메시지 & 생성 시간 조회 - Message latestMessage = messageRepository.findTopByRoomOrderById(messageRoom); + Message latestMessage = messageRepository.findTopByRoomOrderBySentTimeDesc(messageRoom); if (latestMessage != null) { messageRoomDto.setLatestMessageCreatedAt(latestMessage.getSentTime()); messageRoomDto.setLatestMessageContent(latestMessage.getMessage()); From e562b602dee5e974c6fc648911cb1d1658180e65 Mon Sep 17 00:00:00 2001 From: eckrin Date: Tue, 21 Nov 2023 13:45:15 +0900 Subject: [PATCH 4/4] =?UTF-8?q?fix:=20=EC=B1=84=ED=8C=85=EB=B0=A9=20?= =?UTF-8?q?=EC=A0=95=EB=B3=B4=20=ED=95=84=EB=93=9C=EB=AA=85=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 --- .../kusitms/jipbap/chat/domain/dto/MessageResponseDto.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/kusitms/jipbap/chat/domain/dto/MessageResponseDto.java b/src/main/java/com/kusitms/jipbap/chat/domain/dto/MessageResponseDto.java index 93621f2..1aad6d5 100644 --- a/src/main/java/com/kusitms/jipbap/chat/domain/dto/MessageResponseDto.java +++ b/src/main/java/com/kusitms/jipbap/chat/domain/dto/MessageResponseDto.java @@ -17,7 +17,7 @@ public class MessageResponseDto { private String roomId; private String receiver; private String message; - private String createdAt; // (채팅방 조회시) 메세지 마지막 전송 시각 + private String lastMessageSentTime; // (채팅방 조회시) 메세지 마지막 전송 시각 // 채팅방 생성 public MessageResponseDto(Room room) { @@ -46,6 +46,6 @@ public void setLatestMessageContent(String message) { } public void setLatestMessageCreatedAt(String createdAt) { - this.createdAt = createdAt; + this.lastMessageSentTime = createdAt; } } \ No newline at end of file