Skip to content

Commit

Permalink
refactor: chat 도메인 내의 dao -> repository 네이밍 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
hyunn522 committed Oct 31, 2024
1 parent bcde30a commit 750a00f
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChatRoom, Long>, ChatRoomDaoCustom {
public interface ChatRoomRepository extends JpaRepository<ChatRoom, Long>, ChatRoomRepositoryCustom {
ChatRoom findByIdAndStatus(Long chatRoomId, BaseStatusType status);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.List;

@Repository
public interface ChattingDao extends MongoRepository<ChatMessage, String> {
public interface ChattingRepository extends MongoRepository<ChatMessage, String> {
List<ChatMessage> findByChatRoomId(Long chatRoomId);

ChatMessage findTopByChatRoomIdOrderByCreatedAtDesc(Long chatRoomId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.List;


public interface UserChatRoomDao extends JpaRepository<UserChatRoom, Long> {
public interface UserChatRoomRepository extends JpaRepository<UserChatRoom, Long> {
UserChatRoom findByUserAndChatRoomAndStatus(User userByUserId, ChatRoom chatRoomByChatRoomId, BaseStatusType status);

List<UserChatRoom> findByChatRoomAndStatus(ChatRoom chatRoom, BaseStatusType status);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

import java.util.List;

public interface ChatRoomDaoCustom {
public interface ChatRoomRepositoryCustom {
List<ChatRoom> findByUserAndSpace(User user, Space space);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
36 changes: 18 additions & 18 deletions src/main/java/space/space_spring/service/ChatRoomService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 반환
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand All @@ -192,12 +192,12 @@ public ChatSuccessResponse deleteChatRoom(Long chatRoomId) {
}

private boolean isUserInChatRoom(User userByUserId, ChatRoom chatRoomByChatRoomId) {
List<UserChatRoom> chatRoomList = userChatRoomDao.findByChatRoomAndStatus(chatRoomByChatRoomId, BaseStatusType.ACTIVE);
List<UserChatRoom> 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<String, String> lastContent = lastMsg != null ? lastMsg.getContent() : new HashMap<>(Map.of("text", "메시지를 전송해보세요"));

Expand All @@ -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
Expand All @@ -223,7 +223,7 @@ private int calculateUnreadMsgCount(User userByUserId, ChatRoom chatRoom, LocalD

private List<UserInfoInSpace> getUserInChatRoom(Space space, ChatRoom chatRoom) {
List<UserInfoInSpace> userList = new ArrayList<>();
List<UserChatRoom> userChatRoomList = userChatRoomDao.findByChatRoomAndStatus(chatRoom, BaseStatusType.ACTIVE);
List<UserChatRoom> userChatRoomList = userChatRoomRepository.findByChatRoomAndStatus(chatRoom, BaseStatusType.ACTIVE);

userChatRoomList.forEach(userChatRoom -> {
User user = userChatRoom.getUser();
Expand All @@ -247,17 +247,17 @@ private void handleJoinChatRoom(List<Long> 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: 유저가 채팅방에 초대되어서 참가하고 있는 경우 예외 발생
throw new CustomException(USER_IS_ALREADY_IN_CHAT_ROOM);
}

// TODO 4: 채팅방에 초대된 적이 없는 유저에 대한 userChatRoom 테이블 생성
userChatRoomDao.save(UserChatRoom.of(chatRoomByChatRoomId, userByUserId, LocalDateTime.now()));
userChatRoomRepository.save(UserChatRoom.of(chatRoomByChatRoomId, userByUserId, LocalDateTime.now()));
}
}
}
9 changes: 4 additions & 5 deletions src/main/java/space/space_spring/service/ChattingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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 {
Expand Down Expand Up @@ -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(),
Expand All @@ -71,7 +70,7 @@ public ChatMessageResponse sendChatMessage(Long senderId, ChatMessageRequest cha
}

public ChatMessageLogResponse readChatMessageLog(Long chatRoomId) {
List<ChatMessage> chatMessageList = chattingDao.findByChatRoomId(chatRoomId);
List<ChatMessage> chatMessageList = chattingRepository.findByChatRoomId(chatRoomId);
return ChatMessageLogResponse.of(chatMessageList.stream()
.map(ChatMessageResponse::of)
.collect(Collectors.toList()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,7 +28,7 @@ public class ChatRoomRepositoryTest {
private ChatRoomRepository chatRoomRepository;

@Autowired
private UserChatRoomDao userChatRoomDao;
private UserChatRoomRepository userChatRoomRepository;

private Space testSpace;

Expand Down Expand Up @@ -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<ChatRoom> chatRoomListByUser = chatRoomRepository.findByUserAndSpace(testUser, testSpace);
Expand Down

0 comments on commit 750a00f

Please sign in to comment.