[ 작업한 내용 ]
< 관리자: 회원 전체 목록 조회 >
# AdminUserController
- Page<AdminUserListResDTO> 타입으로 결과를 받았던 것을 PageResponse<AdminUserListResDTO>로 변경.
@GetMapping()
public ResponseEntity<?> allListUsers(
@RequestParam(name = "page", defaultValue = "0") int page,
@RequestParam(name = "size", defaultValue = "10") int size,
@AuthenticationPrincipal User user) {
// 서비스로직
PageResponse<AdminUserListResDTO> responseDTO = adminUserService.allListUsers(page, size, user.getId());
// 성공 메시지
String message = messageProvider.getMessage(UserSuccessCode.USER_LIST_FETCHED.getMessage());
// 응답
return ResponseEntity
.status(UserSuccessCode.USER_LIST_FETCHED.getHttpStatus())
.body(ApiResponse.success(
UserSuccessCode.USER_LIST_FETCHED,
message,
responseDTO));
}
# AdminUserService
- allListUsers 메서드의 타입을 PageResponse<AdminUserListResDTO>로 변경.
- Controller 측으로 결과를 반환시 PageResponse<>로 객체를 생성하여 반환.
public PageResponse<AdminUserListResDTO> allListUsers(int page, int size, Long userId) {
// 관리자 권환 조회 및 예외 처리
User user = userRepository.findById(userId)
.orElseThrow(() -> new BaseException(UserErrorCode.USER_NOT_FOUND));
if (user.getRole() != Role.ROLE_ADMIN) {
throw new BaseException(AuthErrorCode.AUTH_FORBIDDEN);
}
// 페이징 정의
Pageable pageable = PageRequest.of(page, size);
// Page형태로 사용자 조회
Page<User> result = userRepository.findAll(pageable);
// User -> AdminUserListResDTO로 맵핑
Page<AdminUserListResDTO> pageDTO = result.map(AdminUserListResDTO::new);
// 반환
return new PageResponse<>(pageDTO);
}
< 관리자: 회원 검색 >
# AdminUserController
- Page<AdminUserSearchResDTO> 를 PageResponse<AdminUserSearchResDTO>로 변경
@GetMapping("search")
public ResponseEntity<?> searchUser(
@RequestParam("type") UserSearchType type,
@RequestParam("keyword") String keyword,
@RequestParam(value = "page", defaultValue = "0") int page,
@RequestParam(value = "size", defaultValue = "10") int size,
@AuthenticationPrincipal User user) {
// 서비스로직
PageResponse<AdminUserSearchResDTO> responseDTO = adminUserService.searchUser(type, keyword, page, size, user.getId());
// 성공메시지
String message = messageProvider.getMessage(UserSuccessCode.USER_FETCHED.getMessage());
// 응답
return ResponseEntity
.status(UserSuccessCode.USER_ACCOUNT_DELETED.getHttpStatus())
.body(ApiResponse.success(
UserSuccessCode.USER_LIST_FETCHED,
message,
responseDTO));
}
# AdminUserService
- searchUser 메서드의 타입을 PageResponse<AdminUserSearchResDTO>로 변경.
- Controller로 반환할 때 PageResponse<> 타입의 객체로 반환.
public PageResponse<AdminUserSearchResDTO> searchUser(UserSearchType type, String keyword, int page, int size, Long userId) {
// 관리자 권한 조회 및 예외 처리
User user = userRepository.findById(userId)
.orElseThrow(() -> new BaseException(UserErrorCode.USER_NOT_FOUND));
if (user.getRole() != Role.ROLE_ADMIN) {
throw new BaseException(AuthErrorCode.AUTH_FORBIDDEN);
}
// 페이징 정의
Pageable pageable = PageRequest.of(page, size);
// 타입별 Page형태로 사용자 검색 조회
Page<User> result;
switch(type) {
case USERNAME:
result = userRepository.findByUsernameContainingIgnoreCase(keyword, pageable);
break;
case EMAIL:
result = userRepository.findByEmailContainingIgnoreCase(keyword, pageable);
break;
default:
throw new IllegalArgumentException("올바른 타입을 선택해주세요 (USERNAME, EMAIL): " + type);
}
// User -> AdminUserSearchResDTO로 맵핑
Page<AdminUserSearchResDTO> pageDTO = result.map(AdminUserSearchResDTO::new);
// 반환
return new PageResponse<>(pageDTO);
}
< 관리자: 회원 상세 조회 >
# AdminUserDetailResDTO
- 회원 상세 조회 응답 DTO
- 사용자 고유번호, 아이디, 이메일, 권한, 가입일, 마지막 로그인, 수정일, 비활성화 날짜, 삭제 날짜, 상태 (id, username, email, role, joinDate, lastLoginDate, updatedAt, inactiveAt, deletedAt, status)
@Getter
public class AdminUserDetailResDTO {
private Long id;
private String username;
private String email;
private Role role;
private LocalDateTime joinDate;
private LocalDateTime lastLoginDate;
private LocalDateTime updatedAt;
private LocalDateTime inactiveAt;
private LocalDateTime deletedAt;
private UserStatus status;
public AdminUserDetailResDTO(User user) {
this.id = user.getId();
this.username = user.getUsername();
this.email = user.getEmail();
this.role = user.getRole();
this.joinDate = user.getJoinDate();
this.lastLoginDate = user.getLastLoginDate();
this.updatedAt = user.getUpdatedAt();
this.inactiveAt = user.getInactiveAt();
this.deletedAt = user.getDeletedAt();
this.status = user.getStatus();
}
}
# AdminUserController
- GET /api/v1/admin/users/{userId} 로 요청.
- 파라미터로 userId, @AuthenticationPrincipal User user 전달받음.
- Service 계층으로 userId, user.getId()를 전달함.
- 결과는 AdminUserDetailResDTO 형태로 저장.
- 클라이언트 측으로 성공메시지와 결과를 ApiResponse.success()로 감싸서 응답.
@GetMapping("/{userId}")
public ResponseEntity<?> detailUser(
@PathVariable("userId") Long userId,
@AuthenticationPrincipal User user) {
// 서비스로직
AdminUserDetailResDTO responseDTO = adminUserService.detailUser(userId, user.getId());
// 성공메시지
String message = messageProvider.getMessage(UserSuccessCode.USER_FETCHED.getMessage());
// 응답
return ResponseEntity
.status(UserSuccessCode.USER_FETCHED.getHttpStatus())
.body(ApiResponse.success(
UserSuccessCode.USER_FETCHED,
message,
responseDTO));
}
# AdminUserService
- userRepository.findById(adminUserId)로 관리자 조회가 되면 User userAdmin으로 저장, 조회가 안된다면 UserErrorCode.USER_NOT_FOUND 예외를 발생시킨다.
- 조회된 관리자의 권한 체크를 하고 관리자 권한이 아니라면 AuthErrorCode.AUTH_FORBIDDEN 예외를 발생 시킨다.
- userRepository.findById(userId)를 사용하여 사용자를 조회하고 조회가 되면 User user로 저장. 조회가 안된다면 UserErrorCode.USER_NOT_FOUND 예외를 발생시킨다.
- Controller로 반환 시 user기반으로 AdminUserDetailResDTO 객체를 생성하여 반환한다.
public AdminUserDetailResDTO detailUser(Long userId, Long adminUserId) {
// 관리자 권한 조회 및 예외 처리
User userAdmin = userRepository.findById(adminUserId)
.orElseThrow(() -> new BaseException(UserErrorCode.USER_NOT_FOUND));
if (userAdmin.getRole() != Role.ROLE_ADMIN) {
throw new BaseException(AuthErrorCode.AUTH_FORBIDDEN);
}
// 사용자 정보 확인 및 예외 처리
User user = userRepository.findById(userId)
.orElseThrow(() -> new BaseException(UserErrorCode.USER_NOT_FOUND, "userId: " + userId));
// 반환
return new AdminUserDetailResDTO(user);
}