Skip to content

Commit

Permalink
Merge pull request #104 from TeamSynergyy/feature/queryDsl
Browse files Browse the repository at this point in the history
#103 채팅 Dto Json 변환
  • Loading branch information
rivkode authored May 28, 2024
2 parents d4cc2d4 + e917a54 commit 1550e6b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.seoultech.synergybe.domain.chat.dto.response;

import com.seoultech.synergybe.domain.chat.domain.ChatType;

public record ChatMessageResponse(
String id,
Long chatRoomId,
String userId,
String message,
ChatType chatType,
String imageName,
String imageUrl
) {
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.seoultech.synergybe.domain.chat.dto.response;

import java.util.List;

public record GetChatRoomResponse(
Long chatRoomId,
String chatRoomName
String chatRoomName,
List<String> userIds
) {
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.seoultech.synergybe.domain.chat.handler;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.seoultech.synergybe.domain.chat.domain.ChatType;
import com.seoultech.synergybe.domain.chat.dto.request.ChatMessageRequest;
import com.seoultech.synergybe.domain.chat.dto.response.ChatMessageResponse;
import com.seoultech.synergybe.domain.chat.exception.WebSocketBadRequestException;
import com.seoultech.synergybe.domain.chat.service.ChatMessageService;
import com.seoultech.synergybe.domain.user.User;
Expand Down Expand Up @@ -110,9 +112,6 @@ protected void handleTextMessage(WebSocketSession session, TextMessage message)
// 채팅 전송
sendMessageToChatRoom(chatMessageRequest, webSocketSessionList);

// 한사람에 대해서만 저장을 해야함
saveMessage(chatMessageRequest);

} else if (chatMessageRequest.chatType().equals(ChatType.IMAGE)) {
// todo
// 이미지 혹은 영상 처리
Expand Down Expand Up @@ -141,19 +140,30 @@ private List<WebSocketSession> getSessionListByChatRoomId(Long chatRoomId) {
return webSocketSessionMap.getWebsocketListHashMap().get(chatRoomId).getWebSocketSessions();
}

private void sendMessageToChatRoom(ChatMessageRequest chatMessageRequest, WebSocketSessionList webSocketSessionList) {
private void sendMessageToChatRoom(ChatMessageRequest chatMessageRequest, WebSocketSessionList webSocketSessionList) throws JsonProcessingException {
for (WebSocketSession session : webSocketSessionList.getWebSocketSessions()) {
sendMessage(session, chatMessageRequest.message());
// 한사람에 대해서만 저장을 해야함
String chatId = saveMessage(chatMessageRequest);
ChatMessageResponse chatMessageResponse = new ChatMessageResponse(
chatId,
chatMessageRequest.chatRoomId(),
chatMessageRequest.userId(),
chatMessageRequest.message(),
chatMessageRequest.chatType(),
null,
null
);
sendMessage(session, objectMapper.writeValueAsString(chatMessageResponse));
}
}

private void saveMessage(ChatMessageRequest chatMessageRequest) {
chatMessageService.saveChat(chatMessageRequest);
private String saveMessage(ChatMessageRequest chatMessageRequest) {
return chatMessageService.saveChat(chatMessageRequest);
}

private <T> void sendMessage(WebSocketSession session, T message) {
try {
session.sendMessage(new TextMessage(objectMapper.writeValueAsString(message)));
session.sendMessage(new TextMessage(message.toString()));
} catch (IOException e) {
log.error(e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
public class ChatMessageService {
private final ChatMessageRepository chatMessageRepository;

public void saveChat(ChatMessageRequest chatMessageRequest) {
public String saveChat(ChatMessageRequest chatMessageRequest) {
// mongoDB는 UTC로 저장되므로 +9를 해주어 한국시간과 맞춘다
// LocalDateTime createAt = LocalDateTime.now().plusHours(9);
LocalDateTime createAt = LocalDateTime.now();

if (chatMessageRequest.chatType().equals(ChatType.TEXT)) {
Expand All @@ -31,10 +30,16 @@ public void saveChat(ChatMessageRequest chatMessageRequest) {
.createAt(createAt)
.build();

log.info("chatMessage Id" + chatMessage.getId());
log.info("chatMessage Id: " + chatMessage.getId());

log.info("chatMessage message " + chatMessage.getMessage());

chatMessageRepository.save(chatMessage);
ChatMessage chat = chatMessageRepository.save(chatMessage);

log.info("chat Id: " + chat.getId());


return chat.getId();

} else if (chatMessageRequest.chatType().equals(ChatType.IMAGE)) {
ChatMessage chatMessage = ChatMessage.builder()
Expand All @@ -46,7 +51,11 @@ public void saveChat(ChatMessageRequest chatMessageRequest) {
.createAt(createAt)
.build();
chatMessageRepository.save(chatMessage);

return chatMessage.getId();

}
return "";
}

public List<ChatMessage> getChatListByChatRoomId(Long chatRoomId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
Expand All @@ -32,13 +33,18 @@ public void createRoom(CreateChatRoomRequest request) {

public List<GetChatRoomResponse> getChatRoomsByUserId(String userId) {
List<ChatRoom> chatRooms = chatRoomRepository.findAllByCreateUserIdOrAttendUserId(userId);

List<GetChatRoomResponse> getChatRoomResponses = chatRooms.stream().map(
chatRoom -> new GetChatRoomResponse(
chatRoom.getId(), chatRoom.getName()
)
return chatRooms.stream().map(
chatRoom -> {
// 기존 userIds 리스트를 복사하고, 새로운 userId를 추가
List<String> newUserIds = new ArrayList<>();
newUserIds.add(chatRoom.getCreateUser().getId());
newUserIds.add(chatRoom.getAttendUser().getId());
return new GetChatRoomResponse(
chatRoom.getId(),
chatRoom.getName(),
newUserIds
);
}
).toList();

return getChatRoomResponses;
}
}

0 comments on commit 1550e6b

Please sign in to comment.