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

[SIX-274] 채팅방이 중복되어서 생성되지 않도록 하고, 인덱스를 추가하였습니다. #127

Merged
merged 3 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
public class ChatRoomService {

private static final int MAX_CAPACITY = 2;
private final MissionChatRoomValidator missionChatRoomValidator;
private final MissionChatRoomReader missionChatRoomReader;
private final MissionChatRoomRepository missionChatRoomRepository;
private final UserMissionChatRoomRepository userMissionChatRoomRepository;
Expand All @@ -42,7 +43,9 @@ public class ChatRoomService {
public MissionChatRoomCreateResponse createChatRoom(
CreateMissionChatRoomServiceRequest request
) {
missionChatRoomValidator.duplicateMissionChatRoom(request.userIds(), request.missionId());
var missionChatRoom = MissionChatRoom.createMissionChatRoom(request.missionId(), request.userIds());

var savedMissionChatRoom = missionChatRoomRepository.save(missionChatRoom);
missionChatRoomRedisRepository.create(MissionChatRoomRedisRequest.from(missionChatRoom));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.sixheroes.onedayheroapplication.chatroom;

import com.sixheroes.onedayherocommon.error.ErrorCode;
import com.sixheroes.onedayherocommon.exception.BusinessException;
import com.sixheroes.onedayherodomain.missionchatroom.repository.UserMissionChatRoomRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Component
public class MissionChatRoomValidator {

private final UserMissionChatRoomRepository userMissionChatRoomRepository;

public void duplicateMissionChatRoom(
List<Long> userIds,
Long missionId
) {
if (!userMissionChatRoomRepository.findByUserIdInAndMissionChatRoom_MissionId(userIds, missionId).isEmpty()) {
log.warn("이미 존재하는 채팅방입니다. userId : {}, missionId : {}", userIds, missionId);
throw new BusinessException(ErrorCode.DUPLICATE_MISSION_CHATROOM);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.sixheroes.onedayheroapplication.IntegrationApplicationTest;
import com.sixheroes.onedayheroapplication.chatroom.request.CreateMissionChatRoomServiceRequest;
import com.sixheroes.onedayherochat.application.repository.request.MissionChatRoomRedisRequest;
import com.sixheroes.onedayherocommon.exception.BusinessException;
import com.sixheroes.onedayherodomain.mission.Mission;
import com.sixheroes.onedayherodomain.mission.MissionInfo;
import com.sixheroes.onedayherodomain.mission.MissionStatus;
Expand All @@ -19,8 +20,7 @@
import java.util.List;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.any;
Expand Down Expand Up @@ -53,6 +53,31 @@ void createChatRoom() {
assertThat(chatRoom.missionId()).isEqualTo(missionId);
}

@DisplayName("유저는 동일한 채팅방을 생성 할 수 없다.")
@Test
void createChatRoomWithDuplicate() {
// given
var missionId = 1L;
var userIds = List.of(1L, 2L);

var request = CreateMissionChatRoomServiceRequest.builder()
.missionId(missionId)
.userIds(userIds)
.build();

var missionChatRoom = MissionChatRoom.createMissionChatRoom(request.missionId(), request.userIds());
var redisRequest = MissionChatRoomRedisRequest.from(missionChatRoom);

given(missionChatRoomRedisRepository.create(any(MissionChatRoomRedisRequest.class)))
.willReturn(redisRequest);

chatRoomService.createChatRoom(request);

// when & then
assertThatThrownBy(() -> chatRoomService.createChatRoom(request))
.isInstanceOf(BusinessException.class);
}

@DisplayName("유저가 둘 다 나가있지 않은 상태에서 채팅방을 나갈 수 있다.")
@Test
void exitChatRoomWithMaxConnection() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ public enum ErrorCode {
ABORT_CHATROOM_EXIT("MCR_001", "채팅방에 입장한 상태에서만 채팅방 나가기가 가능합니다."),
INVALID_CREATE_CHAT("MCR_002", "채팅방을 생성하기 위한 적절한 인원이 들어오지 않았습니다."),
NOT_FOUND_CHAT_TOPIC("MCR_003", "저장되어있는 레디스 채팅 토픽을 찾지 못했습니다."),

// USER_MISSION_CHATROOM
DUPLICATE_MISSION_CHATROOM("MCR_004", "이미 생성되어 있는 채팅방입니다."),

// CHAT
NOT_FOUND_CHAT_TYPE("CC_000", "존재하지 않는 채팅타입입니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
@Slf4j
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "user_mission_chat_rooms")
@Table(name = "user_mission_chat_rooms",
uniqueConstraints = {
@UniqueConstraint(columnNames = {"mission_chat_room_id", "user_id"})
})
@Entity
public class UserMissionChatRoom extends BaseEntity {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ public interface UserMissionChatRoomRepository extends JpaRepository<UserMission
List<UserChatRoomQueryResponse> findReceiverChatRoomInfoInChatRoomIdsAndUserId(List<Long> chatRoomIds, Long userId);

List<UserMissionChatRoom> findByUserIdAndIsJoinedTrue(Long userId);

List<UserMissionChatRoom> findByUserIdInAndMissionChatRoom_MissionId(List<Long> userIds, Long missionId);
}
Loading