From 2f98728fe9f95889d1cda52cd4e04004f40e664f Mon Sep 17 00:00:00 2001 From: TaeHoon0 Date: Wed, 24 Jul 2024 17:56:56 +0900 Subject: [PATCH] =?UTF-8?q?[#39]=20refactor=20:=20Message=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=20=EB=A1=9C=EC=A7=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/CatchStudy/controller/MessageController.java | 7 +++++-- .../java/com/example/CatchStudy/service/ChatService.java | 5 ++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/example/CatchStudy/controller/MessageController.java b/src/main/java/com/example/CatchStudy/controller/MessageController.java index cdd3bf4..f3549b5 100644 --- a/src/main/java/com/example/CatchStudy/controller/MessageController.java +++ b/src/main/java/com/example/CatchStudy/controller/MessageController.java @@ -10,6 +10,8 @@ import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.web.bind.annotation.RestController; +import java.security.Principal; + @RestController @RequiredArgsConstructor public class MessageController { @@ -18,7 +20,8 @@ public class MessageController { @MessageMapping("/{chatRoomId}/chat") @SendTo("/sub/{chatRoomId}/chat") - public void createMessage(@DestinationVariable long chatRoomId, MessageRequestDto messageRequestDto) { - MessageResponseDto messageResponseDto = chatService.createMessage(chatRoomId, messageRequestDto); + public void createMessage(@DestinationVariable long chatRoomId, MessageRequestDto messageRequestDto, Principal principal) { + String email = principal.getName(); + MessageResponseDto messageResponseDto = chatService.createMessage(chatRoomId, messageRequestDto, email); } } diff --git a/src/main/java/com/example/CatchStudy/service/ChatService.java b/src/main/java/com/example/CatchStudy/service/ChatService.java index 0779742..39eb85a 100644 --- a/src/main/java/com/example/CatchStudy/service/ChatService.java +++ b/src/main/java/com/example/CatchStudy/service/ChatService.java @@ -87,10 +87,9 @@ public List getMessageList(long chatRoomId) { } @Transactional - public MessageResponseDto createMessage(long chatRoomId, MessageRequestDto messageRequestDto) { + public MessageResponseDto createMessage(long chatRoomId, MessageRequestDto messageRequestDto, String email) { - Users user = usersRepository.findByUserId(usersService.getCurrentUserId()). - orElseThrow(() -> new CatchStudyException(ErrorCode.USER_NOT_FOUND)); + Users user = usersRepository.findByEmail(email); ChatRoom chatRoom = chatRoomRepository.findById(chatRoomId). orElseThrow(() -> new CatchStudyException(ErrorCode.CHATROOM_NOT_FOUND));