From 750a00ff4ea5c0078e2f270c2e231fa480a70e45 Mon Sep 17 00:00:00 2001 From: hyunn522 Date: Thu, 31 Oct 2024 15:05:03 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20chat=20=EB=8F=84=EB=A9=94=EC=9D=B8?= =?UTF-8?q?=20=EB=82=B4=EC=9D=98=20dao=20->=20repository=20=EB=84=A4?= =?UTF-8?q?=EC=9D=B4=EB=B0=8D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dao/chat/ChatRoomRepository.java | 4 +-- ...attingDao.java => ChattingRepository.java} | 2 +- ...omDao.java => UserChatRoomRepository.java} | 2 +- ...tom.java => ChatRoomRepositoryCustom.java} | 2 +- ...oImpl.java => ChatRoomRepositoryImpl.java} | 2 +- .../space_spring/service/ChatRoomService.java | 36 +++++++++---------- .../space_spring/service/ChattingService.java | 9 +++-- .../repository/ChatRoomRepositoryTest.java | 8 ++--- 8 files changed, 32 insertions(+), 33 deletions(-) rename src/main/java/space/space_spring/dao/chat/{ChattingDao.java => ChattingRepository.java} (86%) rename src/main/java/space/space_spring/dao/chat/{UserChatRoomDao.java => UserChatRoomRepository.java} (86%) rename src/main/java/space/space_spring/dao/chat/custom/{ChatRoomDaoCustom.java => ChatRoomRepositoryCustom.java} (85%) rename src/main/java/space/space_spring/dao/chat/custom/{ChatRoomDaoImpl.java => ChatRoomRepositoryImpl.java} (93%) diff --git a/src/main/java/space/space_spring/dao/chat/ChatRoomRepository.java b/src/main/java/space/space_spring/dao/chat/ChatRoomRepository.java index 1180bcfa..4ddf3158 100644 --- a/src/main/java/space/space_spring/dao/chat/ChatRoomRepository.java +++ b/src/main/java/space/space_spring/dao/chat/ChatRoomRepository.java @@ -2,11 +2,11 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; -import space.space_spring.dao.chat.custom.ChatRoomDaoCustom; +import space.space_spring.dao.chat.custom.ChatRoomRepositoryCustom; import space.space_spring.entity.ChatRoom; import space.space_spring.entity.enumStatus.BaseStatusType; @Repository -public interface ChatRoomRepository extends JpaRepository, ChatRoomDaoCustom { +public interface ChatRoomRepository extends JpaRepository, ChatRoomRepositoryCustom { ChatRoom findByIdAndStatus(Long chatRoomId, BaseStatusType status); } diff --git a/src/main/java/space/space_spring/dao/chat/ChattingDao.java b/src/main/java/space/space_spring/dao/chat/ChattingRepository.java similarity index 86% rename from src/main/java/space/space_spring/dao/chat/ChattingDao.java rename to src/main/java/space/space_spring/dao/chat/ChattingRepository.java index 321997da..975186d9 100644 --- a/src/main/java/space/space_spring/dao/chat/ChattingDao.java +++ b/src/main/java/space/space_spring/dao/chat/ChattingRepository.java @@ -8,7 +8,7 @@ import java.util.List; @Repository -public interface ChattingDao extends MongoRepository { +public interface ChattingRepository extends MongoRepository { List findByChatRoomId(Long chatRoomId); ChatMessage findTopByChatRoomIdOrderByCreatedAtDesc(Long chatRoomId); diff --git a/src/main/java/space/space_spring/dao/chat/UserChatRoomDao.java b/src/main/java/space/space_spring/dao/chat/UserChatRoomRepository.java similarity index 86% rename from src/main/java/space/space_spring/dao/chat/UserChatRoomDao.java rename to src/main/java/space/space_spring/dao/chat/UserChatRoomRepository.java index c978d81a..c50c3a28 100644 --- a/src/main/java/space/space_spring/dao/chat/UserChatRoomDao.java +++ b/src/main/java/space/space_spring/dao/chat/UserChatRoomRepository.java @@ -9,7 +9,7 @@ import java.util.List; -public interface UserChatRoomDao extends JpaRepository { +public interface UserChatRoomRepository extends JpaRepository { UserChatRoom findByUserAndChatRoomAndStatus(User userByUserId, ChatRoom chatRoomByChatRoomId, BaseStatusType status); List findByChatRoomAndStatus(ChatRoom chatRoom, BaseStatusType status); diff --git a/src/main/java/space/space_spring/dao/chat/custom/ChatRoomDaoCustom.java b/src/main/java/space/space_spring/dao/chat/custom/ChatRoomRepositoryCustom.java similarity index 85% rename from src/main/java/space/space_spring/dao/chat/custom/ChatRoomDaoCustom.java rename to src/main/java/space/space_spring/dao/chat/custom/ChatRoomRepositoryCustom.java index be75611b..24b3c2ef 100644 --- a/src/main/java/space/space_spring/dao/chat/custom/ChatRoomDaoCustom.java +++ b/src/main/java/space/space_spring/dao/chat/custom/ChatRoomRepositoryCustom.java @@ -6,6 +6,6 @@ import java.util.List; -public interface ChatRoomDaoCustom { +public interface ChatRoomRepositoryCustom { List findByUserAndSpace(User user, Space space); } diff --git a/src/main/java/space/space_spring/dao/chat/custom/ChatRoomDaoImpl.java b/src/main/java/space/space_spring/dao/chat/custom/ChatRoomRepositoryImpl.java similarity index 93% rename from src/main/java/space/space_spring/dao/chat/custom/ChatRoomDaoImpl.java rename to src/main/java/space/space_spring/dao/chat/custom/ChatRoomRepositoryImpl.java index 1cc79d0c..ee38d3fe 100644 --- a/src/main/java/space/space_spring/dao/chat/custom/ChatRoomDaoImpl.java +++ b/src/main/java/space/space_spring/dao/chat/custom/ChatRoomRepositoryImpl.java @@ -13,7 +13,7 @@ import static space.space_spring.entity.QUserChatRoom.userChatRoom; @RequiredArgsConstructor -public class ChatRoomDaoImpl implements ChatRoomDaoCustom { +public class ChatRoomRepositoryImpl implements ChatRoomRepositoryCustom { private final JPAQueryFactory jpaQueryFactory; diff --git a/src/main/java/space/space_spring/service/ChatRoomService.java b/src/main/java/space/space_spring/service/ChatRoomService.java index 1e3bb105..4d44a7d2 100644 --- a/src/main/java/space/space_spring/service/ChatRoomService.java +++ b/src/main/java/space/space_spring/service/ChatRoomService.java @@ -6,8 +6,8 @@ import org.springframework.stereotype.Service; import space.space_spring.dao.UserSpaceDao; import space.space_spring.dao.chat.ChatRoomRepository; -import space.space_spring.dao.chat.ChattingDao; -import space.space_spring.dao.chat.UserChatRoomDao; +import space.space_spring.dao.chat.ChattingRepository; +import space.space_spring.dao.chat.UserChatRoomRepository; import space.space_spring.dto.chat.dto.LastMessageInfoDto; import space.space_spring.dto.chat.request.JoinChatRoomRequest; import space.space_spring.dto.chat.response.*; @@ -36,9 +36,9 @@ public class ChatRoomService { private final SpaceUtils spaceUtils; private final TimeUtils timeUtils; private final UserSpaceDao userSpaceDao; - private final ChattingDao chattingDao; + private final ChattingRepository chattingRepository; private final ChatRoomRepository chatRoomRepository; - private final UserChatRoomDao userChatRoomDao; + private final UserChatRoomRepository userChatRoomRepository; @Transactional public ReadChatRoomResponse readChatRooms(Long userId, Long spaceId) { @@ -80,10 +80,10 @@ public CreateChatRoomResponse createChatRoom(Long userId, Long spaceId, CreateCh ChatRoom chatRoom = chatRoomRepository.save(ChatRoom.of(spaceBySpaceId, createChatRoomRequest.getName(), chatRoomImgUrl)); // TODO 4: userChatRoom 매핑 정보 저장 - userChatRoomDao.save(UserChatRoom.of(chatRoom, userByUserId, LocalDateTime.now())); + userChatRoomRepository.save(UserChatRoom.of(chatRoom, userByUserId, LocalDateTime.now())); for (Long id : createChatRoomRequest.getMemberList()) { User user = userUtils.findUserByUserId(id); - userChatRoomDao.save(UserChatRoom.of(chatRoom, user, LocalDateTime.now())); + userChatRoomRepository.save(UserChatRoom.of(chatRoom, user, LocalDateTime.now())); } // TODO 5: chatroom id 반환 @@ -118,11 +118,11 @@ public ChatSuccessResponse updateLastReadTime(Long userId, Long chatRoomId) { .orElseThrow(() -> new CustomException(CHATROOM_NOT_EXIST)); // TODO 3: 해당 user와 chatRoom에 대한 userChatRoom find - UserChatRoom targetChatRoom = userChatRoomDao.findByUserAndChatRoomAndStatus(userByUserId, chatRoomByChatRoomId, BaseStatusType.ACTIVE); + UserChatRoom targetChatRoom = userChatRoomRepository.findByUserAndChatRoomAndStatus(userByUserId, chatRoomByChatRoomId, BaseStatusType.ACTIVE); // TODO 4: userChatRoom의 마지막으로 읽은 시간 저장 targetChatRoom.setLastReadTime(LocalDateTime.now()); - userChatRoomDao.save(targetChatRoom); + userChatRoomRepository.save(targetChatRoom); log.info("userId: " + userId + " socket disconnect 시 마지막으로 읽은 시간: " + targetChatRoom.getLastReadTime()); return ChatSuccessResponse.of(true); @@ -168,11 +168,11 @@ public ChatSuccessResponse exitChatRoom(Long userId, Long chatRoomId) { .orElseThrow(() -> new CustomException(CHATROOM_NOT_EXIST)); // TODO 3: 해당 user와 chatRoom에 대한 userChatRoom find - UserChatRoom userChatRoom = userChatRoomDao.findByUserAndChatRoomAndStatus(userByUserId, chatRoomByChatRoomId, BaseStatusType.ACTIVE); + UserChatRoom userChatRoom = userChatRoomRepository.findByUserAndChatRoomAndStatus(userByUserId, chatRoomByChatRoomId, BaseStatusType.ACTIVE); // TODO 4: 해당 userChatRoom inactive로 변경 userChatRoom.updateInactive(); - userChatRoomDao.save(userChatRoom); + userChatRoomRepository.save(userChatRoom); return ChatSuccessResponse.of(true); } @@ -192,12 +192,12 @@ public ChatSuccessResponse deleteChatRoom(Long chatRoomId) { } private boolean isUserInChatRoom(User userByUserId, ChatRoom chatRoomByChatRoomId) { - List chatRoomList = userChatRoomDao.findByChatRoomAndStatus(chatRoomByChatRoomId, BaseStatusType.ACTIVE); + List chatRoomList = userChatRoomRepository.findByChatRoomAndStatus(chatRoomByChatRoomId, BaseStatusType.ACTIVE); return chatRoomList.stream().anyMatch(userChatRoom -> userChatRoom.getUser().equals(userByUserId)); } private LastMessageInfoDto getLastMsgInfo(ChatRoom chatRoom) { - ChatMessage lastMsg = chattingDao.findTopByChatRoomIdOrderByCreatedAtDesc(chatRoom.getId()); + ChatMessage lastMsg = chattingRepository.findTopByChatRoomIdOrderByCreatedAtDesc(chatRoom.getId()); LocalDateTime lastUpdateTime = lastMsg != null ? lastMsg.getCreatedAt() :timeUtils.getEncodedTime(chatRoom.getCreatedAt()); HashMap lastContent = lastMsg != null ? lastMsg.getContent() : new HashMap<>(Map.of("text", "메시지를 전송해보세요")); @@ -207,11 +207,11 @@ private LastMessageInfoDto getLastMsgInfo(ChatRoom chatRoom) { } private int calculateUnreadMsgCount(User userByUserId, ChatRoom chatRoom, LocalDateTime lastUpdateTime) { - UserChatRoom userChatRoom = userChatRoomDao.findByUserAndChatRoomAndStatus(userByUserId, chatRoom, BaseStatusType.ACTIVE); + UserChatRoom userChatRoom = userChatRoomRepository.findByUserAndChatRoomAndStatus(userByUserId, chatRoom, BaseStatusType.ACTIVE); LocalDateTime lastReadTime = timeUtils.getEncodedTime(userChatRoom.getLastReadTime()); // LocalDateTime으로 변환 log.info("마지막으로 읽은 시간: " + lastReadTime); - int unreadMsgCount = chattingDao.countByChatRoomIdAndCreatedAtBetween( + int unreadMsgCount = chattingRepository.countByChatRoomIdAndCreatedAtBetween( chatRoom.getId(), lastReadTime, lastUpdateTime @@ -223,7 +223,7 @@ private int calculateUnreadMsgCount(User userByUserId, ChatRoom chatRoom, LocalD private List getUserInChatRoom(Space space, ChatRoom chatRoom) { List userList = new ArrayList<>(); - List userChatRoomList = userChatRoomDao.findByChatRoomAndStatus(chatRoom, BaseStatusType.ACTIVE); + List userChatRoomList = userChatRoomRepository.findByChatRoomAndStatus(chatRoom, BaseStatusType.ACTIVE); userChatRoomList.forEach(userChatRoom -> { User user = userChatRoom.getUser(); @@ -247,9 +247,9 @@ private void handleJoinChatRoom(List memberIdList, ChatRoom chatRoomByChat if (isUserInChatRoom(userByUserId, chatRoomByChatRoomId)) { // TODO 2: 유저가 채팅방에 초대되었다가 나간 경우 다시 초대 if (userByUserId.getStatus().equals(BaseStatusType.INACTIVE)) { - UserChatRoom userChatRoom = userChatRoomDao.findByUserAndChatRoomAndStatus(userByUserId, chatRoomByChatRoomId, BaseStatusType.ACTIVE); + UserChatRoom userChatRoom = userChatRoomRepository.findByUserAndChatRoomAndStatus(userByUserId, chatRoomByChatRoomId, BaseStatusType.ACTIVE); userChatRoom.setUserRejoin(); - userChatRoomDao.save(userChatRoom); + userChatRoomRepository.save(userChatRoom); continue; } // TODO 3: 유저가 채팅방에 초대되어서 참가하고 있는 경우 예외 발생 @@ -257,7 +257,7 @@ private void handleJoinChatRoom(List memberIdList, ChatRoom chatRoomByChat } // TODO 4: 채팅방에 초대된 적이 없는 유저에 대한 userChatRoom 테이블 생성 - userChatRoomDao.save(UserChatRoom.of(chatRoomByChatRoomId, userByUserId, LocalDateTime.now())); + userChatRoomRepository.save(UserChatRoom.of(chatRoomByChatRoomId, userByUserId, LocalDateTime.now())); } } } diff --git a/src/main/java/space/space_spring/service/ChattingService.java b/src/main/java/space/space_spring/service/ChattingService.java index 01f37b2f..25d4b81f 100644 --- a/src/main/java/space/space_spring/service/ChattingService.java +++ b/src/main/java/space/space_spring/service/ChattingService.java @@ -3,7 +3,7 @@ import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; -import space.space_spring.dao.chat.ChattingDao; +import space.space_spring.dao.chat.ChattingRepository; import space.space_spring.dto.chat.request.ChatMessageRequest; import space.space_spring.dto.chat.response.ChatMessageLogResponse; import space.space_spring.dto.chat.response.ChatMessageResponse; @@ -14,7 +14,6 @@ import space.space_spring.util.userSpace.UserSpaceUtils; import java.io.IOException; -import java.util.HashMap; import java.util.List; import java.util.stream.Collectors; @@ -27,7 +26,7 @@ public class ChattingService { private final S3Uploader s3Uploader; private final UserSpaceUtils userSpaceUtils; - private final ChattingDao chattingDao; + private final ChattingRepository chattingRepository; @Transactional public ChatMessageResponse sendChatMessage(Long senderId, ChatMessageRequest chatMessageRequest, Long chatRoomId) throws IOException { @@ -57,7 +56,7 @@ public ChatMessageResponse sendChatMessage(Long senderId, ChatMessageRequest cha } // TODO 4: DB에 메시지 저장 - ChatMessage message = chattingDao.insert(ChatMessage.of( + ChatMessage message = chattingRepository.insert(ChatMessage.of( chatMessageRequest.getContent(), chatRoomId, chatMessageRequest.getSpaceId(), @@ -71,7 +70,7 @@ public ChatMessageResponse sendChatMessage(Long senderId, ChatMessageRequest cha } public ChatMessageLogResponse readChatMessageLog(Long chatRoomId) { - List chatMessageList = chattingDao.findByChatRoomId(chatRoomId); + List chatMessageList = chattingRepository.findByChatRoomId(chatRoomId); return ChatMessageLogResponse.of(chatMessageList.stream() .map(ChatMessageResponse::of) .collect(Collectors.toList())); diff --git a/src/test/java/space/space_spring/repository/ChatRoomRepositoryTest.java b/src/test/java/space/space_spring/repository/ChatRoomRepositoryTest.java index 019d1570..3c0d120b 100644 --- a/src/test/java/space/space_spring/repository/ChatRoomRepositoryTest.java +++ b/src/test/java/space/space_spring/repository/ChatRoomRepositoryTest.java @@ -6,7 +6,7 @@ import org.springframework.context.annotation.Import; import space.space_spring.config.QueryDslConfig; import space.space_spring.dao.chat.ChatRoomRepository; -import space.space_spring.dao.chat.UserChatRoomDao; +import space.space_spring.dao.chat.UserChatRoomRepository; import space.space_spring.entity.ChatRoom; import space.space_spring.entity.Space; import space.space_spring.entity.User; @@ -28,7 +28,7 @@ public class ChatRoomRepositoryTest { private ChatRoomRepository chatRoomRepository; @Autowired - private UserChatRoomDao userChatRoomDao; + private UserChatRoomRepository userChatRoomRepository; private Space testSpace; @@ -91,8 +91,8 @@ public class ChatRoomRepositoryTest { ChatRoom savedTestChatRoom2 = chatRoomRepository.save(testChatRoom2); UserChatRoom userChatRoom1 = UserChatRoom.of(savedTestChatRoom1, testUser, LocalDateTime.now()); UserChatRoom userChatRoom2 = UserChatRoom.of(savedTestChatRoom2, testUser, LocalDateTime.now()); - userChatRoomDao.save(userChatRoom1); - userChatRoomDao.save(userChatRoom2); + userChatRoomRepository.save(userChatRoom1); + userChatRoomRepository.save(userChatRoom2); // when List chatRoomListByUser = chatRoomRepository.findByUserAndSpace(testUser, testSpace);