Skip to content

Commit 9c941a7

Browse files
committed
Feat: 채팅방 삭제, join 기능 refactor
1 parent ad8768e commit 9c941a7

File tree

5 files changed

+88
-21
lines changed

5 files changed

+88
-21
lines changed

src/main/java/cmf/commitField/domain/chat/chatRoom/controller/ChatRoomController.java

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import cmf.commitField.domain.chat.chatRoom.dto.ChatRoomDto;
55
import cmf.commitField.domain.chat.chatRoom.service.ChatRoomService;
66
import cmf.commitField.domain.user.entity.CustomOAuth2User;
7+
import cmf.commitField.global.error.ErrorCode;
78
import cmf.commitField.global.globalDto.GlobalResponse;
89
import cmf.commitField.global.security.LoginCheck;
910
import jakarta.validation.Valid;
@@ -32,9 +33,9 @@ public GlobalResponse<Object> createRoom(
3233
CustomOAuth2User principal = (CustomOAuth2User) authentication.getPrincipal();
3334
Long userId = principal.getId(); // getId()를 통해 userId를 추출
3435
chatRoomService.createRoom(chatRoomRequest, userId); // userId를 전달
35-
return GlobalResponse.success();
36+
return GlobalResponse.success("채팅방을 생성하였습니다.");
3637
} else {
37-
throw new IllegalArgumentException("User not logged in.");
38+
throw new IllegalArgumentException("로그인 후에 이용해 주세요.");
3839
}
3940
}
4041

@@ -47,9 +48,9 @@ public GlobalResponse<Object> joinRoom(@PathVariable Long roomId) {
4748
CustomOAuth2User principal = (CustomOAuth2User) authentication.getPrincipal();
4849
Long userId = principal.getId(); // getId()를 통해 userId를 추출
4950
chatRoomService.joinRoom(roomId, userId); // userId를 전달
50-
return GlobalResponse.success();
51+
return GlobalResponse.success("해당 채팅방에 입장하셨습니다");
5152
} else {
52-
throw new IllegalArgumentException("User not logged in.");
53+
throw new IllegalArgumentException("로그인 후에 이용해 주세요.");
5354
}
5455
}
5556

@@ -58,7 +59,13 @@ public GlobalResponse<Object> joinRoom(@PathVariable Long roomId) {
5859
@LoginCheck
5960
public GlobalResponse<Object> roomList(Pageable pageable) {
6061
List<ChatRoomDto> roomList = chatRoomService.getRoomList(pageable);
61-
return GlobalResponse.success(roomList);
62+
63+
// 방 리스트가 비어 있으면 notFound 응답 반환
64+
if (roomList.isEmpty()) {
65+
return GlobalResponse.error(ErrorCode.NO_ROOM_FOUND);
66+
}
67+
68+
return GlobalResponse.success("전체 목록 조회에 성공하였습니다.",roomList);
6269
}
6370

6471
// 사용자(자신)가 생성한 방 리스트 조회
@@ -72,9 +79,15 @@ public GlobalResponse<Object> getByUserRoomList(Pageable pageable) {
7279
Long userId = principal.getId(); // getId()를 통해 userId를 추출
7380

7481
List<ChatRoomDto> userByRoomList = chatRoomService.getUserByRoomList(userId, pageable);
75-
return GlobalResponse.success(userByRoomList);
82+
83+
// 방 리스트가 비어 있으면 notFound 응답 반환
84+
if (userByRoomList.isEmpty()) {
85+
return GlobalResponse.error(ErrorCode.USER_CREATED_ROOM_NOT_FOUND);
86+
}
87+
88+
return GlobalResponse.success("사용자가 생성한 방 조회 성공.",userByRoomList);
7689
} else {
77-
throw new IllegalArgumentException("User not logged in.");
90+
throw new IllegalArgumentException("로그인 후에 이용해 주세요.");
7891
}
7992
}
8093

@@ -88,9 +101,15 @@ public GlobalResponse<Object> getByUserRoomPartList(Pageable pageable) {
88101
CustomOAuth2User principal = (CustomOAuth2User) authentication.getPrincipal();
89102
Long userId = principal.getId(); // getId()를 통해 userId를 추출
90103
List<ChatRoomDto> userByRoomPartList = chatRoomService.getUserByRoomPartList(userId, pageable);
91-
return GlobalResponse.success(userByRoomPartList);
104+
105+
// 만약 방 리스트가 없다면 notFound 응답 반환
106+
if (userByRoomPartList.isEmpty()) {
107+
return GlobalResponse.error(ErrorCode.NONE_ROOM);
108+
}
109+
110+
return GlobalResponse.success("사용자가 들어가 있는 방 리스트 조회 성공.",userByRoomPartList);
92111
} else {
93-
throw new IllegalArgumentException("User not logged in.");
112+
throw new IllegalArgumentException("로그인 후에 이용해 주세요.");
94113
}
95114
}
96115

@@ -105,9 +124,9 @@ public GlobalResponse<Object> outRoom(
105124
CustomOAuth2User principal = (CustomOAuth2User) authentication.getPrincipal();
106125
Long userId = principal.getId(); // getId()를 통해 userId를 추출
107126
chatRoomService.outRoom(userId, roomId);
108-
return GlobalResponse.success();
127+
return GlobalResponse.success("채팅방을 나갔습니다.");
109128
} else {
110-
throw new IllegalArgumentException("User not logged in.");
129+
throw new IllegalArgumentException("로그인 후에 이용해 주세요.");
111130
}
112131
}
113132

@@ -122,9 +141,9 @@ public GlobalResponse<Object> deleteRoom(
122141
CustomOAuth2User principal = (CustomOAuth2User) authentication.getPrincipal();
123142
Long userId = principal.getId(); // getId()를 통해 userId를 추출
124143
chatRoomService.deleteRoom(userId, roomId);
125-
return GlobalResponse.success();
144+
return GlobalResponse.success("채팅방을 삭제했습니다.");
126145
} else {
127-
throw new IllegalArgumentException("User not logged in.");
146+
throw new IllegalArgumentException("로그인 후에 이용해 주세요.");
128147
}
129148
}
130149

src/main/java/cmf/commitField/domain/chat/chatRoom/service/ChatRoomServiceImpl.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,17 @@ public void joinRoom(Long roomId, Long userId) {
7474

7575
// 현재 인원 수 조회
7676
Long currentUserCount = userChatRoomRepository.countByChatRoomId(roomId);
77-
7877
// 채팅방이 꽉 찼을 경우 예외 처리
7978
if (currentUserCount >= chatRoom.getUserCountMax()) {
8079
throw new CustomException(ErrorCode.ROOM_USER_FULL);
8180
}
8281

82+
// 이미 참여한 방인지 확인
83+
if (userChatRoomRepository.existsByChatRoomIdAndUserId(roomId, userId)) {
84+
throw new CustomException(ErrorCode.ALREADY_JOIN_ROOM);
85+
}
86+
87+
8388
// user_chatroom 관계 생성
8489
UserChatRoom userChatRoom = UserChatRoom.builder()
8590
.user(findUser)
@@ -137,13 +142,13 @@ public void outRoom(Long userId, Long roomId) {
137142
.findChatRoomByRoomCreator(roomId);
138143
// 방장이 아니라면
139144
if (!Objects.equals(roomCreatorId, userId)) {
140-
userChatRoomRepository.deleteUserChatRoomByUserId(userId);
145+
userChatRoomRepository.deleteUserChatRoomByChatRoom_IdAndUserId(roomId, userId);
141146
return;
142147
}
143148
// 방장이라면 방 삭제
144149
userChatRoomRepository.deleteUserChatRoomByChatRoom_Id(roomId);
145150
chatRoomRepository.deleteById(roomId);
146-
// chatMsgRepository.deleteById(roomId); 방 삭제 시 채팅도 다 삭제
151+
// chatMsgRepository.deleteById(roomId); 방 삭제 시 채팅도 다 삭제(필요 시)
147152
}
148153

149154

@@ -152,11 +157,14 @@ public void outRoom(Long userId, Long roomId) {
152157
public void deleteRoom(Long userId, Long roomId) {
153158
Long roomCreatorId = chatRoomRepository
154159
.findChatRoomByRoomCreator(roomId);
160+
//방장이 아닐 경우, 삭제 불가
155161
if (!Objects.equals(roomCreatorId, userId)) {
156162
throw new CustomException(ErrorCode.NOT_ROOM_CREATOR);
157163
}
164+
//모든 사용자 제거 후 방 삭제
158165
userChatRoomRepository.deleteUserChatRoomByChatRoom_Id(roomId);
159166
chatRoomRepository.deleteById(roomId);
167+
// chatMessageRepository.deleteId(roomId);
160168
}
161169

162170

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package cmf.commitField.domain.chat.userChatRoom.dto;
2+
3+
import lombok.*;
4+
5+
@Getter
6+
@Setter
7+
@NoArgsConstructor
8+
@AllArgsConstructor
9+
@Builder
10+
public class UserChatRoomDto {
11+
private Long userId;
12+
}

src/main/java/cmf/commitField/domain/chat/userChatRoom/repository/UserChatRoomRepository.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import org.springframework.data.repository.query.Param;
1111
import org.springframework.stereotype.Repository;
1212

13+
import java.util.List;
14+
1315
@Repository
1416
public interface UserChatRoomRepository extends JpaRepository<UserChatRoom, Long> {
1517

@@ -18,11 +20,19 @@ public interface UserChatRoomRepository extends JpaRepository<UserChatRoom, Long
1820
Long countByChatRoomId(Long roomId); // 비관적 락
1921

2022
// @Query("select count(*) from UserChatRoom u where u.chatRoom.id = ?1 ")
21-
// Long countNonLockByChatRoomId(Long roomId); // test 용도
2223
@Query("select count(*) from UserChatRoom u where u.chatRoom.id = :roomId")
2324
Long countNonLockByChatRoomId(@Param("roomId")Long roomId); // test 용도
24-
25+
// 특정 방의 모든 유저 관계 삭제
2526
void deleteUserChatRoomByChatRoom_Id(Long chatRoomId);
26-
27+
// 특정 방에서 특정 사용자만 삭제
28+
void deleteUserChatRoomByChatRoom_IdAndUserId(Long chatRoomId, Long userId);
29+
// 특정 방과 사용자 관계 삭제
2730
void deleteUserChatRoomByUserId(Long userId);
31+
// 사용자가 해당 방에 참여한 여부 확인
32+
boolean existsByChatRoomIdAndUserId(Long roomId, Long userId);
33+
// 특정 방에 참여한 모든 UserChatRoom 관계 조회
34+
List<UserChatRoom> findByChatRoom_Id(Long chatRoomId);
35+
36+
37+
2838
}

src/main/java/cmf/commitField/global/globalDto/GlobalResponse.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,39 @@ private GlobalResponse(ErrorCode errorCode, T data) {
2929
this.data = data;
3030
}
3131

32+
// 메시지와 데이터를 받는 생성자
33+
private GlobalResponse(int statusCode, String message, T data) {
34+
this.timestamp = LocalDateTime.now();
35+
this.statusCode = statusCode;
36+
this.message = message;
37+
this.data = data;
38+
}
39+
40+
// 성공 응답 (데이터 포함)
41+
public static <T> GlobalResponse<T> success(String message, T data) {
42+
return new GlobalResponse<>(GlobalResponseCode.OK.getCode(), message, data);
43+
}
44+
3245
// 성공 응답 (데이터 포함)
3346
public static <T> GlobalResponse<T> success(T data) {
3447
return new GlobalResponse<>(GlobalResponseCode.OK, data);
3548
}
3649

3750
// 성공 응답 (데이터 없음)
38-
public static <T> GlobalResponse<T> success() {
39-
return success(null);
51+
public static <T> GlobalResponse<T> success(String message) {
52+
return success(message, null);
4053
}
4154

4255
// 에러 응답 (데이터 포함)
4356
public static <T> GlobalResponse<T> error(ErrorCode errorCode, T data) {
4457
return new GlobalResponse<>(errorCode, data);
4558
}
4659

60+
// 성공 응답 (기본 메세지)
61+
public static <T> GlobalResponse<T> success() {
62+
return success(null);
63+
}
64+
4765
// 에러 응답 (데이터 없음)
4866
public static <T> GlobalResponse<T> error(ErrorCode errorCode) {
4967
return error(errorCode, null);

0 commit comments

Comments
 (0)