From 9a5ca1ef917e15310dd8b025a6fce7215fb01412 Mon Sep 17 00:00:00 2001 From: JJ503 <63184334+JJ503@users.noreply.github.com> Date: Sun, 10 Mar 2024 05:04:38 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20map=20=ED=83=80=EC=9E=85=EC=97=90?= =?UTF-8?q?=20=EB=8C=80=ED=95=9C=20dto=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...hatWebSocketHandleTextMessageProvider.java | 43 ++++++++++++------- .../chat/handler/dto/ChatMessageDataDto.java | 4 ++ .../handler/dto/SessionAttributeDto.java | 4 ++ 3 files changed, 35 insertions(+), 16 deletions(-) create mode 100644 backend/ddang/src/main/java/com/ddang/ddang/chat/handler/dto/ChatMessageDataDto.java create mode 100644 backend/ddang/src/main/java/com/ddang/ddang/websocket/handler/dto/SessionAttributeDto.java diff --git a/backend/ddang/src/main/java/com/ddang/ddang/chat/handler/ChatWebSocketHandleTextMessageProvider.java b/backend/ddang/src/main/java/com/ddang/ddang/chat/handler/ChatWebSocketHandleTextMessageProvider.java index 738b18d72..36f43f139 100644 --- a/backend/ddang/src/main/java/com/ddang/ddang/chat/handler/ChatWebSocketHandleTextMessageProvider.java +++ b/backend/ddang/src/main/java/com/ddang/ddang/chat/handler/ChatWebSocketHandleTextMessageProvider.java @@ -4,10 +4,12 @@ import com.ddang.ddang.chat.application.dto.CreateMessageDto; import com.ddang.ddang.chat.domain.WebSocketChatSessions; import com.ddang.ddang.chat.domain.WebSocketSessions; +import com.ddang.ddang.chat.handler.dto.ChatMessageDataDto; import com.ddang.ddang.chat.presentation.dto.request.CreateMessageRequest; import com.ddang.ddang.chat.presentation.dto.response.ReadMessageResponse; import com.ddang.ddang.websocket.handler.WebSocketHandleTextMessageProvider; import com.ddang.ddang.websocket.handler.dto.SendMessagesDto; +import com.ddang.ddang.websocket.handler.dto.SessionAttributeDto; import com.ddang.ddang.websocket.handler.dto.TextMessageType; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.RequiredArgsConstructor; @@ -36,30 +38,24 @@ public TextMessageType supportTextMessageType() { @Override public List handle(final WebSocketSession session, final Map data) throws Exception { - final CreateMessageRequest request = new CreateMessageRequest( - Long.parseLong(data.get("recevierId")), - data.get("contents") - ); + final SessionAttributeDto sessionAttribute = getSessionAttributes(session); + final ChatMessageDataDto messageData = objectMapper.convertValue(data, ChatMessageDataDto.class); + sessions.add(session, messageData.chatRoomId()); - final long chatRoomId = Long.parseLong(data.get("chatRoomId")); - sessions.add(session, chatRoomId); - - final Map attributes = session.getAttributes(); - final Long userId = Long.parseLong(String.valueOf(attributes.get("userId"))); - final CreateMessageDto createMessageDto = CreateMessageDto.of(userId, chatRoomId, request); + final Long senderId = sessionAttribute.userId(); + final CreateMessageDto createMessageDto = createMessageDto(messageData, senderId); - final String baseUrl = String.valueOf(attributes.get("baseUrl")); - final Long messageId = messageService.create(createMessageDto, baseUrl); + final Long messageId = messageService.create(createMessageDto, sessionAttribute.baseUrl()); final Map chatRoomSessions = sessions.getChatRoomSessions(); - final WebSocketSessions webSocketSessions = chatRoomSessions.get(chatRoomId); + final WebSocketSessions webSocketSessions = chatRoomSessions.get(messageData.chatRoomId()); final Set groupSessions = webSocketSessions.getSessions(); final List sendMessagesDtos = new ArrayList<>(); for (WebSocketSession currentSession : groupSessions) { ReadMessageResponse response; - if (currentSession.getAttributes().get("userId") == userId) { - response = new ReadMessageResponse(messageId, LocalDateTime.now(), true, request.contents()); + if (currentSession.getAttributes().get("userId") == senderId) { + response = new ReadMessageResponse(messageId, LocalDateTime.now(), true, messageData.contents()); } else { - response = new ReadMessageResponse(messageId, LocalDateTime.now(), false, request.contents()); + response = new ReadMessageResponse(messageId, LocalDateTime.now(), false, messageData.contents()); } final TextMessage textMessage = new TextMessage(objectMapper.writeValueAsString(response)); sendMessagesDtos.add(new SendMessagesDto(session, textMessage)); @@ -68,6 +64,21 @@ public List handle(final WebSocketSession session, final Map attributes = session.getAttributes(); + + return objectMapper.convertValue(attributes, SessionAttributeDto.class); + } + + private CreateMessageDto createMessageDto(final ChatMessageDataDto messageData, final Long userId) { + final CreateMessageRequest request = new CreateMessageRequest( + messageData.receiverId(), + messageData.contents() + ); + + return CreateMessageDto.of(userId, messageData.chatRoomId(), request); + } + @Override public void remove(final WebSocketSession session) { sessions.remove(session); diff --git a/backend/ddang/src/main/java/com/ddang/ddang/chat/handler/dto/ChatMessageDataDto.java b/backend/ddang/src/main/java/com/ddang/ddang/chat/handler/dto/ChatMessageDataDto.java new file mode 100644 index 000000000..169982651 --- /dev/null +++ b/backend/ddang/src/main/java/com/ddang/ddang/chat/handler/dto/ChatMessageDataDto.java @@ -0,0 +1,4 @@ +package com.ddang.ddang.chat.handler.dto; + +public record ChatMessageDataDto(Long chatRoomId, Long receiverId, String contents) { +} diff --git a/backend/ddang/src/main/java/com/ddang/ddang/websocket/handler/dto/SessionAttributeDto.java b/backend/ddang/src/main/java/com/ddang/ddang/websocket/handler/dto/SessionAttributeDto.java new file mode 100644 index 000000000..d175b0eb3 --- /dev/null +++ b/backend/ddang/src/main/java/com/ddang/ddang/websocket/handler/dto/SessionAttributeDto.java @@ -0,0 +1,4 @@ +package com.ddang.ddang.websocket.handler.dto; + +public record SessionAttributeDto(Long userId, String baseUrl) { +}