Skip to content

Commit

Permalink
fix: 본인 제외 유저 목록 조회 API 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
phonil committed Dec 17, 2023
1 parent 5f8ce3e commit e439743
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public class Room {
private String url;

@OneToMany(mappedBy = "room")
@JsonManagedReference
private List<User> users = new ArrayList<>();

@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -163,28 +160,33 @@ public ResponseEntity<?> findUsersExclusionMe(Long userId, Long roomId) {
return ResponseEntity.badRequest().body(findUserRes);
}

Optional<User> findUserWithRoomAndWaitingRoom = userRepository.findUserWithRoomAndWaitingRoom(userId);
// Optional<User> findUserById = userRepository.findById(userId);
if (!findUserWithRoomAndWaitingRoom.isPresent()) {
Room room = roomById.get();

Optional<User> userById = userRepository.findById(userId);
if (userById.isEmpty()) {
FindUserRes findUserRes = FindUserRes.builder()
.message("유저가 존재하지 않습니다.")
.build();

return ResponseEntity.badRequest().body(findUserRes);
}

User user = findUserWithRoomAndWaitingRoom.get();

Room room = user.getRoom();
User user = userById.get();

List<User> users = room.getUsers();
List<User> exclusiveMe = new ArrayList<>();
List<User> users = userRepository.findAllByRoom(room);
List<UserRes> 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()
Expand All @@ -193,7 +195,6 @@ public ResponseEntity<?> findUsersExclusionMe(Long userId, Long roomId) {
.build();

return ResponseEntity.ok(exclusionMeRes);

}

// Description : 방 나가기 / 마지막 유저가 방 나갈 시 방도 함께 삭제
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public class User {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn
@JsonBackReference
private Room room;

@ManyToOne(fetch = FetchType.LAZY)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,6 +18,8 @@ public interface UserRepository extends JpaRepository<User, Long> {
// DTO와 함께 사용
List<User> findAllByWaitingRoom(WaitingRoom waitingRoom);

List<User> findAllByRoom(Room room);

@Query("SELECT DISTINCT u FROM User u LEFT JOIN FETCH u.room LEFT JOIN FETCH u.waitingRoom")
List<User> findAllWithRoomAndWaitingRoom();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
@Data
public class ExclusionMeRes {

private List<User> users = new ArrayList<>();
private List<UserRes> users = new ArrayList<>();

private String message;

@Builder
public ExclusionMeRes(List<User> users, String message) {
public ExclusionMeRes(List<UserRes> users, String message) {
this.users = users;
this.message = message;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit e439743

Please sign in to comment.