Skip to content

Commit

Permalink
Merge pull request #66 from kookmin-sw/refactor/leftchatroom
Browse files Browse the repository at this point in the history
Refactor/leftchatroom
  • Loading branch information
imjanghyeok authored May 11, 2024
2 parents b457c7f + 172d489 commit 4f723e3
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import capstone.facefriend.member.exception.member.MemberExceptionType;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -35,6 +36,7 @@ public class ChatRoomService {
private final ChatMessageRepository chatMessageRepository;
private final ChatRoomInfoRedisRepository chatRoomInfoRedisRepository;
private final MemberRepository memberRepository;
private final SimpMessagingTemplate simpMessagingTemplate;
private static final String EMPTY_MESSAGE = "채팅을 시작하지 않았습니다.";
private static final String OPEN_MESSAGE = "채팅을 시작해보세요!";
private static final String CLOSE_MESSAGE = "상대방이 떠났습니다.";
Expand Down Expand Up @@ -156,6 +158,7 @@ public String leftRoom(Long roomId, Long memberId) {
ChatRoom.Status status = chatRoom.getStatus();
ChatRoomMember chatRoomMember = findChatRoomMemberByChatRoomId(roomId);
Member member = findMemberById(memberId);
Member sender = identifySender(chatRoomMember, memberId);
Member leftMember = identifyLeftMember(memberId, chatRoomMember);
if (status== ChatRoom.Status.close) {
if (member != leftMember) {
Expand All @@ -176,11 +179,13 @@ public String leftRoom(Long roomId, Long memberId) {
} else if (chatRoomMember.getReceiver() == member){
chatRoomMember.setReceiverExist(false);
} else {
return "속해있지 않은 채팅방입니디.";
return "속해있지 않은 채팅방입니다.";
}
chatRoom.setStatus(ChatRoom.Status.close);
simpMessagingTemplate.convertAndSend("/sub/chat/" + sender.getId(), ChatRoomLeftResponse.of(sender, "상대방이 떠났습니다."));
chatRoomRepository.save(chatRoom);
chatRoomMemberRepository.save(chatRoomMember);

return "채팅방을 떠났습니다";
}

Expand Down
24 changes: 13 additions & 11 deletions src/main/java/capstone/facefriend/chat/service/MessageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import capstone.facefriend.chat.exception.ChatExceptionType;
import capstone.facefriend.chat.repository.*;
import capstone.facefriend.chat.service.dto.heart.HeartReplyRequest;
import capstone.facefriend.chat.service.dto.heart.HeartReplyResponse;
import capstone.facefriend.chat.service.dto.heart.SendHeartResponse;
import capstone.facefriend.chat.service.dto.message.MessageListResponse;
import capstone.facefriend.chat.service.dto.message.MessageRequest;
Expand Down Expand Up @@ -192,37 +193,38 @@ 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(exceptionDestination, receiveId);
Member sender = findMemberById(exceptionDestination, heartReplyRequest.getSenderId());
Member sender = findMemberById(exceptionDestination, heartReplyRequest.senderId());

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

if (heartReplyRequest.getIntention().equals("positive")) {
if (heartReplyRequest.intention().equals("positive")) {
chatRoom.setStatus(ChatRoom.Status.open);
chatRoomRepository.save(chatRoom);
chatRoomMemberRepository.save(chatRoomMember);

message = receiver.getBasicInfo().getNickname() + "님이 수락했습니다.";
} else if (heartReplyRequest.getIntention().equals("negative")) {
// 대화 수락
simpMessagingTemplate.convertAndSend(exceptionDestination, "대화 수락");

} else if (heartReplyRequest.intention().equals("negative")) {
chatRoomMemberRepository.delete(chatRoomMember);
chatRoomRepository.delete(chatRoom);
// 대화 거절
simpMessagingTemplate.convertAndSend(exceptionDestination, "대화 거절");

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

// 대화 수락
simpMessagingTemplate.convertAndSend(exceptionDestination, "대화 수락 성공");


HeartReplyResponse heartReplyResponse = HeartReplyResponse.of(receiveId, heartReplyRequest);

// 메시지 전송
simpMessagingTemplate.convertAndSend(destination, message);
simpMessagingTemplate.convertAndSend(destination, heartReplyResponse);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package capstone.facefriend.chat.service.dto.chatroom;

import capstone.facefriend.member.domain.member.Member;

public record ChatRoomLeftResponse (
Long senderId,
String senderNickname,
String content
){
public static ChatRoomLeftResponse of (Member sender, String content) {
return new ChatRoomLeftResponse(
sender.getId(),
sender.getBasicInfo().getNickname(),
content
);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package capstone.facefriend.chat.service.dto.heart;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class HeartReplyRequest {
Long senderId;
String intention;
}
public record HeartReplyRequest(
Long senderId,
String intention
) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package capstone.facefriend.chat.service.dto.heart;

public record HeartReplyResponse(
Long senderId,
String intention
) {
public static HeartReplyResponse of (Long senderId, HeartReplyRequest heartReplyRequest) {
return new HeartReplyResponse(
senderId,
heartReplyRequest.intention()
);
}
}

0 comments on commit 4f723e3

Please sign in to comment.