Skip to content

Commit

Permalink
Merge pull request #148 from dd-jiyun/main
Browse files Browse the repository at this point in the history
๋ฉ”์„ธ์ง€ ์ „์ฒด ์‚ญ์ œ ๊ธฐ๋Šฅ
  • Loading branch information
dd-jiyun authored Mar 7, 2024
2 parents 3514d06 + e7ed0fc commit c1d0a46
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 "๋ฉ”์‹œ์ง€ ์ „์ฒด ์‚ญ์ œ ์„ฑ๊ณต";
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ public List<Response> getBothMessages(String email, Long selectedUserId) {
public List<MessageDTO.Preview> 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<Message> messages = messageRepository.findAllByReceiver(user);
return convertMessagesToPreviewDTO(messages);
}
Expand All @@ -79,7 +82,10 @@ public List<MessageDTO.Response> getMessagesByReceiver(String email) {
public List<Response> 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<Message> messages = messageRepository.findAllBySender(sender);
return convertMessagesToDTO(messages);
}
Expand All @@ -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<Message> messages = messageRepository.findAllBySenderAndReceiverOrReceiverAndSender(user, selectedUser,
user, selectedUser);
for (Message message : messages) {
message.setDeleted(true);
}
}

private List<MessageDTO.Response> convertMessagesToDTO(List<Message> messages) {
return messages.stream()
.map(MessageDTO.Response::new)
Expand Down

0 comments on commit c1d0a46

Please sign in to comment.