From 2c2b2001633b7de4275c0775ca374287d58d5d75 Mon Sep 17 00:00:00 2001 From: dd-jiyun Date: Thu, 7 Mar 2024 16:30:23 +0900 Subject: [PATCH] feat: Delete All message MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 메세지 전체 조회시 삭제된 메세지가 안뜸. - 서로 주고 받은 메세지를 삭제하면 삭제한 사람에겐 메세지가 안뜸. - 삭제를 하지 않은 사용자에게는 메세지가 그대로 나옴. --- .../message/controller/MessageController.java | 18 +++++++++++++ .../message/service/MessageService.java | 26 +++++++++++++++++-- 2 files changed, 42 insertions(+), 2 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 a839a36..0e589d1 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 @@ -119,5 +119,23 @@ public String deleteMessage( messageService.deleteMessage(messageId, userDetails.getUsername()); return "메시지 삭제 성공"; } + + // TODO : 전체 메세지 삭제 (sender인 유저가 receiver유저와 한 대화내용을 삭제 / 지운사람한테만 지워짐.) + @PutMapping("/delete-all/{seletedUserId}") + @Operation( + summary = "전체 메시지 삭제", + description = "선택한 유저와의 전체 메시지를 삭제합니다.", + responses = { + @ApiResponse(responseCode = "200", description = "요청 성공"), + @ApiResponse(responseCode = "400", description = "잘못된 요청"), + @ApiResponse(responseCode = "500", description = "관리자 문의") + }) + public String deleteAllMessage( + @PathVariable Long seletedUserId, + @AuthenticationPrincipal UserDetails userDetails) { + messageService.deleteAllMessages(userDetails.getUsername(), seletedUserId); + return "메시지 전체 삭제 성공"; + } + } 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 d8307b8..9d6ae1d 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 @@ -56,7 +56,10 @@ public List getBothMessages(String email, Long selectedUserId) { public List getAllMessagePreview(String email) { User user = userRepository.findByEmail(email) .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); - + if (user.getReceivedMessages().stream().anyMatch(Message::isDeleted)) { + return convertMessagesToPreviewDTO( + user.getReceivedMessages().stream().filter(message -> !message.isDeleted()).toList()); + } List messages = messageRepository.findAllByReceiver(user); return convertMessagesToPreviewDTO(messages); } @@ -79,7 +82,10 @@ public List getMessagesByReceiver(String email) { public List getMessagesBySender(String email) { User sender = userRepository.findByEmail(email) .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); - + if (sender.getSentMessages().stream().anyMatch(Message::isDeleted)) { + return convertMessagesToDTO( + sender.getSentMessages().stream().filter(message -> !message.isDeleted()).toList()); + } List messages = messageRepository.findAllBySender(sender); return convertMessagesToDTO(messages); } @@ -106,6 +112,22 @@ public void deleteMessage(Long messageId, String email) { } } + // 메세지 전체 삭제 + @Transactional + public void deleteAllMessages(String email, Long selectedUserId) { + User user = userRepository.findByEmail(email) + .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); + + User selectedUser = userRepository.findById(selectedUserId) + .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); + + List messages = messageRepository.findAllBySenderAndReceiverOrReceiverAndSender(user, selectedUser, + user, selectedUser); + for (Message message : messages) { + message.setDeleted(true); + } + } + private List convertMessagesToDTO(List messages) { return messages.stream() .map(MessageDTO.Response::new)