Skip to content

Commit df9a533

Browse files
committed
Feat: 사용자가 참여한 방 나가기
1 parent 5c77469 commit df9a533

File tree

4 files changed

+50
-47
lines changed

4 files changed

+50
-47
lines changed

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

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import jakarta.validation.Valid;
1010
import lombok.RequiredArgsConstructor;
1111
import org.springframework.data.domain.Pageable;
12-
import org.springframework.http.ResponseEntity;
1312
import org.springframework.security.core.Authentication;
1413
import org.springframework.security.core.context.SecurityContextHolder;
1514
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
@@ -57,23 +56,23 @@ public GlobalResponse<Object> joinRoom(@PathVariable Long roomId) {
5756
// 전체 리스트
5857
@GetMapping("/room")
5958
@LoginCheck
60-
public ResponseEntity<Object> roomList(Pageable pageable) {
59+
public GlobalResponse<Object> roomList(Pageable pageable) {
6160
List<ChatRoomDto> roomList = chatRoomService.getRoomList(pageable);
62-
return ResponseEntity.ok().body(roomList);
61+
return GlobalResponse.success(roomList);
6362
}
6463

6564
// 사용자(자신)가 생성한 방 리스트 조회
6665
@GetMapping("/room/creator")
6766
@LoginCheck
68-
public ResponseEntity<Object> getByUserRoomList(Pageable pageable) {
67+
public GlobalResponse<Object> getByUserRoomList(Pageable pageable) {
6968
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
7069

7170
if (authentication instanceof OAuth2AuthenticationToken) {
7271
CustomOAuth2User principal = (CustomOAuth2User) authentication.getPrincipal();
7372
Long userId = principal.getId(); // getId()를 통해 userId를 추출
7473

7574
List<ChatRoomDto> userByRoomList = chatRoomService.getUserByRoomList(userId, pageable);
76-
return ResponseEntity.ok().body(userByRoomList);
75+
return GlobalResponse.success(userByRoomList);
7776
} else {
7877
throw new IllegalArgumentException("User not logged in.");
7978
}
@@ -82,35 +81,35 @@ public ResponseEntity<Object> getByUserRoomList(Pageable pageable) {
8281
// 사용자(자신)가 들어가 있는 방 리스트 조회
8382
@GetMapping("/room/part")
8483
@LoginCheck
85-
public ResponseEntity<Object> getByUserRoomPartList(Pageable pageable) {
84+
public GlobalResponse<Object> getByUserRoomPartList(Pageable pageable) {
8685
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
8786

8887
if (authentication instanceof OAuth2AuthenticationToken) {
8988
CustomOAuth2User principal = (CustomOAuth2User) authentication.getPrincipal();
9089
Long userId = principal.getId(); // getId()를 통해 userId를 추출
9190
List<ChatRoomDto> userByRoomPartList = chatRoomService.getUserByRoomPartList(userId, pageable);
92-
return ResponseEntity.ok().body(userByRoomPartList);
91+
return GlobalResponse.success(userByRoomPartList);
9392
} else {
9493
throw new IllegalArgumentException("User not logged in.");
9594
}
9695
}
9796

98-
// // 채팅방 나가기
99-
// @DeleteMapping("/room/out/{roomId}")
100-
// @LoginCheck
101-
// public ResponseEntity<Object> outRoom(
102-
// @PathVariable Long roomId) {
103-
// Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
104-
//
105-
// if (authentication instanceof OAuth2AuthenticationToken) {
106-
// CustomOAuth2User principal = (CustomOAuth2User) authentication.getPrincipal();
107-
// Long userId = principal.getId(); // getId()를 통해 userId를 추출
108-
// chatRoomService.outRoom(userId, roomId);
109-
// return ResponseEntity.ok().body("success");
110-
// } else {
111-
// throw new IllegalArgumentException("User not logged in.");
112-
// }
113-
// }
97+
// 채팅방 나가기
98+
@DeleteMapping("/room/out/{roomId}")
99+
@LoginCheck
100+
public GlobalResponse<Object> outRoom(
101+
@PathVariable Long roomId) {
102+
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
103+
104+
if (authentication instanceof OAuth2AuthenticationToken) {
105+
CustomOAuth2User principal = (CustomOAuth2User) authentication.getPrincipal();
106+
Long userId = principal.getId(); // getId()를 통해 userId를 추출
107+
chatRoomService.outRoom(userId, roomId);
108+
return GlobalResponse.success();
109+
} else {
110+
throw new IllegalArgumentException("User not logged in.");
111+
}
112+
}
114113
//
115114
// // 채팅방 삭제
116115
// @DeleteMapping("/room/delete/{roomId}")

src/main/java/cmf/commitField/domain/chat/chatRoom/repository/ChatRoomRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ public interface ChatRoomRepository extends JpaRepository<ChatRoom, Long> {
2626

2727
Page<ChatRoom> findAllByUserChatRoomsUserId(Long userId,Pageable pageable);
2828

29-
// @Query(value = "SELECT ROOM_CREATOR FROM chat_room WHERE CHAT_ROOM_ID = ?", nativeQuery = true)
30-
// Long findChatRoomByRoomCreator(Long roomId);
29+
@Query(value = "SELECT ROOM_CREATOR FROM chat_room WHERE ID = ?", nativeQuery = true)
30+
Long findChatRoomByRoomCreator(Long roomId);
3131
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ public interface ChatRoomService {
1616
List<ChatRoomDto> getRoomList(Pageable pageable);
1717

1818
// 자신이 생성한 방 리스트 조회
19-
List<ChatRoomDto> getUserByRoomList(Long userId, org.springframework.data.domain.Pageable pageable);
19+
List<ChatRoomDto> getUserByRoomList(Long userId, Pageable pageable);
2020

2121
List<ChatRoomDto> getUserByRoomPartList(Long userId, Pageable pageable);
22-
//
23-
// void outRoom(Long userId, Long roomId);
22+
23+
void outRoom(Long userId, Long roomId);
2424
//
2525
// void deleteRoom(Long userId, Long roomId);
2626
}

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

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.time.LocalDateTime;
2121
import java.util.ArrayList;
2222
import java.util.List;
23+
import java.util.Objects;
2324

2425
@Service
2526
@RequiredArgsConstructor
@@ -37,6 +38,11 @@ public void createRoom(ChatRoomRequest chatRoomRequest, Long userId) {
3738
User findUser = userRepository.findById(userId)
3839
.orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_USER));
3940

41+
// findUser가 null이 아닐 경우, User의 ID를 사용
42+
if (findUser == null) {
43+
throw new CustomException(ErrorCode.NOT_FOUND_USER); // 예외 처리 추가
44+
}
45+
4046
// ChatRoom 생성
4147
ChatRoom chatRoom = ChatRoom.builder()
4248
.roomCreator(findUser.getId())
@@ -82,9 +88,6 @@ public void joinRoom(Long roomId, Long userId) {
8288
userChatRoomRepository.save(userChatRoom);
8389
}
8490

85-
//// chatMsgRepository.deleteById(roomId); 방 삭제 시 채팅도 다 삭제 되어야 함.
86-
// }
87-
//
8891
// 방 조회 DTO 변환 메서드 추출
8992
private static List<ChatRoomDto> getChatRoomDtos(Page<ChatRoom> all) {
9093
List<ChatRoomDto> chatRoomList = new ArrayList<>();
@@ -127,21 +130,22 @@ public List<ChatRoomDto> getUserByRoomPartList(Long userId, Pageable pageable) {
127130
return getChatRoomDtos(allByUserIdAndUserChatRooms);
128131
}
129132

130-
// @Override
131-
// @Transactional
132-
// public void outRoom(Long userId, Long roomId) {
133-
// Long roomCreatorId = chatRoomRepository
134-
// .findChatRoomByRoomCreator(roomId);
135-
// // 방장이 아니라면
136-
// if (!Objects.equals(roomCreatorId, userId)) {
137-
// userChatRoomRepository.deleteUserChatRoomByUserId(userId);
138-
// return;
139-
// }
140-
// // 방장이라면 방 삭제
141-
// userChatRoomRepository.deleteUserChatRoomByChatRoom_Id(roomId);
142-
// chatRoomRepository.deleteById(roomId);
143-
// }
144-
//
133+
@Override
134+
@Transactional(readOnly = true)
135+
public void outRoom(Long userId, Long roomId) {
136+
Long roomCreatorId = chatRoomRepository
137+
.findChatRoomByRoomCreator(roomId);
138+
// 방장이 아니라면
139+
if (!Objects.equals(roomCreatorId, userId)) {
140+
userChatRoomRepository.deleteUserChatRoomByUserId(userId);
141+
return;
142+
}
143+
// 방장이라면 방 삭제
144+
userChatRoomRepository.deleteUserChatRoomByChatRoom_Id(roomId);
145+
chatRoomRepository.deleteById(roomId);
146+
}
147+
148+
//// chatMsgRepository.deleteById(roomId); 방 삭제 시 채팅도 다 삭제 되어야 함.
145149
// @Override
146150
// @Transactional
147151
// public void deleteRoom(Long userId, Long roomId) {
@@ -153,7 +157,7 @@ public List<ChatRoomDto> getUserByRoomPartList(Long userId, Pageable pageable) {
153157
// userChatRoomRepository.deleteUserChatRoomByChatRoom_Id(roomId);
154158
// chatRoomRepository.deleteById(roomId);
155159
// 채팅 메시지 구현 시, 방 삭제할 때 메시지도 같이 삭제되는 메서드 구현
156-
160+
//}
157161

158162

159163

0 commit comments

Comments
 (0)