Skip to content

Commit

Permalink
refactor: map 타입에 대한 dto 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
JJ503 committed Mar 9, 2024
1 parent b5832b4 commit 9a5ca1e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -36,30 +38,24 @@ public TextMessageType supportTextMessageType() {

@Override
public List<SendMessagesDto> handle(final WebSocketSession session, final Map<String, String> 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<String, Object> 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<Long, WebSocketSessions> chatRoomSessions = sessions.getChatRoomSessions();
final WebSocketSessions webSocketSessions = chatRoomSessions.get(chatRoomId);
final WebSocketSessions webSocketSessions = chatRoomSessions.get(messageData.chatRoomId());
final Set<WebSocketSession> groupSessions = webSocketSessions.getSessions();
final List<SendMessagesDto> 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));
Expand All @@ -68,6 +64,21 @@ public List<SendMessagesDto> handle(final WebSocketSession session, final Map<St
return sendMessagesDtos;
}

private SessionAttributeDto getSessionAttributes(final WebSocketSession session) {
final Map<String, Object> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.ddang.ddang.chat.handler.dto;

public record ChatMessageDataDto(Long chatRoomId, Long receiverId, String contents) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.ddang.ddang.websocket.handler.dto;

public record SessionAttributeDto(Long userId, String baseUrl) {
}

0 comments on commit 9a5ca1e

Please sign in to comment.