Skip to content

Commit

Permalink
Merge pull request #188 from dd-jiyun/main
Browse files Browse the repository at this point in the history
메세지함 μˆ˜μ •
  • Loading branch information
dd-jiyun authored Mar 24, 2024
2 parents 125cf96 + 260300e commit ff08af3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public ResponseEntity<String> writeMessage(@RequestBody MessageDTO.Request reque
// μͺ½μ§€ν•¨ λͺ©λ‘ 쑰회
@GetMapping("/all")
public ResponseEntity<List<MessageDTO.Preview>> getAllMessages(@AuthenticationPrincipal UserDetails userDetails) {
List<Message> userMessages = messageService.getUserMessages(userDetails.getUsername());
List<Message> userMessages = messageService.getUserConversationsMessages(userDetails.getUsername());
List<MessageDTO.Preview> previews = userMessages.stream()
.map(MessageDTO.Preview::new)
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ List<Message> findBySenderAndReceiverAndDeletedBySenderFalseOrReceiverAndSenderA

List<Message> findBySenderAndDeletedBySenderFalseOrReceiverAndDeletedByReceiverFalseOrderBySentAtDesc(User sender, User receiver);

List<Message> findBySenderAndDeletedBySenderFalseOrderBySentAtDesc(User user);

List<Message> findByReceiverAndDeletedByReceiverFalseOrderBySentAtDesc(User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,14 +56,32 @@ public List<MessageDTO.Response> getBothMessages(String email, Long selectedUser
}

@Transactional
public List<Message> getUserMessages(String email) {
public List<Message> getUserConversationsMessages(String email) {
User user = userRepository.findByEmail(email)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));

List<Message> userMessages = messageRepository.findBySenderAndDeletedBySenderFalseOrReceiverAndDeletedByReceiverFalseOrderBySentAtDesc(user, user);
List<Message> userSentMessages = messageRepository.findBySenderAndDeletedBySenderFalseOrderBySentAtDesc(user);
List<Message> userReceivedMessages = messageRepository.findByReceiverAndDeletedByReceiverFalseOrderBySentAtDesc(user);

List<Message> userMessages = new ArrayList<>();
userMessages.addAll(userSentMessages);
userMessages.addAll(userReceivedMessages);

// λ©”μ‹œμ§€λ₯Ό μ‹œκ°„ μˆœμ„œλŒ€λ‘œ μ •λ ¬
userMessages.sort(Comparator.comparing(Message::getSentAt).reversed());

return userMessages;
}

// @Transactional
// public List<Message> getUserMessages(String email) {
// User user = userRepository.findByEmail(email)
// .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
//
// List<Message> userMessages = messageRepository.findBySenderAndDeletedBySenderFalseOrReceiverAndDeletedByReceiverFalseOrderBySentAtDesc(user, user);
// return userMessages;
// }

// λ©”μ„Έμ§€ν•¨μ—μ„œ μ‚¬μš©μž 별 κ°€μž₯ 졜근 λ©”μ„Έμ§€λ§Œ λ³΄λ‚΄μ€Œ.
// @Transactional
// public Map<User, Message> getUserConversations(String email) {
Expand Down

0 comments on commit ff08af3

Please sign in to comment.