diff --git a/src/main/java/com/rollingpaperserver/domain/room/domain/Room.java b/src/main/java/com/rollingpaperserver/domain/room/domain/Room.java index 722ff57..7989405 100644 --- a/src/main/java/com/rollingpaperserver/domain/room/domain/Room.java +++ b/src/main/java/com/rollingpaperserver/domain/room/domain/Room.java @@ -24,7 +24,6 @@ public class Room { private String url; @OneToMany(mappedBy = "room") - @JsonManagedReference private List users = new ArrayList<>(); @Builder diff --git a/src/main/java/com/rollingpaperserver/domain/user/application/UserService.java b/src/main/java/com/rollingpaperserver/domain/user/application/UserService.java index d1bb13f..160db70 100644 --- a/src/main/java/com/rollingpaperserver/domain/user/application/UserService.java +++ b/src/main/java/com/rollingpaperserver/domain/user/application/UserService.java @@ -9,10 +9,7 @@ import com.rollingpaperserver.domain.user.domain.repository.UserRepository; import com.rollingpaperserver.domain.user.dto.UserDTO; import com.rollingpaperserver.domain.user.dto.request.CreateUserReq; -import com.rollingpaperserver.domain.user.dto.response.CreateUserRes; -import com.rollingpaperserver.domain.user.dto.response.ExclusionMeRes; -import com.rollingpaperserver.domain.user.dto.response.FindUserRes; -import com.rollingpaperserver.domain.user.dto.response.OutRoomRes; +import com.rollingpaperserver.domain.user.dto.response.*; import com.rollingpaperserver.domain.waitingRoom.domain.WaitingRoom; import com.rollingpaperserver.domain.waitingRoom.domain.repository.WaitingRoomRepository; import com.rollingpaperserver.domain.waitingRoom.dto.response.FindWaitingRoomRes; @@ -163,9 +160,10 @@ public ResponseEntity findUsersExclusionMe(Long userId, Long roomId) { return ResponseEntity.badRequest().body(findUserRes); } - Optional findUserWithRoomAndWaitingRoom = userRepository.findUserWithRoomAndWaitingRoom(userId); -// Optional findUserById = userRepository.findById(userId); - if (!findUserWithRoomAndWaitingRoom.isPresent()) { + Room room = roomById.get(); + + Optional userById = userRepository.findById(userId); + if (userById.isEmpty()) { FindUserRes findUserRes = FindUserRes.builder() .message("유저가 존재하지 않습니다.") .build(); @@ -173,18 +171,22 @@ public ResponseEntity findUsersExclusionMe(Long userId, Long roomId) { return ResponseEntity.badRequest().body(findUserRes); } - User user = findUserWithRoomAndWaitingRoom.get(); - - Room room = user.getRoom(); + User user = userById.get(); - List users = room.getUsers(); - List exclusiveMe = new ArrayList<>(); + List users = userRepository.findAllByRoom(room); + List exclusiveMe = new ArrayList<>(); for (User userInRoom : users) { if (userInRoom.getId() == userId) continue; - exclusiveMe.add(userInRoom); + UserRes userRes = UserRes.builder() + .id(userInRoom.getId()) + .userName(userInRoom.getUserName()) + .userType(userInRoom.getUserType()) + .build(); + + exclusiveMe.add(userRes); } ExclusionMeRes exclusionMeRes = ExclusionMeRes.builder() @@ -193,7 +195,6 @@ public ResponseEntity findUsersExclusionMe(Long userId, Long roomId) { .build(); return ResponseEntity.ok(exclusionMeRes); - } // Description : 방 나가기 / 마지막 유저가 방 나갈 시 방도 함께 삭제 diff --git a/src/main/java/com/rollingpaperserver/domain/user/domain/User.java b/src/main/java/com/rollingpaperserver/domain/user/domain/User.java index d291dbb..45d6d9e 100644 --- a/src/main/java/com/rollingpaperserver/domain/user/domain/User.java +++ b/src/main/java/com/rollingpaperserver/domain/user/domain/User.java @@ -29,7 +29,6 @@ public class User { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn - @JsonBackReference private Room room; @ManyToOne(fetch = FetchType.LAZY) diff --git a/src/main/java/com/rollingpaperserver/domain/user/domain/repository/UserRepository.java b/src/main/java/com/rollingpaperserver/domain/user/domain/repository/UserRepository.java index 1e833d8..a7e9419 100644 --- a/src/main/java/com/rollingpaperserver/domain/user/domain/repository/UserRepository.java +++ b/src/main/java/com/rollingpaperserver/domain/user/domain/repository/UserRepository.java @@ -1,5 +1,6 @@ package com.rollingpaperserver.domain.user.domain.repository; +import com.rollingpaperserver.domain.room.domain.Room; import com.rollingpaperserver.domain.user.domain.User; import com.rollingpaperserver.domain.user.dto.UserDTO; import com.rollingpaperserver.domain.waitingRoom.domain.WaitingRoom; @@ -17,6 +18,8 @@ public interface UserRepository extends JpaRepository { // DTO와 함께 사용 List findAllByWaitingRoom(WaitingRoom waitingRoom); + List findAllByRoom(Room room); + @Query("SELECT DISTINCT u FROM User u LEFT JOIN FETCH u.room LEFT JOIN FETCH u.waitingRoom") List findAllWithRoomAndWaitingRoom(); diff --git a/src/main/java/com/rollingpaperserver/domain/user/dto/response/ExclusionMeRes.java b/src/main/java/com/rollingpaperserver/domain/user/dto/response/ExclusionMeRes.java index f3d1009..16f0120 100644 --- a/src/main/java/com/rollingpaperserver/domain/user/dto/response/ExclusionMeRes.java +++ b/src/main/java/com/rollingpaperserver/domain/user/dto/response/ExclusionMeRes.java @@ -17,12 +17,12 @@ @Data public class ExclusionMeRes { - private List users = new ArrayList<>(); + private List users = new ArrayList<>(); private String message; @Builder - public ExclusionMeRes(List users, String message) { + public ExclusionMeRes(List users, String message) { this.users = users; this.message = message; } diff --git a/src/main/java/com/rollingpaperserver/domain/user/dto/response/UserRes.java b/src/main/java/com/rollingpaperserver/domain/user/dto/response/UserRes.java new file mode 100644 index 0000000..1e26167 --- /dev/null +++ b/src/main/java/com/rollingpaperserver/domain/user/dto/response/UserRes.java @@ -0,0 +1,25 @@ +package com.rollingpaperserver.domain.user.dto.response; + +import com.rollingpaperserver.domain.user.domain.UserType; +import lombok.AccessLevel; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@Data +public class UserRes { + + private Long id; + + private String userName; + + private UserType userType; + + @Builder + public UserRes(Long id, String userName, UserType userType) { + this.id = id; + this.userName = userName; + this.userType = userType; + } +}