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

Refact/#362 chat domain #367

Merged
merged 19 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
86cae82
[REFACT] ChatMessageType Deprecated ์ฒ˜๋ฆฌ
tidavid1 Apr 15, 2024
46c50b6
[REFACT] ChatRoom Document ๊ตฌํ˜„
tidavid1 Apr 15, 2024
bc49ee9
[TEST] `ChatRoom` Document ํ…Œ์ŠคํŠธ ์ถ”๊ฐ€
tidavid1 Apr 15, 2024
4a2e1d3
[REFACT] `ChatRoom` ์„œ๋น„์Šค ๊ตฌํ˜„
tidavid1 Apr 17, 2024
0c00015
[REFACT] `ChatMessageType` Enum ์ œ๊ฑฐ
tidavid1 Apr 17, 2024
96e99f7
[REFACT] `ChatMessage` ๋ฆฌํŽ™ํ† ๋ง ๋ฐ ํ…Œ์ŠคํŠธ
tidavid1 Apr 17, 2024
9d3ed6e
[REFACT] ์ฑ„ํŒ… ์ „์†ก์‹œ ์ฑ„ํŒ…๋ฐฉ ๋งˆ์ง€๋ง‰ ๋ฉ”์‹œ์ง€ ๊ฐฑ์‹  ๋ฐฉ์‹์œผ๋กœ ๋ฆฌํŒฉํ† ๋ง
tidavid1 Apr 17, 2024
17a5e58
[TEST] `ChatRoom` Document ํ…Œ์ŠคํŠธ ์ถ”๊ฐ€
tidavid1 Apr 17, 2024
664cf57
[REFACT] `ChatController` ๋ฆฌํŽ™ํ† ๋ง
tidavid1 Apr 17, 2024
a1c9d2d
[REFACT] `ChatService` ๋ฆฌํŽ™ํ† ๋ง
tidavid1 Apr 17, 2024
606b3ce
[REFACT] `ChatRoomDataRes` ๋ฆฌํŽ™ํ† ๋ง
tidavid1 Apr 17, 2024
e5de7f0
[REFACT] ์ฑ„ํŒ…๋ฐฉ ์ƒ์„ฑ ๋กœ์ง ์˜ค๋ฅ˜ ์ˆ˜์ •
tidavid1 Apr 17, 2024
85466da
[REMOVE] ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ํด๋ž˜์Šค ์ œ๊ฑฐ ๋ฐ `@Deprecated` ์ฒ˜๋ฆฌ
tidavid1 Apr 17, 2024
062f2d5
[TEST] Mock ๊ฐ์ฒด ์ž‘๋™ ๋ฐฉ์‹ ์ˆ˜์ •
tidavid1 Apr 18, 2024
d0da978
[TEST] `ChatService` ํ…Œ์ŠคํŠธ ์ถ”๊ฐ€
tidavid1 Apr 18, 2024
c3001d1
[REFACT] ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ ์ œ๊ฑฐ
tidavid1 Apr 18, 2024
af2afdb
[TEST] `ChatWebSocketService` ํ…Œ์ŠคํŠธ ์ถ”๊ฐ€
tidavid1 Apr 19, 2024
7cb783d
[REFACT] API ์‘๋‹ต ๋กค๋ฐฑ
tidavid1 Apr 25, 2024
d926a78
Merge branch 'develop' into refact/#362-chat_domain
tidavid1 Apr 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.oeid.mogakgo.common.base;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Min;
import java.util.Objects;
import lombok.Getter;
import org.springframework.data.domain.Sort.Direction;
Expand All @@ -9,16 +9,14 @@
@Getter
public class CursorPaginationInfoReq {

@Nullable
private final Long cursorId;

@NotNull
@Min(1)
private final int pageSize;

@Nullable
private final Direction sortOrder;

public CursorPaginationInfoReq(@Nullable Long cursorId, int pageSize, Direction sortOrder) {
public CursorPaginationInfoReq(@Nullable Long cursorId, int pageSize,
@Nullable Direction sortOrder) {
this.cursorId = cursorId;
this.pageSize = pageSize;
// ์ตœ๊ทผ์ˆœ ๊ธฐ๋ณธ
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/io/oeid/mogakgo/core/configuration/MongoConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.oeid.mogakgo.core.configuration;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.EnableMongoAuditing;

@Configuration
@EnableMongoAuditing
public class MongoConfig {

}
112 changes: 36 additions & 76 deletions src/main/java/io/oeid/mogakgo/domain/chat/application/ChatService.java
Original file line number Diff line number Diff line change
@@ -1,115 +1,75 @@
package io.oeid.mogakgo.domain.chat.application;

import io.oeid.mogakgo.common.base.CursorPaginationInfoReq;
import io.oeid.mogakgo.common.base.CursorPaginationResult;
import io.oeid.mogakgo.domain.chat.application.dto.res.ChatRoomDataRes;
import io.oeid.mogakgo.domain.chat.application.dto.res.ChatRoomPublicRes;
import io.oeid.mogakgo.domain.chat.entity.ChatRoom;
import io.oeid.mogakgo.domain.chat.entity.document.ChatRoom;
import io.oeid.mogakgo.domain.chat.entity.vo.ChatRoomDetail;
import io.oeid.mogakgo.domain.chat.exception.ChatException;
import io.oeid.mogakgo.domain.chat.infrastructure.ChatRepository;
import io.oeid.mogakgo.domain.chat.infrastructure.ChatRoomRoomJpaRepository;
import io.oeid.mogakgo.domain.chat.infrastructure.ChatUserJpaRepository;
import io.oeid.mogakgo.domain.chat.infrastructure.ChatRoomDocumentRepository;
import io.oeid.mogakgo.domain.chat.presentation.dto.res.ChatDataApiRes;
import io.oeid.mogakgo.domain.project.domain.entity.Project;
import io.oeid.mogakgo.domain.user.application.UserCommonService;
import io.oeid.mogakgo.domain.user.domain.User;
import io.oeid.mogakgo.exception.code.ErrorCode404;
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class ChatService {

private final UserCommonService userCommonService;
private final ChatRoomRoomJpaRepository chatRoomRepository;
private final ChatUserJpaRepository chatUserRepository;
private final ChatRoomDocumentRepository chatRoomRepository;
private final ChatRepository chatRepository;
private final ChatIdSequenceGeneratorService sequenceGeneratorService;

// ์ฑ„ํŒ…๋ฐฉ ๋ฆฌ์ŠคํŠธ ์กฐํšŒ
public CursorPaginationResult<ChatRoomPublicRes> findAllChatRoomByUserId(Long userId,
CursorPaginationInfoReq pageable) {
log.info("findAllChatRoomByUserId - userId: {}", userId);
var chatRoomList = chatRoomRepository.getChatRoomList(userId, pageable.getCursorId(),
pageable.getPageSize());
var result = chatRoomList.stream().map(
chatRoom -> {
var user = chatRoom.getOppositeUser(userId);
ChatRoomPublicRes res = ChatRoomPublicRes.of(chatRoom, user);
var chatMessage = chatRepository.findLastChatByCollection(
chatRoom.getId().toString());
chatMessage.ifPresent(
message -> res.addLastMessage(message.getMessage(), message.getCreatedAt()));
return res;
}
).toList();
return CursorPaginationResult.fromDataWithExtraItemForNextCheck(result,
pageable.getPageSize());
}

// ์ฑ„ํŒ…๋ฐฉ ์ƒ์„ฑ
@Transactional
public void createChatRoom(Project project, User creator, User sender) {
ChatRoom chatRoom = chatRoomRepository.save(new ChatRoom(project));
chatUserRepository.save(chatRoom.join(creator));
chatUserRepository.save(chatRoom.join(sender));
chatRepository.createCollection(chatRoom.getId().toString());
var cursorId = sequenceGeneratorService.generateSequence("chatroom_metadata");
var chatRoom = ChatRoom.of(cursorId, UUID.randomUUID(), ChatRoomDetail.from(project));
chatRoom.addParticipant(creator);
chatRoom.addParticipant(sender);
chatRepository.createCollection(chatRoom.getRoomId().toString());
chatRoomRepository.save(chatRoom);
}

@Transactional
public void leaveChatroom(Long userId, String chatRoomId) {
var user = findUserById(userId);
var chatRoom = findChatRoomById(chatRoomId);
chatRoom.leaveUser(user);
// ์ฑ„ํŒ…๋ฐฉ ๋น„ํ™œ์„ฑํ™”
chatRoom.closeChat();
}

// ์ฑ„ํŒ…๋ฐฉ ์กฐํšŒ
public CursorPaginationResult<ChatDataApiRes> findAllChatInChatRoom(Long userId,
String chatRoomId,
CursorPaginationInfoReq pageable) {
verifyChatUser(chatRoomId, userId);
return chatRepository.findAllByCollection(chatRoomId, pageable);
public void leaveChatRoom(UUID roomId, Long userId) {
var chatRoom = chatRoomRepository.findByRoomIdAndUserId(roomId, userId)
.orElseThrow(() -> new ChatException(ErrorCode404.CHAT_ROOM_NOT_FOUND));
chatRoom.leaveChatRoom(userId);
chatRoomRepository.save(chatRoom);
}

public ChatRoomDataRes findChatRoomDetailData(Long userId, String chatRoomId) {
log.info("findChatRoomDetailData - userId: {}, chatRoomId: {}", userId, chatRoomId);
verifyChatUser(chatRoomId, userId);
return chatRoomRepository.getChatDetailData(userId, UUID.fromString(chatRoomId));
public List<ChatRoomPublicRes> findChatRoomsByUserId(Long userId, Long cursorId, int pageSize) {
return chatRoomRepository.findChatRoomsByUserId(userId, cursorId, pageSize).stream()
.map(chatRoom -> ChatRoomPublicRes.of(chatRoom, userId)).toList();
}

public String findChatRoomIdByProjectId(Long userId, Long projectId) {
var user = findUserById(userId);
var chatRoom = chatRoomRepository.findByProject_Id(projectId)
public ChatRoomDataRes findChatRoomDetailData(UUID roomId, Long userId) {
var chatRoom = chatRoomRepository.findByRoomIdAndUserId(roomId, userId)
.orElseThrow(() -> new ChatException(ErrorCode404.CHAT_ROOM_NOT_FOUND));
verifyChatUser(chatRoom.getId().toString(), user.getId());
return chatRoom.getId().toString();
var userInfo = chatRoom.getParticipants().values().stream()
.filter(info -> !info.userId().equals(userId))
.findFirst().orElseThrow(() -> new ChatException(ErrorCode404.CHAT_USER_NOT_FOUND));
return new ChatRoomDataRes(chatRoom.getChatRoomDetail(), userInfo);
}

private ChatRoom findChatRoomById(String chatRoomId) {
return chatRoomRepository.findById(chatRoomId)
.orElseThrow(() -> new ChatException(ErrorCode404.CHAT_ROOM_NOT_FOUND));
public UUID findChatRoomIdByProjectId(Long projectId) {
return chatRoomRepository.findByProjectId(projectId)
.orElseThrow(() -> new ChatException(ErrorCode404.CHAT_ROOM_NOT_FOUND)).getRoomId();
}

private User findUserById(Long userId) {
return userCommonService.getUserById(userId);
public List<ChatDataApiRes> findChatMessagesByRoomId(UUID roomId, Long userId,
Long cursorId, int pageSize) {
var chatRoom = chatRoomRepository.findByRoomIdAndUserId(roomId, userId)
.orElseThrow(() -> new ChatException(ErrorCode404.CHAT_ROOM_NOT_FOUND));
return chatRepository.findAllByCollection(chatRoom.getRoomId().toString(), cursorId,
pageSize).stream().map(ChatDataApiRes::from).toList();
}

private void verifyChatUser(String chatRoomIdStr, Long userId) {
log.info("verifyChatUser - chatRoomId: {}, userId: {}", chatRoomIdStr, userId);
UUID chatRoomId = UUID.fromString(chatRoomIdStr);
chatUserRepository.findByChatRoomIdAndUserId(chatRoomId, userId)
.ifPresentOrElse(chatuser -> {
},
() -> {
throw new ChatException(ErrorCode404.CHAT_USER_NOT_FOUND);
});
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package io.oeid.mogakgo.domain.chat.application;


import io.oeid.mogakgo.domain.chat.application.dto.req.ChatReq;
import io.oeid.mogakgo.domain.chat.application.dto.res.ChatDataRes;
import io.oeid.mogakgo.domain.chat.entity.ChatRoom;
import io.oeid.mogakgo.domain.chat.entity.document.ChatMessage;
import io.oeid.mogakgo.domain.chat.entity.enums.ChatStatus;
import io.oeid.mogakgo.domain.chat.exception.ChatException;
import io.oeid.mogakgo.domain.chat.infrastructure.ChatRepository;
import io.oeid.mogakgo.domain.chat.infrastructure.ChatUserJpaRepository;
import io.oeid.mogakgo.domain.user.infrastructure.UserJpaRepository;
import io.oeid.mogakgo.domain.chat.infrastructure.ChatRoomDocumentRepository;
import io.oeid.mogakgo.exception.code.ErrorCode400;
import io.oeid.mogakgo.exception.code.ErrorCode404;
import java.util.UUID;
Expand All @@ -22,35 +19,24 @@
@Service
public class ChatWebSocketService {

private final ChatUserJpaRepository chatUserRepository;
private final ChatRepository chatRepository;
private final ChatIdSequenceGeneratorService sequenceGeneratorService;
private final UserJpaRepository userRepository;

public ChatDataRes handleChatMessage(Long userId, String roomId, ChatReq request) {
log.info("handleChatMessage userId: {}, roomId: {}", userId, roomId);
verifyChatRoomByRoomIdAndUser(roomId, userId);
var receiver = chatUserRepository.findReceiverByRoomIdAndUserId(UUID.fromString(roomId),
userId)
.orElseThrow(() -> new ChatException(ErrorCode404.CHAT_USER_NOT_FOUND));
var sender = userRepository.findById(userId)
.orElseThrow(() -> new ChatException(ErrorCode404.CHAT_USER_NOT_FOUND));
ChatMessage chatMessage = chatRepository.save(
ChatMessage.builder().id(sequenceGeneratorService.generateSequence(roomId))
.senderId(userId)
.messageType(request.getMessageType())
.message(request.getMessage())
.build(), roomId);
return ChatDataRes.of(receiver.getUser().getId(), sender.getUsername(), chatMessage);
}
private final ChatRepository chatRepository;
private final ChatRoomDocumentRepository chatroomRepository;

private void verifyChatRoomByRoomIdAndUser(String roomId, Long userId) {
log.info("verifyChatRoomByRoomIdAndUser - roomId: {}, UserId: {}", roomId, userId);
ChatRoom chatRoom = chatUserRepository.findByChatRoomIdAndUserId(UUID.fromString(roomId),
userId)
.orElseThrow(() -> new ChatException(ErrorCode404.CHAT_ROOM_NOT_FOUND)).getChatRoom();
if (chatRoom.getStatus().equals(ChatStatus.CLOSED)) {
public ChatDataRes handleChatMessage(UUID roomId, Long userId, String message) {
var chatRoom = chatroomRepository.findByRoomIdAndUserId(roomId, userId)
.orElseThrow(() -> new ChatException(ErrorCode404.CHAT_ROOM_NOT_FOUND));
if (chatRoom.getChatStatus().equals(ChatStatus.CLOSED)) {
throw new ChatException(ErrorCode400.CHAT_ROOM_CLOSED);
}
var chatMessage = chatRepository.save(
ChatMessage.builder().id(sequenceGeneratorService.generateSequence(roomId.toString()))
.senderId(userId)
.message(message)
.build(), roomId.toString());
chatRoom.updateLastMessage(chatMessage);
chatroomRepository.save(chatRoom);
return ChatDataRes.of(chatRoom, chatMessage);
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.oeid.mogakgo.domain.chat.application.dto.res;

import io.oeid.mogakgo.domain.chat.entity.document.ChatMessage;
import io.oeid.mogakgo.domain.chat.entity.enums.ChatMessageType;
import io.oeid.mogakgo.domain.chat.entity.document.ChatRoom;
import io.oeid.mogakgo.domain.chat.presentation.dto.res.ChatDataApiRes;
import java.time.LocalDateTime;
import lombok.AccessLevel;
Expand All @@ -14,24 +14,24 @@ public class ChatDataRes {

private Long receiverId;
private Long id;
private ChatMessageType messageType;
private Long senderId;
private String senderUserName;
private String message;
private LocalDateTime createdAt;

public static ChatDataRes of(Long receiverId, String senderUserName, ChatMessage chatMessage) {
public static ChatDataRes of(ChatRoom chatRoom, ChatMessage chatMessage) {
var sender = chatRoom.getParticipantUserInfo(chatMessage.getSenderId());
var receiver = chatRoom.getOpponentUserInfo(chatMessage.getSenderId());
return new ChatDataRes(
receiverId,
receiver.userId(),
chatMessage.getId(),
chatMessage.getMessageType(),
chatMessage.getSenderId(),
senderUserName,
sender.username(),
chatMessage.getMessage(),
chatMessage.getCreatedAt());
}

public ChatDataApiRes toApiResponse() {
return ChatDataApiRes.of(id, messageType, senderId, message, createdAt);
return ChatDataApiRes.of(id, senderId, message, createdAt);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package io.oeid.mogakgo.domain.chat.application.dto.res;

import io.oeid.mogakgo.domain.chat.application.vo.ChatUserInfo;
import io.oeid.mogakgo.domain.project.domain.entity.vo.MeetingInfo;
import io.oeid.mogakgo.domain.chat.application.vo.ChatUserInfoRes;
import io.oeid.mogakgo.domain.chat.entity.vo.ChatRoomDetail;
import io.oeid.mogakgo.domain.chat.entity.vo.ChatUserInfo;
import io.swagger.v3.oas.annotations.media.Schema;
import java.time.LocalDateTime;
import lombok.Getter;
Expand All @@ -23,16 +24,15 @@ public class ChatRoomDataRes {
@Schema(description = "ํ”„๋กœ์ ํŠธ ์ข…๋ฃŒ ์‹œ๊ฐ„")
private LocalDateTime meetEndTime;

private ChatUserInfo chatUserInfo;
private ChatUserInfoRes chatUserInfoRes;

public ChatRoomDataRes(MeetingInfo meetingInfo, ChatUserInfo chatUserInfo) {
var meetLocation = meetingInfo.getMeetLocation();
this.meetDetail = meetingInfo.getMeetDetail();
this.meetStartTime = meetingInfo.getMeetStartTime();
this.meetLocationLatitude = meetLocation.getX();
this.meetLocationLongitude = meetLocation.getY();
this.meetEndTime = meetingInfo.getMeetEndTime();
this.chatUserInfo = chatUserInfo;
public ChatRoomDataRes(ChatRoomDetail chatRoomDetail, ChatUserInfo chatUserInfo) {
this.meetDetail = chatRoomDetail.getMeetDetail();
this.meetStartTime = chatRoomDetail.getMeetStartTime();
this.meetLocationLatitude = chatRoomDetail.getMeetLocationLatitude();
this.meetLocationLongitude = chatRoomDetail.getMeetLocationLongitude();
this.meetEndTime = chatRoomDetail.getMeetEndTime();
this.chatUserInfoRes = ChatUserInfoRes.from(chatUserInfo);
}

}
Loading
Loading