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/dto/MessageResponseDto.java b/src/main/java/com/kusitms/jipbap/chat/domain/dto/MessageResponseDto.java index 903da35..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 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..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 @@ -34,4 +34,8 @@ public Message(String senderName, Room room, String message) { 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/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/MessageService.java b/src/main/java/com/kusitms/jipbap/chat/service/MessageService.java index 84721b2..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()); - 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..99366e9 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 대화) @@ -99,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()); @@ -116,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()); @@ -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("회원정보가 존재하지 않습니다."));