Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactor]:Socket 코드 정리 #167

Merged
merged 12 commits into from
May 23, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package Backend.socket.api.chat.controller;


import Backend.socket.api.chat.service.dto.request.ChatMessageRoomRequestDto;
import Backend.socket.api.chat.service.ChatService;
import Backend.socket.global.common.MessageSuccessCode;
import Backend.socket.global.common.MessageSuccessResponse;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
public class ChatController {
private final ChatService chatService;
private final SimpMessagingTemplate template;
private final RedisTemplate redisTemplate;
public ChatController(ChatService chatService, SimpMessagingTemplate template,
@Qualifier("redisTemplate") RedisTemplate redisTemplate) {
this.chatService = chatService;
this.template = template;
this.redisTemplate = redisTemplate;
}

@MessageMapping("/room/{roomName}")
@SendTo("/sub/room/{roomName}")
public MessageSuccessResponse sendChatMessageInRoom(@DestinationVariable("roomName") final String roomName,
@RequestBody final ChatMessageRoomRequestDto chatMessageRoomRequestDto) throws IOException {
return MessageSuccessResponse.of(MessageSuccessCode.RECEIVED, chatService.createSendMessageContentInRoom(roomName, chatMessageRoomRequestDto).getMessage());
}


}
116 changes: 116 additions & 0 deletions socket/src/main/java/Backend/socket/api/chat/service/ChatService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package Backend.socket.api.chat.service;

import Backend.socket.api.chat.service.dto.request.ChatMessageRoomRequestDto;
import Backend.socket.api.chat.service.dto.response.ChatMessageElementResponseDto;
import Backend.socket.api.chat.service.dto.response.ChatMessageRoomResponseDto;
import Backend.socket.domain.chat.domain.*;
import Backend.socket.domain.chat.repository.RoomRepository;
import Backend.socket.domain.user.repository.UserRepository;
import Backend.socket.domain.user.entity.User;
import Backend.socket.global.error.socketException.EntityNotFoundException;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

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

import static Backend.socket.domain.chat.domain.ChatContent.createChatContent;
import static Backend.socket.global.error.ErrorCode.USER_NOT_FOUND;


@RequiredArgsConstructor
@Transactional
@Service
public class ChatService {
private final MongoTemplate mongoTemplate;
private final UserRepository userRepository;
private final RoomRepository roomRepository;
public ChatMessageRoomResponseDto createSendMessageContentInRoom(String roomName, ChatMessageRoomRequestDto chatMessageRoomRequestDto) {
Room room = getChatBySessionsInRoom(roomName, chatMessageRoomRequestDto.getChatSession());
User user = userRepository.findBySessionId(chatMessageRoomRequestDto.getChatSession()).orElseThrow();
ChatContent chatContent = createChatContent(chatMessageRoomRequestDto.getFromUserName(), chatMessageRoomRequestDto.getContent(), room, chatMessageRoomRequestDto.getImage());
ChatMessageElementResponseDto chatMessage = ChatMessageElementResponseDto.of(chatContent, chatMessageRoomRequestDto.getChatSession(), user.getProfile(), chatMessageRoomRequestDto.getImage(), validate(room.getOwnerSession(),chatMessageRoomRequestDto.getChatSession()));
List<String> sessionIdList = getSessionIdListInRoom(roomName, chatMessageRoomRequestDto.getChatSession());
saveChatRoom(room);
return ChatMessageRoomResponseDto.of(chatMessageRoomRequestDto.getToRoomName(), sessionIdList, chatMessage);
}
private Boolean validate(String ownerSession, String sessionId){
if(ownerSession.equals(sessionId))
return true;
return false;
}

private List<String> getSessionIdListInRoom(String roomName, String sessionId) {
List<String> sessionList = new ArrayList<>();

// roomId를 기반으로 Room 문서 찾기
Room room = findRoomChatByRoomName(roomName);

if (room != null) {
// Room에 속한 모든 ChatUser의 sessionId를 리스트에 추가
for (ChatUser chatUser : room.getChatUserList()) {
sessionList.add(chatUser.getSessionId());
}
}

return sessionList;
}


private Room getChatBySessionsInRoom(String roomName, String sessionId) {
Room room = findRoomChatByRoomName(roomName);
if (Objects.isNull(room)) {
// 채팅방이 없는 경우 새로운 채팅방 생성
room = createNewRoom(roomName);
}

// 채팅방에 sessionId를 가진 유저가 있는지 확인
if (!isUserExistsInRoom(room, sessionId)) {
// 유저가 없다면 새로운 유저 생성하여 채팅방에 추가
ChatUser chatUser = createChatUser(sessionId);
room.addChatRoom(chatUser);
}

return room;
}

private Room createNewRoom(String roomName) {
Room room = Room.builder()
.roomName(roomName)
.build();
return room;
}
private boolean isUserExistsInRoom(Room room, String sessionId) {
for (ChatUser chatUser : room.getChatUserList()) {
if (chatUser.getSessionId().equals(sessionId)) {
return true;
}
}
return false;
}
private ChatUser createChatUser(String sessionId) {
User user = getUserFromSessionId(sessionId);
return ChatUser.createChatUser(user);
}

private Room findRoomChatByRoomName(String roomName) {
Query query = new Query();
query.addCriteria(Criteria.where("roomName").is(roomName));
return mongoTemplate.findOne(query, Room.class);
}


private User getUserFromSessionId(String sessionId) {
return userRepository.findBySessionId(sessionId)
.orElseThrow(() -> new EntityNotFoundException(USER_NOT_FOUND));
}

public void saveChatRoom(Room room) {
roomRepository.save(room);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Backend.socket.domain.chat.application.controller.dto.request;
package Backend.socket.api.chat.service.dto.request;

import lombok.AccessLevel;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package Backend.socket.domain.chat.application.controller.dto.response;
package Backend.socket.api.chat.service.dto.response;

import Backend.socket.domain.chat.application.service.TriFunction;
import Backend.socket.domain.chat.domain.ChatContent;
import Backend.socket.domain.chat.domain.ChatUser;
import lombok.Builder;
import lombok.Getter;

import java.util.List;
import java.util.stream.Collectors;

@Builder
@Getter
public class ChatMessageElementResponseDto {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Backend.socket.domain.chat.application.controller.dto.response;
package Backend.socket.api.chat.service.dto.response;

import lombok.Builder;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
package Backend.socket.domain.chat.application.controller;
package Backend.socket.api.room.controller;

import Backend.socket.domain.chat.application.controller.dto.request.ChatListRequestDto;
import Backend.socket.domain.chat.application.controller.dto.request.ChatMessageListRequestDto;
import Backend.socket.domain.chat.application.controller.dto.request.ChatMessageRoomRequestDto;
import Backend.socket.domain.chat.application.controller.dto.response.*;
import Backend.socket.domain.chat.application.service.ChatService;
import Backend.socket.domain.chat.application.service.RoomService;
import Backend.socket.domain.chat.domain.Chat;
import Backend.socket.api.chat.service.dto.request.ChatMessageRoomRequestDto;
import Backend.socket.api.room.service.response.RoomListResponseDto;
import Backend.socket.api.room.service.response.RoomMessageListResponseDto;
import Backend.socket.api.chat.service.ChatService;
import Backend.socket.api.room.service.RoomService;
import Backend.socket.global.common.MessageSuccessCode;
import Backend.socket.global.common.MessageSuccessResponse;
import Backend.socket.infra.config.auth.UserId;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.PathVariable;
Expand Down Expand Up @@ -42,7 +37,6 @@ public void sendChatDetailMessage(@DestinationVariable("roomName") final String
final RoomMessageListResponseDto responseDto = roomService.sendRoomDetailMessage(roomName);
template.convertAndSend("/sub/room/" + roomName, MessageSuccessResponse.of(MessageSuccessCode.MESSAGE, responseDto));
}
//
@MessageMapping("/room/all/{sessionId}")
public void sendUserChatListMessage(@DestinationVariable("sessionId") final String sessionId) {
final RoomListResponseDto responseDto = roomService.sendUserChatListMessage(sessionId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package Backend.socket.domain.chat.application.service;
package Backend.socket.api.room.service;

import Backend.socket.domain.chat.application.controller.dto.request.ChatMessageListRequestDto;
import Backend.socket.domain.chat.application.controller.dto.response.*;
import Backend.socket.domain.chat.domain.Chat;
import Backend.socket.api.room.service.response.*;
import Backend.socket.domain.chat.service.Formatter;
import Backend.socket.domain.chat.domain.ChatContent;
import Backend.socket.domain.chat.domain.ChatUser;
import Backend.socket.domain.chat.domain.Room;
import Backend.socket.domain.chat.repository.ChatRepository;
import Backend.socket.domain.chat.repository.RoomRepository;
import Backend.socket.domain.chat.repository.UserRepository;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.data.mongodb.core.MongoTemplate;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Backend.socket.domain.chat.application.controller.dto.response;
package Backend.socket.api.room.service.response;

import Backend.socket.domain.chat.domain.ChatUser;
import lombok.Builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package Backend.socket.domain.chat.application.controller.dto.response;
package Backend.socket.api.room.service.response;

import Backend.socket.domain.chat.domain.Room;
import lombok.Builder;
import lombok.Getter;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Builder
@Getter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Backend.socket.domain.chat.application.controller.dto.response;
package Backend.socket.api.room.service.response;

import lombok.Builder;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package Backend.socket.domain.chat.application.controller.dto.response;
package Backend.socket.api.room.service.response;

import Backend.socket.domain.chat.application.service.TriFunction;
import Backend.socket.global.common.TriFunction;
import Backend.socket.domain.chat.domain.ChatContent;
import Backend.socket.domain.chat.domain.ChatUser;
import lombok.Builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package Backend.socket.domain.chat.application.controller.dto.response;
package Backend.socket.api.room.service.response;

import lombok.Builder;
import lombok.Getter;
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading