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/#207 chatroom domain #214

Merged
merged 14 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
21 changes: 21 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
services:
mogakgo-db:
image: mysql:8.2.0
container_name: mogakgo-mysql
environment:
MYSQL_ROOT_PASSWORD: root!
MYSQL_DATABASE: mogakgo
ports:
- "3306:3306"
mogakgo-redis:
image: redis:latest
container_name: mogakgo-redis
ports:
- "6379:6379"
mogakgo-mongodb:
image: mongo:latest
container_name: mogakgo-mongodb
environment:
MONGI_INIT_DATABSE: mogakgo
ports:
- "27017:27017"
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import jakarta.validation.constraints.NotNull;
import java.util.Objects;
import lombok.Getter;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.lang.Nullable;

Expand All @@ -17,7 +16,7 @@ public class CursorPaginationInfoReq {
private final int pageSize;

@Nullable
private final Sort.Direction sortOrder;
private final Direction sortOrder;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sort.Directionκ³Ό Direction의 차이가 λ­”κ°€μš”? (ꢁ금)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

λ™μΌν•œ ν΄λž˜μŠ€μ—μš”!


public CursorPaginationInfoReq(@Nullable Long cursorId, int pageSize, Direction sortOrder) {
this.cursorId = cursorId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import org.springframework.http.ResponseEntity;

@Tag(name = "Chat", description = "μ±„νŒ… κ΄€λ ¨ API")
Expand All @@ -38,7 +37,14 @@ public interface ChatSwagger {
examples = @ExampleObject(name = "E020301", value = SwaggerUserErrorExamples.USER_NOT_FOUND)
))
})
ResponseEntity<List<ChatRoomPublicRes>> getChatRoomList(@Parameter(hidden = true) Long userId);
@Parameters({
@Parameter(name = "cursorId", description = "기쀀이 λ˜λŠ” μ»€μ„œ ID", example = "1"),
@Parameter(name = "pageSize", description = "μš”μ²­ν•  데이터 크기", example = "5", required = true),
@Parameter(name = "sortOrder", description = "μ •λ ¬ λ°©ν–₯", example = "ASC"),
})
ResponseEntity<CursorPaginationResult<ChatRoomPublicRes>> getChatRoomList(
@Parameter(hidden = true) Long userId,
@Parameter(hidden = true) CursorPaginationInfoReq pageable);

@Operation(summary = "μ±„νŒ…λ°© 상세 쑰회", description = "μ±„νŒ…λ°©μ˜ 상세 정보λ₯Ό μ‘°νšŒν•˜λŠ” API")
@ApiResponses(value = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import io.oeid.mogakgo.core.properties.swagger.error.SwaggerProjectErrorExamples;
import io.oeid.mogakgo.core.properties.swagger.error.SwaggerUserErrorExamples;
import io.oeid.mogakgo.domain.geo.domain.enums.Region;
import io.oeid.mogakgo.domain.project.presentation.dto.req.ProjectCreateReq;
import io.oeid.mogakgo.domain.project.presentation.dto.res.ProjectDensityRankRes;
import io.oeid.mogakgo.domain.project.presentation.dto.res.ProjectDetailAPIRes;
import io.oeid.mogakgo.domain.project.presentation.dto.req.ProjectCreateReq;
import io.oeid.mogakgo.domain.project.presentation.dto.res.ProjectIdRes;
import io.oeid.mogakgo.domain.project.presentation.dto.res.ProjectInfoAPIRes;
import io.oeid.mogakgo.domain.project_join_req.presentation.dto.res.projectJoinRequestRes;
import io.oeid.mogakgo.domain.project_join_req.presentation.dto.res.ProjectJoinRequestRes;
import io.oeid.mogakgo.exception.dto.ErrorResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
Expand Down Expand Up @@ -168,7 +168,7 @@ ResponseEntity<CursorPaginationResult<ProjectDetailAPIRes>> getRandomOrderedProj
@Parameter(name = "pageSize", description = "μš”μ²­ν•  데이터 크기", example = "5", required = true),
@Parameter(name = "sortOrder", description = "μ •λ ¬ λ°©ν–₯", example = "ASC"),
})
ResponseEntity<CursorPaginationResult<projectJoinRequestRes>> getJoinRequest(
ResponseEntity<CursorPaginationResult<ProjectJoinRequestRes>> getJoinRequest(
@Parameter(hidden = true) Long userId,
@Parameter(description = "ν”„λ‘œμ νŠΈ ID", required = true) Long id,
@Parameter(hidden = true) CursorPaginationInfoReq pageable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@

import io.oeid.mogakgo.common.base.CursorPaginationInfoReq;
import io.oeid.mogakgo.common.base.CursorPaginationResult;
import io.oeid.mogakgo.domain.chat.application.dto.req.ChatRoomCreateReq;
import io.oeid.mogakgo.domain.chat.application.dto.res.ChatDataRes;
import io.oeid.mogakgo.domain.chat.application.dto.res.ChatRoomCreateRes;
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.infrastructure.ChatRepository;
import io.oeid.mogakgo.domain.chat.infrastructure.ChatRoomRoomJpaRepository;
import io.oeid.mogakgo.domain.chat.infrastructure.ChatUserJpaRepository;
import io.oeid.mogakgo.domain.matching.exception.MatchingException;
import io.oeid.mogakgo.domain.project.domain.entity.Project;
import io.oeid.mogakgo.domain.project.exception.ProjectException;
import io.oeid.mogakgo.domain.project.infrastructure.ProjectJpaRepository;
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;
Expand All @@ -31,55 +28,59 @@ public class ChatService {

private final UserCommonService userCommonService;
private final ChatRoomRoomJpaRepository chatRoomRepository;
private final ChatUserJpaRepository chatUserRepository;
private final ChatRepository chatRepository;
private final ProjectJpaRepository projectRepository;

// μ±„νŒ…λ°© 리슀트 쑰회
// TODO λ§ˆμ§€λ§‰ μ±„νŒ… 기둝 κ°€μ Έμ˜€κΈ° κ΅¬ν˜„
public List<ChatRoomPublicRes> findAllChatRoomByUserId(Long userId) {
findUserById(userId);
return chatRoomRepository.findAllChatRoomByUserId(userId);
public CursorPaginationResult<ChatRoomPublicRes> findAllChatRoomByUserId(Long userId,
CursorPaginationInfoReq pageable) {
log.info("findAllChatRoomByUserId - userId: {}", userId);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ‘πŸ» 이제 둜그 μ°λŠ”κ±° μΆ”κ°€ν•΄μ•Όν•˜λŠ”λ° 이 뢀뢄도 μ»¨λ²€μ…˜ 고민해보면 쒋을 λ“― ν•©λ‹ˆλ‹€~

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 ChatRoomCreateRes createChatRoom(Long creatorId, ChatRoomCreateReq request) {
Project project = projectRepository.findById(request.getProjectId())
.orElseThrow(() -> new MatchingException(ErrorCode404.PROJECT_NOT_FOUND));
User creator = findUserById(creatorId);
User sender = findUserById(request.getSenderId());
ChatRoom chatRoom = chatRoomRepository.save(
ChatRoom.builder().project(project).creator(creator).sender(sender).build());
chatRepository.createCollection(chatRoom.getId());
return ChatRoomCreateRes.from(chatRoom);
public void createChatRoom(Project project, User creator, User sender) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μœ„μ— μ½”λ“œμ—μ„œλŠ” μœ νš¨μ„±μ„ κ²€μ‚¬ν•˜λŠ” 둜직이 μžˆμ—ˆλŠ”λ° μƒˆλ‘œ 바뀐 λ‘œμ§μ—λŠ” μ—†λŠ” 것 κ°™μ•„μ„œ ν˜Ήμ‹œ μΌλΆ€λŸ¬ μ œκ±°ν•œ μ΄μœ κ°€ μžˆμ„κΉŒμš”?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이미 λ§€μΉ­μ„œλΉ„μŠ€μ—μ„œ μœ νš¨μ„± 검사λ₯Ό μ§„ν–‰ν•΄μ„œ μΆ”κ°€μ μœΌλ‘œ 진행할 ν•„μš”κ°€ μ—†λ‹€κ³  νŒλ‹¨ν–ˆμŠ΅λ‹ˆλ‹€!

ChatRoom chatRoom = chatRoomRepository.save(new ChatRoom(project));
chatUserRepository.save(chatRoom.join(creator));
chatUserRepository.save(chatRoom.join(sender));
chatRepository.createCollection(chatRoom.getId().toString());
}

@Transactional
public void leaveChatroom(Long userId, String chatRoomId) {
var user = findUserById(userId);
var chatRoom = findChatRoomById(chatRoomId);

chatRoom.leaveUser(user);
Copy link
Collaborator

@JIN-076 JIN-076 Mar 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ±„νŒ… 도메인 λ¦¬νŒ©ν•΄μ„œ 올렀주면 μ±„νŒ…λ°© λ‚˜κ°€κΈ° 둜직 μˆ˜μ •ν•˜κ² λ‹€κ³  μ–˜κΈ°ν–ˆμ—ˆλŠ”λ° 이미 κ΅¬ν˜„μ΄ λ‹€ λ˜μ–΄μžˆκ΅°μš”,,,! μ•žμœΌλ‘œλŠ” 미리 μ§œκ² λ‹€κ³  말해주면 쒋을 것 κ°™μ•„μš”πŸ˜­

// μ±„νŒ…λ°© λΉ„ν™œμ„±ν™”
chatRoom.closeChat();

chatRoom.leave(user);
}

// μ±„νŒ…λ°© 쑰회
public CursorPaginationResult<ChatDataRes> findAllChatInChatRoom(Long userId, String chatRoomId, CursorPaginationInfoReq pageable) {
var user = findUserById(userId);
var chatRoom = findChatRoomById(chatRoomId);
chatRoom.validateContainsUser(user);
public CursorPaginationResult<ChatDataRes> findAllChatInChatRoom(Long userId, String chatRoomId,
CursorPaginationInfoReq pageable) {
verifyChatUser(chatRoomId, userId);
return chatRepository.findAllByCollection(chatRoomId, pageable);
}

public ChatRoomDataRes findChatRoomDetailData(Long userId, String chatRoomId) {
var user = findUserById(userId);
var chatRoom = findChatRoomById(chatRoomId);
chatRoom.validateContainsUser(user);
var project = projectRepository.findById(chatRoom.getProject().getId())
.orElseThrow(() -> new ProjectException(ErrorCode404.PROJECT_NOT_FOUND));
return ChatRoomDataRes.from(project.getMeetingInfo());
log.info("findChatRoomDetailData - userId: {}, chatRoomId: {}", userId, chatRoomId);
verifyChatUser(chatRoomId, userId);
return chatRoomRepository.getChatDetailData(userId, UUID.fromString(chatRoomId));
}

private ChatRoom findChatRoomById(String chatRoomId) {
Expand All @@ -91,4 +92,14 @@ private User findUserById(Long userId) {
return userCommonService.getUserById(userId);
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
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.ChatRoomRoomJpaRepository;
import io.oeid.mogakgo.domain.user.application.UserCommonService;
import io.oeid.mogakgo.domain.user.domain.User;
import io.oeid.mogakgo.domain.chat.infrastructure.ChatUserJpaRepository;
import io.oeid.mogakgo.exception.code.ErrorCode400;
import io.oeid.mogakgo.exception.code.ErrorCode404;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -22,26 +21,26 @@
@Service
public class ChatWebSocketService {

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

public ChatDataRes handleChatMessage(Long userId, String roomId, ChatReq request) {
User user = userCommonService.getUserById(userId);
verifyChatRoomByRoomIdAndUser(roomId, user);
log.info("handleChatMessage userId: {}, roomId: {}", userId, roomId);
verifyChatRoomByRoomIdAndUser(roomId, userId);
ChatMessage chatMessage = chatRepository.save(
ChatMessage.builder().id(sequenceGeneratorService.generateSequence(roomId))
.senderId(user.getId())
.senderId(userId)
.messageType(request.getMessageType())
.message(request.getMessage())
.build(), roomId);
return ChatDataRes.from(chatMessage);
}

private void verifyChatRoomByRoomIdAndUser(String roomId, User user) {
ChatRoom chatRoom = chatRoomJpaRepository.findByIdAndUser(roomId, user)
.orElseThrow(() -> new ChatException(ErrorCode404.CHAT_ROOM_NOT_FOUND));
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)) {
throw new ChatException(ErrorCode400.CHAT_ROOM_CLOSED);
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
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.swagger.v3.oas.annotations.media.Schema;
import java.time.LocalDateTime;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Schema(description = "μ±„νŒ…λ°© ν”„λ‘œμ νŠΈ 정보")
@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor
public class ChatRoomDataRes {

Expand All @@ -25,13 +23,16 @@ public class ChatRoomDataRes {
@Schema(description = "ν”„λ‘œμ νŠΈ μ’…λ£Œ μ‹œκ°„")
private LocalDateTime meetEndTime;

public static ChatRoomDataRes from(MeetingInfo meetingInfo) {
private ChatUserInfo chatUserInfo;

public ChatRoomDataRes(MeetingInfo meetingInfo, ChatUserInfo chatUserInfo) {
var meetLocation = meetingInfo.getMeetLocation();
return new ChatRoomDataRes(
meetingInfo.getMeetDetail(),
meetingInfo.getMeetStartTime(),
meetLocation.getX(),
meetLocation.getY(),
meetingInfo.getMeetEndTime());
this.meetDetail = meetingInfo.getMeetDetail();
this.meetStartTime = meetingInfo.getMeetStartTime();
this.meetLocationLatitude = meetLocation.getX();
this.meetLocationLongitude = meetLocation.getY();
this.meetEndTime = meetingInfo.getMeetEndTime();
this.chatUserInfo = chatUserInfo;
}

}
Loading
Loading