From 6fa58d5737fea2b626411b70dabd7f30008c5d0c Mon Sep 17 00:00:00 2001 From: dd-jiyun Date: Sun, 24 Mar 2024 14:53:21 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EB=A9=94=EC=84=B8=EC=A7=80=ED=95=A8=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../message/controller/MessageController.java | 2 +- .../message/repository/MessageRepository.java | 3 +++ .../message/service/MessageService.java | 24 +++++++++++++++++-- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/Titto_Backend/src/main/java/com/example/titto_backend/message/controller/MessageController.java b/Titto_Backend/src/main/java/com/example/titto_backend/message/controller/MessageController.java index 7dfeca4..8ca2997 100644 --- a/Titto_Backend/src/main/java/com/example/titto_backend/message/controller/MessageController.java +++ b/Titto_Backend/src/main/java/com/example/titto_backend/message/controller/MessageController.java @@ -48,7 +48,7 @@ public ResponseEntity writeMessage(@RequestBody MessageDTO.Request reque // 쪽지함 목록 조회 @GetMapping("/all") public ResponseEntity> getAllMessages(@AuthenticationPrincipal UserDetails userDetails) { - List userMessages = messageService.getUserMessages(userDetails.getUsername()); + List userMessages = messageService.getUserConversationsMessages(userDetails.getUsername()); List previews = userMessages.stream() .map(MessageDTO.Preview::new) .collect(Collectors.toList()); diff --git a/Titto_Backend/src/main/java/com/example/titto_backend/message/repository/MessageRepository.java b/Titto_Backend/src/main/java/com/example/titto_backend/message/repository/MessageRepository.java index 71615ad..bf12f02 100644 --- a/Titto_Backend/src/main/java/com/example/titto_backend/message/repository/MessageRepository.java +++ b/Titto_Backend/src/main/java/com/example/titto_backend/message/repository/MessageRepository.java @@ -15,4 +15,7 @@ List findBySenderAndReceiverAndDeletedBySenderFalseOrReceiverAndSenderA List findBySenderAndDeletedBySenderFalseOrReceiverAndDeletedByReceiverFalseOrderBySentAtDesc(User sender, User receiver); + List findBySenderAndDeletedBySenderFalseOrderBySentAtDesc(User user); + + List findByReceiverAndDeletedByReceiverFalseOrderBySentAtDesc(User user); } diff --git a/Titto_Backend/src/main/java/com/example/titto_backend/message/service/MessageService.java b/Titto_Backend/src/main/java/com/example/titto_backend/message/service/MessageService.java index 9224855..e51ff55 100644 --- a/Titto_Backend/src/main/java/com/example/titto_backend/message/service/MessageService.java +++ b/Titto_Backend/src/main/java/com/example/titto_backend/message/service/MessageService.java @@ -7,6 +7,8 @@ import com.example.titto_backend.message.domain.Message; import com.example.titto_backend.message.dto.MessageDTO; import com.example.titto_backend.message.repository.MessageRepository; +import java.util.ArrayList; +import java.util.Comparator; import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -54,14 +56,32 @@ public List getBothMessages(String email, Long selectedUser } @Transactional - public List getUserMessages(String email) { + public List getUserConversationsMessages(String email) { User user = userRepository.findByEmail(email) .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); - List userMessages = messageRepository.findBySenderAndDeletedBySenderFalseOrReceiverAndDeletedByReceiverFalseOrderBySentAtDesc(user, user); + List userSentMessages = messageRepository.findBySenderAndDeletedBySenderFalseOrderBySentAtDesc(user); + List userReceivedMessages = messageRepository.findByReceiverAndDeletedByReceiverFalseOrderBySentAtDesc(user); + + List userMessages = new ArrayList<>(); + userMessages.addAll(userSentMessages); + userMessages.addAll(userReceivedMessages); + + // 메시지를 시간 순서대로 정렬 + userMessages.sort(Comparator.comparing(Message::getSentAt).reversed()); + return userMessages; } +// @Transactional +// public List getUserMessages(String email) { +// User user = userRepository.findByEmail(email) +// .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); +// +// List userMessages = messageRepository.findBySenderAndDeletedBySenderFalseOrReceiverAndDeletedByReceiverFalseOrderBySentAtDesc(user, user); +// return userMessages; +// } + // 메세지함에서 사용자 별 가장 최근 메세지만 보내줌. // @Transactional // public Map getUserConversations(String email) {