Skip to content

Commit

Permalink
Merge pull request #49 from dd-jiyun/jiyun
Browse files Browse the repository at this point in the history
feat: Delete All message
  • Loading branch information
dd-jiyun authored Mar 7, 2024
2 parents 3514d06 + 2c2b200 commit e7ed0fc
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 e7ed0fc

Please sign in to comment.