Skip to content

Commit

Permalink
feat: 채팅 마지막 인덱스 필드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
min9yu98 committed Aug 9, 2024
1 parent 7af30da commit 4190a99
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.example.tripmingle.dto.req.chat.GetAllChatMessageReqDTO;
import com.example.tripmingle.entity.ChatMessage;
import com.example.tripmingle.entity.ChatRoomType;
import com.example.tripmingle.port.out.ChatPersistPort;

import lombok.RequiredArgsConstructor;
Expand All @@ -14,7 +15,7 @@
@RequiredArgsConstructor
public class ChatPersistAdapter implements ChatPersistPort {

// private final ChatRepository chatRepository;
private final ChatRepository chatRepository;

@Override
public Slice<ChatMessage> getChatMessages(GetAllChatMessageReqDTO getAllChatMessageReqDTO, Pageable pageable) {
Expand All @@ -28,4 +29,10 @@ public Slice<ChatMessage> getChatMessages(GetAllChatMessageReqDTO getAllChatMess
public void save(ChatMessage chatMessage) {

}

@Override
public Long getChatMessagesCount(ChatRoomType chatRoomType, Long chatRoomId) {
// return chatRepository.countByChatRoomIdAndChatRoomType(chatRoomId, chatRoomType);
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.example.tripmingle.application.facadeService;

import static com.example.tripmingle.common.constants.Constants.*;

import java.util.List;

import org.springframework.stereotype.Service;

import com.example.tripmingle.application.service.BoardService;
import com.example.tripmingle.application.service.ChatRoomService;
import com.example.tripmingle.application.service.ChatService;
import com.example.tripmingle.application.service.UserService;
import com.example.tripmingle.dto.req.chat.EnterGroupChatRoomReqDTO;
import com.example.tripmingle.dto.req.chat.EnterOneOnOneChatRoomReqDTO;
Expand All @@ -32,14 +35,17 @@
public class ChatRoomFacadeService implements ChatRoomUseCase {

private final ChatRoomService chatRoomService;
private final ChatService chatService;
private final BoardService boardService;
private final UserService userService;

private ChatRoomUser generateChatRoomUserEntity(Long chatRoomId, Long userId, ChatRoomType chatRoomType) {
private ChatRoomUser generateChatRoomUserEntity(Long chatRoomId, Long userId, ChatRoomType chatRoomType,
Long chatFirstIndex) {
return ChatRoomUser.builder()
.chatRoomId(chatRoomId)
.user(userService.getUserById(userId))
.chatRoomType(chatRoomType)
.chatFirstIndex(chatFirstIndex)
.build();
}

Expand All @@ -54,13 +60,12 @@ public EnterOneOnOneChatRoomResDTO enterOneOnOneChatRoom(EnterOneOnOneChatRoomRe
.user2(contactUser)
.build();
OneOnOneChatRoom savedOneOnOneChatRoom = chatRoomService.saveOneOnOneChatRoom(oneOnOneChatRoom);

ChatRoomUser currentChatRoomUser = generateChatRoomUserEntity(savedOneOnOneChatRoom.getId(),
currentUser.getId(), ChatRoomType.ONE_ON_ONE);
currentUser.getId(), ChatRoomType.ONE_ON_ONE, FIRST_ENTER_CHAT_ROOM_CHAT_COUNT);
chatRoomService.joinOneOnOneChatRoom(currentChatRoomUser);

ChatRoomUser contactChatRoomUser = generateChatRoomUserEntity(savedOneOnOneChatRoom.getId(),
contactUser.getId(), ChatRoomType.ONE_ON_ONE);
contactUser.getId(), ChatRoomType.ONE_ON_ONE, FIRST_ENTER_CHAT_ROOM_CHAT_COUNT);
chatRoomService.joinOneOnOneChatRoom(contactChatRoomUser);
} else {
oneOnOneChatRoom = chatRoomService.getOneOnOneChatRoomByUserIds(currentUser.getId(), contactUser.getId());
Expand All @@ -83,16 +88,20 @@ public EnterGroupChatRoomResDTO enterGroupChatRoom(EnterGroupChatRoomReqDTO ente
.user(board.getUser())
.chatRoomId(groupChatRoom.getId())
.chatRoomType(ChatRoomType.GROUP)
.chatFirstIndex(FIRST_ENTER_CHAT_ROOM_CHAT_COUNT)
.build();
chatRoomService.joinGroupChatRoom(chatRoomMasterUser);
} else {
groupChatRoom = chatRoomService.getGroupChatRoomByBoardId(enterGroupChatRoomReqDTO.getBoardId());
}
if (!chatRoomService.existsUserInChatRoom(user.getId())) {
Long alreadyExistsChatCount = chatService.getChatMessagesCount(ChatRoomType.GROUP, groupChatRoom.getId());
ChatRoomUser chatRoomUser = ChatRoomUser.builder()
.chatRoomId(groupChatRoom.getId())
.user(user)
.chatRoomType(ChatRoomType.GROUP)
.chatFirstIndex(
alreadyExistsChatCount == 0 ? FIRST_ENTER_CHAT_ROOM_CHAT_COUNT : alreadyExistsChatCount - 1)
.build();
chatRoomService.joinGroupChatRoom(chatRoomUser);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.example.tripmingle.dto.req.chat.GetAllChatMessageReqDTO;
import com.example.tripmingle.entity.ChatMessage;
import com.example.tripmingle.entity.ChatRoomType;
import com.example.tripmingle.port.out.ChatPersistPort;

import lombok.RequiredArgsConstructor;
Expand All @@ -17,7 +18,10 @@ public class ChatService {
private final ChatPersistPort chatPersistPort;

public Slice<ChatMessage> getChatMessages(GetAllChatMessageReqDTO getAllChatMessageReqDTO, Pageable pageable) {
return chatPersistPort.getChatMessages(getAllChatMessageReqDTO, pageable);
return null;
}

public Long getChatMessagesCount(ChatRoomType chatRoomType, Long chatRoomId) {
return chatPersistPort.getChatMessagesCount(chatRoomType, chatRoomId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ public class Constants {
public static String SORT_CREATING_CRITERIA = "createdAt";
public static String SORT_ID_CRITERIA = "id";
public static Long NO_PARENT_COMMENT_ID = -1L;
public static Long FIRST_ENTER_CHAT_ROOM_CHAT_COUNT = 0L;
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@ public class ChatRoomUser extends BaseEntity {

private boolean connectionState;

private Long chatFirstIndex;

@Builder
public ChatRoomUser(User user, Long chatRoomId, ChatRoomType chatRoomType) {
public ChatRoomUser(User user, Long chatRoomId, ChatRoomType chatRoomType, Long chatFirstIndex) {
this.user = user;
this.chatRoomId = chatRoomId;
this.chatRoomType = chatRoomType;
this.chatFirstIndex = chatFirstIndex;
}

public void exitChatRoom() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@

import com.example.tripmingle.dto.req.chat.GetAllChatMessageReqDTO;
import com.example.tripmingle.entity.ChatMessage;
import com.example.tripmingle.entity.ChatRoomType;

public interface ChatPersistPort {

Slice<ChatMessage> getChatMessages(GetAllChatMessageReqDTO getAllChatMessageReqDTO, Pageable pageable);

void save(ChatMessage chatMessage);

Long getChatMessagesCount(ChatRoomType chatRoomType, Long chatRoomId);
}

0 comments on commit 4190a99

Please sign in to comment.