From ab14de0856a7e5c5bbe51132e8e1a12d8ff94257 Mon Sep 17 00:00:00 2001 From: kimtaeyoung <62414583+kimtaeyoung201910794@users.noreply.github.com> Date: Thu, 22 Aug 2024 23:34:05 +0900 Subject: [PATCH 1/2] =?UTF-8?q?socket=20sender=20uuid=EB=A1=9C=20=ED=8C=90?= =?UTF-8?q?=EB=B3=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../majorLink/controller/ChatController.java | 33 ++++++++++++------- .../example/majorLink/domain/ChatRoom.java | 2 ++ .../majorLink/dto/ChatjoinRequestDTO.java | 3 +- .../majorLink/global/config/WebConfig.java | 2 +- .../majorLink/service/ChatService.java | 6 ++-- .../majorLink/service/ChatServiceImpl.java | 33 ++++++++++++------- 6 files changed, 50 insertions(+), 29 deletions(-) diff --git a/majorLink/src/main/java/com/example/majorLink/controller/ChatController.java b/majorLink/src/main/java/com/example/majorLink/controller/ChatController.java index 7b59fc5..35eca4b 100644 --- a/majorLink/src/main/java/com/example/majorLink/controller/ChatController.java +++ b/majorLink/src/main/java/com/example/majorLink/controller/ChatController.java @@ -3,37 +3,44 @@ import com.example.majorLink.domain.ChatMessage; import com.example.majorLink.domain.ChatRoom; +import com.example.majorLink.domain.User; import com.example.majorLink.dto.*; +import com.example.majorLink.global.auth.AuthUser; +import com.example.majorLink.repository.UserRepository; import com.example.majorLink.service.ChatService; import lombok.AllArgsConstructor; import org.springframework.messaging.handler.annotation.MessageMapping; import org.springframework.messaging.handler.annotation.SendTo; +import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.*; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.UUID; +import java.util.*; @RestController @AllArgsConstructor public class ChatController { private final ChatService chatService; - + private final UserRepository userRepository; //send message 컨트롤러 @MessageMapping("/chat.message") @SendTo("/topic/public") public Map sendMessage(Map message) { UUID sender = UUID.fromString(message.get("sender").toString()); +// User user = authUser.getUser(); +// String username = user.getUsername(); + + User user = userRepository.findById(sender).orElseThrow(() -> + new IllegalArgumentException("유저를 찾을 수 없습니다: " + sender)); + String content = message.get("content").toString(); Integer chatRoomIdInteger = (Integer) message.get("chatroom"); long roomId = chatRoomIdInteger.longValue(); // 로그 추가 System.out.println("Received message: " + content); - System.out.println("Sender UUID: " + sender); + System.out.println("username: " + user.getUsername()); System.out.println("Room ID: " + roomId); ChatRoom chatRoom = chatService.getChatroomById(roomId); @@ -41,13 +48,14 @@ public Map sendMessage(Map message) { ChatMessage message1 = ChatMessage.builder() .content(content) .chatRoom(chatRoom) + .sender(user) .build(); // 로그 추가 System.out.println("Saving message: " + message1); - ChatMessage chat = chatService.saveChatMessage(message1, sender); + ChatMessage chat = chatService.saveChatMessage(message1, user); Map result = new HashMap<>(); @@ -61,8 +69,9 @@ public Map sendMessage(Map message) { //채팅방 생성 컨트롤러 @PostMapping("/chatrooms") - public ChatRoom createChatroom(@RequestBody ChatRoomRequestDTO chatRoomRequestDTO) { - return chatService.createChatroom(chatRoomRequestDTO.getName()); + public ChatRoom createChatroom(@RequestBody ChatRoomRequestDTO chatRoomRequestDTO, @AuthenticationPrincipal AuthUser authUser) { + User user = authUser.getUser(); + return chatService.createChatroom(chatRoomRequestDTO.getName(), user); } @@ -89,9 +98,9 @@ public List getChatHistory(@PathVariable Long roomId) { } //채팅방 입장 컨트롤러 - @PostMapping("/chat/join") - public String joinRoom(@RequestBody ChatjoinRequestDTO chatjoinRequestDTO) { - return chatService.joinRoom(chatjoinRequestDTO.getRoomId(), chatjoinRequestDTO.getUserId()); + @PostMapping("/chat/join/{roomId}") + public String joinRoom(@PathVariable Long roomId, @AuthenticationPrincipal AuthUser authUser) { + return chatService.joinRoom(roomId, authUser.getUser()); } //채팅방별 입장한 유저 조회 diff --git a/majorLink/src/main/java/com/example/majorLink/domain/ChatRoom.java b/majorLink/src/main/java/com/example/majorLink/domain/ChatRoom.java index 1b7e465..30595a6 100644 --- a/majorLink/src/main/java/com/example/majorLink/domain/ChatRoom.java +++ b/majorLink/src/main/java/com/example/majorLink/domain/ChatRoom.java @@ -27,4 +27,6 @@ public class ChatRoom extends BaseEntity{ @OneToMany(mappedBy = "chatRoom", cascade = CascadeType.ALL) private List chatList = new ArrayList<>(); + + } \ No newline at end of file diff --git a/majorLink/src/main/java/com/example/majorLink/dto/ChatjoinRequestDTO.java b/majorLink/src/main/java/com/example/majorLink/dto/ChatjoinRequestDTO.java index 3a97616..52639f1 100644 --- a/majorLink/src/main/java/com/example/majorLink/dto/ChatjoinRequestDTO.java +++ b/majorLink/src/main/java/com/example/majorLink/dto/ChatjoinRequestDTO.java @@ -1,5 +1,6 @@ package com.example.majorLink.dto; +import com.example.majorLink.domain.User; import lombok.Builder; import lombok.Getter; @@ -9,5 +10,5 @@ @Builder public class ChatjoinRequestDTO { private Long roomId; - private UUID userId; + } diff --git a/majorLink/src/main/java/com/example/majorLink/global/config/WebConfig.java b/majorLink/src/main/java/com/example/majorLink/global/config/WebConfig.java index 446b092..2cdf43c 100644 --- a/majorLink/src/main/java/com/example/majorLink/global/config/WebConfig.java +++ b/majorLink/src/main/java/com/example/majorLink/global/config/WebConfig.java @@ -10,7 +10,7 @@ public class WebConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") - .allowedOrigins("http://localhost:3000", + .allowedOrigins("http://localhost:3000","http://localhost:3003","http://localhost:3004", "http://localhost:3007","http://localhost:3008", "http://localhost:59923", "http://localhost:3003", diff --git a/majorLink/src/main/java/com/example/majorLink/service/ChatService.java b/majorLink/src/main/java/com/example/majorLink/service/ChatService.java index 53928ca..8a76dc9 100644 --- a/majorLink/src/main/java/com/example/majorLink/service/ChatService.java +++ b/majorLink/src/main/java/com/example/majorLink/service/ChatService.java @@ -10,7 +10,7 @@ public interface ChatService { //메세지 저장 - ChatMessage saveChatMessage(ChatMessage chatMessage, UUID sender); + ChatMessage saveChatMessage(ChatMessage chatMessage, User user); //채팅방 메세지 전체 조회 List getChatMessageById(Long roomId); @@ -23,10 +23,10 @@ public interface ChatService { List getUserById(Long roomId); //채팅방 생성 - ChatRoom createChatroom(String roomName); + ChatRoom createChatroom(String roomName, User user); //채팅방 입장 - String joinRoom(Long roomId, UUID userId); + String joinRoom(Long roomId, User user); ChatRoom getChatroomById(Long roomId); diff --git a/majorLink/src/main/java/com/example/majorLink/service/ChatServiceImpl.java b/majorLink/src/main/java/com/example/majorLink/service/ChatServiceImpl.java index 764d849..945fde8 100644 --- a/majorLink/src/main/java/com/example/majorLink/service/ChatServiceImpl.java +++ b/majorLink/src/main/java/com/example/majorLink/service/ChatServiceImpl.java @@ -9,6 +9,8 @@ import com.example.majorLink.repository.UserChatRepository; import com.example.majorLink.repository.UserRepository; import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.NoArgsConstructor; import org.springframework.stereotype.Service; import java.util.List; @@ -25,10 +27,9 @@ public class ChatServiceImpl implements ChatService{ private final UserRepository userRepository; - //메세지 저장 + @Override - public ChatMessage saveChatMessage(ChatMessage chatMessage, UUID sender) { - User user = userRepository.findById(sender).orElseThrow(() -> new RuntimeException("존재하지않는 유저입니다")); //예외처리 부분 추후 수정 + public ChatMessage saveChatMessage(ChatMessage chatMessage, User user) { // 로그 추가 System.out.println("User found: " + user); @@ -37,9 +38,7 @@ public ChatMessage saveChatMessage(ChatMessage chatMessage, UUID sender) { // 로그 추가 System.out.println("Saving chat message: " + chatMessage); - return chatMessageRepository.save(chatMessage); - } //방id로 메세지 조회 @@ -63,22 +62,32 @@ public List getUserById(Long roomId) { .collect(Collectors.toList()); //List로 변환 } - - - - //채팅방 생성 @Override - public ChatRoom createChatroom(String roomName) { + public ChatRoom createChatroom(String roomName, User user) { ChatRoom chatRoom = ChatRoom.builder() .name(roomName) .build(); +// UserChat userChat = UserChat.builder() +// .chatRoom(chatRoom) +// .user(user) +// .build(); return chatRoomRepository.save(chatRoom); } + //채팅방 생성 +// @Override +// public ChatRoom createChatroom(String roomName) { +// ChatRoom chatRoom = ChatRoom.builder() +// .name(roomName) +// .build(); +// return chatRoomRepository.save(chatRoom); +// } + + //채팅방 입장 - public String joinRoom(Long roomId, UUID userId) { - User user = userRepository.findById(userId).orElseThrow(() -> new RuntimeException("존재하지 않는 유저 입니다")); + public String joinRoom(Long roomId, User user) { + ChatRoom chatRoom = chatRoomRepository.findById(roomId).orElseThrow(() -> new RuntimeException("존재하지 않는 채팅방입니다")); UserChat userChat = UserChat.builder() .user(user) From 7c35e177191dd542b7f3f72ff6416c02c5fe1be6 Mon Sep 17 00:00:00 2001 From: kimtaeyoung <62414583+kimtaeyoung201910794@users.noreply.github.com> Date: Fri, 23 Aug 2024 00:48:20 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=EC=B1=84=ED=8C=85=20=EC=9D=91=EB=8B=B5=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/example/majorLink/controller/ChatController.java | 2 +- .../java/com/example/majorLink/dto/ChatmessageResponseDTO.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/majorLink/src/main/java/com/example/majorLink/controller/ChatController.java b/majorLink/src/main/java/com/example/majorLink/controller/ChatController.java index 35eca4b..36db278 100644 --- a/majorLink/src/main/java/com/example/majorLink/controller/ChatController.java +++ b/majorLink/src/main/java/com/example/majorLink/controller/ChatController.java @@ -90,7 +90,7 @@ public List getAllChatRoom() { public List getChatHistory(@PathVariable Long roomId) { return chatService.getChatMessageById(roomId).stream().map(chatMessage -> ChatmessageResponseDTO.builder() .id(chatMessage.getId()) - .senderUsername(chatMessage.getSender().getUsername()) + .name(chatMessage.getSender().getUsername()) .content(chatMessage.getContent()) .chatroomId(chatMessage.getChatRoom().getId()) .build()) diff --git a/majorLink/src/main/java/com/example/majorLink/dto/ChatmessageResponseDTO.java b/majorLink/src/main/java/com/example/majorLink/dto/ChatmessageResponseDTO.java index b04d0f9..2234852 100644 --- a/majorLink/src/main/java/com/example/majorLink/dto/ChatmessageResponseDTO.java +++ b/majorLink/src/main/java/com/example/majorLink/dto/ChatmessageResponseDTO.java @@ -14,7 +14,7 @@ public class ChatmessageResponseDTO { private Long id; private String content; - private String senderUsername; + private String name; private Long chatroomId; }