Skip to content

Commit

Permalink
Merge pull request #62 from kookmin-sw/fix/sendheart
Browse files Browse the repository at this point in the history
Refactor: Exception handling 서버에서만 예외를 처리하는 게 아니라 stomp로 이를 전송
  • Loading branch information
imjanghyeok authored May 10, 2024
2 parents 4bb3043 + 6dc4495 commit 8b0b73b
Showing 1 changed file with 49 additions and 23 deletions.
72 changes: 49 additions & 23 deletions src/main/java/capstone/facefriend/chat/service/MessageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,44 @@ public class MessageService {
private final SimpMessagingTemplate simpMessagingTemplate;
private final ChatRoomInfoRedisRepository chatRoomInfoRedisRepository;

private Member findMemberById(Long memberId) {
private Member findMemberById(String destination, Long memberId) {
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new MemberException(MemberExceptionType.NOT_FOUND));
.orElse(null);
if (member == null) {
simpMessagingTemplate.convertAndSend(destination, MemberExceptionType.NOT_FOUND.message());
throw new MemberException(MemberExceptionType.NOT_FOUND);
}

return member;
}

private ChatRoom findRoomById(Long roomId) {
private ChatRoom findRoomById(String destination, Long roomId) {
ChatRoom chatRoom = chatRoomRepository.findById(roomId)
.orElseThrow(()-> new ChatException(ChatExceptionType.NOT_FOUND));
.orElse(null);
if (chatRoom == null) {
simpMessagingTemplate.convertAndSend(destination, MemberExceptionType.NOT_FOUND.message());
throw new MemberException(MemberExceptionType.NOT_FOUND);
}
return chatRoom;
}

private ChatRoomMember findSenderReceiver(Long senderId, Long receiveId) {
private ChatRoomMember findSenderReceiver(String destination, Long senderId, Long receiveId) {
ChatRoomMember chatRoomMember = chatRoomMemberRepository.findBySenderAndReceiver(senderId, receiveId)
.orElseThrow(()-> new ChatException(ChatExceptionType.NOT_FOUND));
.orElse(null);
if (chatRoomMember == null) {
simpMessagingTemplate.convertAndSend(destination, MemberExceptionType.NOT_FOUND.message());
throw new MemberException(MemberExceptionType.NOT_FOUND);
}
return chatRoomMember;
}

private ChatRoomMember findChatRoomMemberByChatRoomId(Long roomId) {
private ChatRoomMember findChatRoomMemberByChatRoomId(String destination, Long roomId) {
ChatRoomMember chatRoomMember = chatRoomMemberRepository.findByChatRoomId(roomId)
.orElseThrow(()-> new ChatException(ChatExceptionType.NOT_FOUND));
.orElse(null);
if (chatRoomMember == null) {
simpMessagingTemplate.convertAndSend(destination, MemberExceptionType.NOT_FOUND.message());
throw new MemberException(MemberExceptionType.NOT_FOUND);
}
return chatRoomMember;
}

Expand All @@ -81,20 +98,24 @@ private ChatRoomInfo findChatRoomInfo(String chatRoomInfoId) {

@Transactional
public void sendMessage(MessageRequest messageRequest, Long senderId) {
Member sender = findMemberById(senderId);
Member receiver = findMemberById(messageRequest.getReceiveId());
ChatRoom chatRoom = findRoomById(messageRequest.getRoomId());
String exceptionDestination = "/sub/chat/" + senderId;
Member sender = findMemberById(exceptionDestination, senderId);
Member receiver = findMemberById(exceptionDestination, messageRequest.getReceiveId());
ChatRoom chatRoom = findRoomById(exceptionDestination, messageRequest.getRoomId());

if (chatRoom.getStatus() == ChatRoom.Status.open) {
chatRoom.setStatus(ChatRoom.Status.progress);
ChatRoomMember chatRoomMember = findChatRoomMemberByChatRoomId(chatRoom.getId());
ChatRoomMember chatRoomMember = findChatRoomMemberByChatRoomId(exceptionDestination, chatRoom.getId());
chatRoomRepository.save(chatRoom);
chatRoomMemberRepository.save(chatRoomMember);
} else if ((chatRoom.getStatus() == ChatRoom.Status.close)) {
simpMessagingTemplate.convertAndSend(exceptionDestination, ChatExceptionType.INVALIDED_CHATROOM.message());
throw new ChatException(ChatExceptionType.INVALIDED_CHATROOM);
} else if ((chatRoom.getStatus() == ChatRoom.Status.set)) {
simpMessagingTemplate.convertAndSend(exceptionDestination, ChatExceptionType.INVALIDED_CHATROOM.message());
throw new ChatException(ChatExceptionType.INVALIDED_CHATROOM);
} else if ((chatRoom.getStatus() == ChatRoom.Status.delete)) {
simpMessagingTemplate.convertAndSend(exceptionDestination, ChatExceptionType.INVALIDED_CHATROOM.message());
throw new ChatException(ChatExceptionType.INVALIDED_CHATROOM);
}

Expand Down Expand Up @@ -127,19 +148,22 @@ public void sendMessage(MessageRequest messageRequest, Long senderId) {
}
@Transactional
public void sendHeart(Long senderId, Long receiveId) {
String exceptionDestination = "/sub/chat/" + senderId;

// if (chatRoomMemberRepository.findBySenderAndReceiver(senderId, receiveId).isPresent()){
// String exceptionMessage = ChatExceptionType.ALREADY_CHATROOM.message();
// simpMessagingTemplate.convertAndSend(exceptionDestination, exceptionMessage);
// throw new ChatException(ChatExceptionType.ALREADY_CHATROOM);
// }

ChatRoom chatRoom = ChatRoom.builder()
.status(ChatRoom.Status.set)
.isPublic(false)
.build();
chatRoomRepository.save(chatRoom);

Member sender = findMemberById(senderId);
Member receiver = findMemberById(receiveId);


if (chatRoomMemberRepository.findBySenderAndReceiver(senderId, receiveId).isPresent())
throw new ChatException(ChatExceptionType.ALREADY_CHATROOM);

Member sender = findMemberById(exceptionDestination, senderId);
Member receiver = findMemberById(exceptionDestination, receiveId);

ChatRoomMember chatRoomMember = ChatRoomMember.builder()
.chatRoom(chatRoom)
Expand All @@ -166,13 +190,14 @@ public void sendHeart(Long senderId, Long receiveId) {
}
@Transactional
public void heartReply(HeartReplyRequest heartReplyRequest, Long receiveId) {
String exceptionDestination = "/sub/chat/" + receiveId;
String message = null;

Member receiver = findMemberById(receiveId);
Member sender = findMemberById(heartReplyRequest.getSenderId());
Member receiver = findMemberById(exceptionDestination, receiveId);
Member sender = findMemberById(exceptionDestination, heartReplyRequest.getSenderId());

ChatRoomMember chatRoomMember = findSenderReceiver(sender.getId(), receiver.getId());
ChatRoom chatRoom = findRoomById(chatRoomMember.getChatRoom().getId());
ChatRoomMember chatRoomMember = findSenderReceiver(exceptionDestination, sender.getId(), receiver.getId());
ChatRoom chatRoom = findRoomById(exceptionDestination, chatRoomMember.getChatRoom().getId());

if (heartReplyRequest.getIntention().equals("positive")) {
chatRoom.setStatus(ChatRoom.Status.open);
Expand All @@ -186,6 +211,7 @@ public void heartReply(HeartReplyRequest heartReplyRequest, Long receiveId) {

message = receiver.getBasicInfo().getNickname() + "님이 거절했습니다.";
} else {
simpMessagingTemplate.convertAndSend(exceptionDestination, ChatExceptionType.ALREADY_CHATROOM);
throw new ChatException(ChatExceptionType.ALREADY_CHATROOM);
}
// 동적으로 목적지 설정
Expand Down

0 comments on commit 8b0b73b

Please sign in to comment.