Skip to content

Commit

Permalink
refactor: Method signatures updated to use entities instead of IDs
Browse files Browse the repository at this point in the history
- Updated `jointChatRoom` method to take `ChatRoomEntity` and `MemberEntity` instead of their IDs.
- Adjusted `createChatRoom` method to reflect these changes.
- Improved comments and corrected minor typos for better code readability.
```

관련 이슈: #123
참조: https://example.com/issue/123

Signed-off-by: Namgyu11 <103015031+Namgyu11@users.noreply.github.com>
  • Loading branch information
Namgyu11 committed Oct 8, 2024
1 parent 057b67d commit 7d92c9b
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public void createAccompanyPost(Long memberId, CreateAccompanyPostRequest reques
new AccompanyStatusEntity(post, AccompanyStatusEnum.PROGRESSING));

// 채팅방 생성 및 게시물 작성자 채팅방 자동 참여 처리
chatRoomService.createChatRoom(post.getId(), memberId);
chatRoomService.createChatRoom(post, memberEntity);
}

private String generateShortUrl(String input) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package connectripbe.connectrip_be.chat.service;

import connectripbe.connectrip_be.chat.entity.ChatRoomEntity;
import connectripbe.connectrip_be.chat.entity.ChatRoomMemberEntity;
import connectripbe.connectrip_be.member.entity.MemberEntity;

public interface ChatRoomMemberService {

void jointChatRoom(Long chatRoomId, Long memberId);
ChatRoomMemberEntity jointChatRoom(ChatRoomEntity chatRoom, MemberEntity member);

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package connectripbe.connectrip_be.chat.service;

import connectripbe.connectrip_be.accompany.post.entity.AccompanyPostEntity;
import connectripbe.connectrip_be.chat.dto.ChatRoomEnterDto;
import connectripbe.connectrip_be.chat.dto.ChatRoomListResponse;
import connectripbe.connectrip_be.chat.dto.ChatRoomMemberResponse;
import connectripbe.connectrip_be.chat.dto.ChatUnreadMessagesResponse;
import connectripbe.connectrip_be.member.entity.MemberEntity;
import java.util.List;

public interface ChatRoomService {
Expand All @@ -12,7 +14,7 @@ public interface ChatRoomService {

List<ChatRoomMemberResponse> getChatRoomMembers(Long chatRoomId, Long memberId);

void createChatRoom(Long postId, Long memberId);
void createChatRoom(AccompanyPostEntity postEntity, MemberEntity memberEntity);

void exitChatRoom(Long chatRoomId, Long id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
import connectripbe.connectrip_be.chat.entity.ChatRoomMemberEntity;
import connectripbe.connectrip_be.chat.entity.type.ChatRoomMemberStatus;
import connectripbe.connectrip_be.chat.repository.ChatRoomMemberRepository;
import connectripbe.connectrip_be.chat.repository.ChatRoomRepository;
import connectripbe.connectrip_be.chat.service.ChatRoomMemberService;
import connectripbe.connectrip_be.global.exception.GlobalException;
import connectripbe.connectrip_be.global.exception.type.ErrorCode;
import connectripbe.connectrip_be.member.entity.MemberEntity;
import connectripbe.connectrip_be.member.repository.MemberJpaRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand All @@ -18,29 +16,21 @@
public class ChatRoomMemberServiceImpl implements ChatRoomMemberService {

private final ChatRoomMemberRepository chatRoomMemberRepository;
private final ChatRoomRepository chatRoomRepository;
private final MemberJpaRepository memberRepository;


/**
* 사용자를 특정 채팅방에 참여 채팅방과 사용자가 존재하는지 확인한 후, 사용자가 이미 채팅방에 참여 중인지 확인 참여하지 않은 경우 채팅방에 사용자를 추가하고, 참여한 경우 예외를 발생
*
* @param chatRoomId 참여할 채팅방의 ID
* @param memberId 참여할 사용자의 ID
* @param chatRoom 참여할 채팅방의 ID
* @param member 참여할 사용자의 ID
* @throws GlobalException 채팅방이 존재하지 않거나, 사용자가 존재하지 않거나, 사용자가 이미 채팅방에 참여한 경우 발생하는 예외
*/
@Override
public void jointChatRoom(Long chatRoomId, Long memberId) {

// 채팅방이 존재하는지 확인
ChatRoomEntity chatRoom = chatRoomRepository.findById(chatRoomId)
.orElseThrow(() -> new GlobalException(ErrorCode.CHAT_ROOM_NOT_FOUND));

// 사용자가 존재하는지 확인
MemberEntity member = memberRepository.findById(memberId)
.orElseThrow(() -> new GlobalException(ErrorCode.USER_NOT_FOUND));
public ChatRoomMemberEntity jointChatRoom(ChatRoomEntity chatRoom, MemberEntity member) {

// 이미 채팅방에 참여중인지 확인
boolean isMemberAlreadyInRoom = chatRoomMemberRepository.existsByChatRoomIdAndMemberId(chatRoomId, memberId);
boolean isMemberAlreadyInRoom = chatRoomMemberRepository.existsByChatRoomIdAndMemberId(chatRoom.getId(),
member.getId());

if (isMemberAlreadyInRoom) {
throw new GlobalException(ErrorCode.ALREADY_JOINED_CHAT_ROOM);
Expand All @@ -55,5 +45,7 @@ public void jointChatRoom(Long chatRoomId, Long memberId) {
chatRoom.addChatRoomMember(chatRoomMember);
chatRoomMemberRepository.save(chatRoomMember);

return chatRoomMember;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import connectripbe.connectrip_be.accompany.post.entity.AccompanyPostEntity;
import connectripbe.connectrip_be.accompany.post.exception.NotFoundAccompanyPostException;
import connectripbe.connectrip_be.accompany.post.repository.AccompanyPostRepository;
import connectripbe.connectrip_be.accompany.status.entity.AccompanyStatusEntity;
import connectripbe.connectrip_be.accompany.status.entity.AccompanyStatusEnum;
import connectripbe.connectrip_be.accompany.status.repository.AccompanyStatusJpaRepository;
Expand All @@ -23,6 +22,7 @@
import connectripbe.connectrip_be.chat.service.ChatRoomService;
import connectripbe.connectrip_be.global.exception.GlobalException;
import connectripbe.connectrip_be.global.exception.type.ErrorCode;
import connectripbe.connectrip_be.member.entity.MemberEntity;
import connectripbe.connectrip_be.pending_list.entity.PendingListEntity;
import connectripbe.connectrip_be.pending_list.entity.type.PendingStatus;
import connectripbe.connectrip_be.pending_list.repository.PendingListRepository;
Expand All @@ -42,7 +42,6 @@ public class ChatRoomServiceImpl implements ChatRoomService {

private final ChatRoomRepository chatRoomRepository;
private final ChatRoomMemberRepository chatRoomMemberRepository;
private final AccompanyPostRepository accompanyPostRepository;
private final AccompanyStatusJpaRepository accompanyStatusJpaRepository;
private final PendingListRepository pendingListRepository;
private final ChatMessageRepository chatMessageRepository;
Expand Down Expand Up @@ -122,37 +121,36 @@ private ChatRoomMemberResponse chatRoomMemberResponse(ChatRoomMemberEntity chatR
/**
* 특정 게시물에 대한 채팅방을 생성하고, 게시물 작성자를 해당 채팅방에 자동으로 참여. 생성된 채팅방에서 게시물 작성자는 초기 방장으로 설정.
*
* @param postId 채팅방을 생성할 게시물의 ID
* @param memberId 채팅방에 자동으로 참여시킬 게시물 작성자의 ID
* @param postEntity 채팅방을 생성할 게시물의 ID
* @param memberEntity 채팅방에 자동으로 참여시킬 게시물 작성자의 ID
* @throws GlobalException 게시물을 찾을 수 없거나, 게시물 작성자를 채팅방에 참여시키는 데 문제가 발생한 경우 발생하는 예외
*/
@Override
public void createChatRoom(Long postId, Long memberId) {
public void createChatRoom(AccompanyPostEntity postEntity, MemberEntity memberEntity) {
ChatRoomEntity chatRoom = ChatRoomEntity
.builder()
.accompanyPost(accompanyPostRepository.findById(postId)
.orElseThrow(() -> new GlobalException(ErrorCode.POST_NOT_FOUND)))
.accompanyPost(postEntity)
.chatRoomType(ChatRoomType.ACTIVE)
.build();

// 채팅방 생성
chatRoomRepository.save(chatRoom);

// 채팅방에 게시물 작성자 자동 참여
chatRoomMemberService.jointChatRoom(chatRoom.getId(), memberId);

// 채팅방 방장 초기 설정
ChatRoomMemberEntity leaderMember = chatRoomMemberRepository
.findByChatRoom_IdAndMember_Id(chatRoom.getId(), memberId)
.orElseThrow(() -> new GlobalException(ErrorCode.CHAT_ROOM_MEMBER_NOT_FOUND));
ChatRoomMemberEntity leaderMember = chatRoomMemberService.jointChatRoom(chatRoom, memberEntity);
//
// // 채팅방 방장 초기 설정
// ChatRoomMemberEntity leaderMember = chatRoomMemberRepository
// .findByChatRoom_IdAndMember_Id(chatRoom.getId(), memberId)
// .orElseThrow(() -> new GlobalException(ErrorCode.CHAT_ROOM_MEMBER_NOT_FOUND));

// 방장 설정
chatRoom.setInitialLeader(leaderMember);

ChatMessage chatMessage = ChatMessage.builder()
.type(MessageType.ENTER)
.chatRoomId(chatRoom.getId())
.senderId(memberId)
.senderId(memberEntity.getId())
.senderNickname(leaderMember.getMember().getNickname())
.senderProfileImage(leaderMember.getMember().getProfileImagePath())
.content(leaderMember.getMember().getNickname() + "님이 입장하셨습니다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public PendingResponse accompanyPending(Long memberId, Long accompanyPostId) {
member)
.orElseThrow(() -> new GlobalException(ErrorCode.PENDING_NOT_FOUND));

// 상태가 DEFAULT라면 PENDING으로 업데이트
// 상태가 DEFAULT 라면 PENDING 으로 업데이트
if (existingPending.getStatus().equals(PendingStatus.DEFAULT)) {
existingPending.updateStatus(PendingStatus.PENDING);
pendingListRepository.save(existingPending);
Expand Down Expand Up @@ -203,15 +203,15 @@ public PendingResponse acceptPending(Long memberId, Long accompanyPostId) {
ChatRoomEntity chatRoom = chatRoomRepository.findByAccompanyPost_Id(accompanyPostId)
.orElseThrow(() -> new GlobalException(ErrorCode.CHATROOM_NOT_FOUND));

chatRoomMemberService.jointChatRoom(chatRoom.getId(), memberId);
chatRoomMemberService.jointChatRoom(chatRoom, member);

return PendingResponse.builder()
.status(pending.getStatus().toString())
.build();
}

/**
* 사용자의 동행 신청을 거절합니다. 신청 상태를 REJECTED로 변경합니다.
* 사용자의 동행 신청을 거절합니다. 신청 상태를 REJECTED 로 변경합니다.
*
* @param memberId 거절할 사용자의 ID
* @param accompanyPostId 신청한 게시물의 ID
Expand Down

0 comments on commit 7d92c9b

Please sign in to comment.