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 838d1d7..3d90d48 100644 --- a/src/main/java/com/rollingpaperserver/domain/user/application/UserService.java +++ b/src/main/java/com/rollingpaperserver/domain/user/application/UserService.java @@ -21,6 +21,7 @@ import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; import java.util.Optional; @@ -185,22 +186,31 @@ public ResponseEntity findUsersExclusionMe(Long userId, String roomUrl) { User user = userById.get(); - List users = userRepository.findAllByRoom(room); + List allUsersInRoom = userRepository.findAllByRoom(room); List exclusiveMe = new ArrayList<>(); - for (User userInRoom : users) { + for (User userInRoom : allUsersInRoom) { if (userInRoom.getId() == userId) continue; + // userId를 기준으로 뒤쪽으로 회전시키기 위한 offset 계산 + int offset = (int) (userInRoom.getId() - userId); + + // 겹치지 않게 sequence를 조합 + int newSequence = (offset > 0) ? offset : allUsersInRoom.size() + offset; + UserRes userRes = UserRes.builder() .id(userInRoom.getId()) .userName(userInRoom.getUserName()) .userType(userInRoom.getUserType()) + .sequence(newSequence) .build(); exclusiveMe.add(userRes); } + exclusiveMe.sort(Comparator.comparing(UserRes::getSequence)); + ExclusionMeRes exclusionMeRes = ExclusionMeRes.builder() .users(exclusiveMe) .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 index 1e26167..9c7b956 100644 --- a/src/main/java/com/rollingpaperserver/domain/user/dto/response/UserRes.java +++ b/src/main/java/com/rollingpaperserver/domain/user/dto/response/UserRes.java @@ -16,10 +16,13 @@ public class UserRes { private UserType userType; + private Integer sequence; + @Builder - public UserRes(Long id, String userName, UserType userType) { + public UserRes(Long id, String userName, UserType userType, Integer sequence) { this.id = id; this.userName = userName; this.userType = userType; + this.sequence = sequence; } }