Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

회원정보 검색에서 페이징 처리 결과값 깔끔하게 수정 #414

Merged
merged 1 commit into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.green.acamatch.config.exception;

import lombok.Getter;
import org.springframework.data.domain.Page;

import java.util.List;

@Getter
public class CustomPageResponse<T> {
private final List<T> content;
private final int pageNumber;
private final int pageSize;
private final int totalPages;
private final long totalElements;
private final boolean isFirst;
private final boolean isLast;

public CustomPageResponse(Page<T> page) {
this.content = page.getContent();
this.pageNumber = page.getNumber();
this.pageSize = page.getSize();
this.totalPages = page.getTotalPages();
this.totalElements = page.getTotalElements();
this.isFirst = page.isFirst();
this.isLast = page.isLast();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.green.acamatch.user.contoller;

import com.green.acamatch.config.exception.CustomPageResponse;
import com.green.acamatch.config.model.ResultResponse;
import com.green.acamatch.entity.myenum.UserRole;
import com.green.acamatch.user.UserUtils;
Expand All @@ -14,6 +15,9 @@
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

Expand Down Expand Up @@ -177,32 +181,66 @@ public ResultResponse<List<UserReportProjection>> getAllUsersInfo() {
.build();
}

// @GetMapping("/search")
// @Operation(summary = "사용자 검색", description = "userId, 이름, 역할로 검색 가능")
// public ResultResponse<Page<UserReportProjection>> searchUsers(
// @RequestParam(required = false) Long userId,
// @RequestParam(required = false) String name,
// @RequestParam(required = false) String nickName,
// @RequestParam(required = false) UserRole userRole,
// @RequestParam(defaultValue = "0") int page,
// @RequestParam(defaultValue = "10") int size ) {
//
// // page가 1 이상일 경우, Spring의 0-based index에 맞추기 위해 -1 적용 가능
// int adjustedPage = (page > 0) ? page - 1 : 0;
//
// Page<UserReportProjection> users = userManagementService.searchUsers(userId, name, nickName, userRole, adjustedPage, size);
//
// if (users.isEmpty()) {
// return ResultResponse.<Page<UserReportProjection>>builder()
// .resultMessage("해당 조건에 맞는 사용자가 없습니다.")
// .resultData(null)
// .build();
// }
//
// return ResultResponse.<Page<UserReportProjection>>builder()
// .resultMessage("사용자 조회 성공")
// .resultData(users)
// .build();
// }

@GetMapping("/search")
@Operation(summary = "사용자 검색", description = "userId, 이름, 역할로 검색 가능")
public ResultResponse<Page<UserReportProjection>> searchUsers(
public ResultResponse<CustomPageResponse<UserReportProjection>> searchUsers(
@RequestParam(required = false) Long userId,
@RequestParam(required = false) String name,
@RequestParam(required = false) String nickName,
@RequestParam(required = false) UserRole userRole,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size ) {
@RequestParam(defaultValue = "1") int page, // 기본값 1로 설정
@RequestParam(defaultValue = "10") int size // 기본값 10으로 설정
) {
// Spring Data JPA의 페이징은 0부터 시작하므로 page를 -1 조정
int adjustedPage = Math.max(page - 1, 0);

// page가 1 이상일 경우, Spring의 0-based index에 맞추기 위해 -1 적용 가능
int adjustedPage = (page > 0) ? page - 1 : 0;
// Pageable 객체 생성 (size가 0이면 페이징 없이 전체 조회)
Pageable pageable = (size <= 0) ? Pageable.unpaged() : PageRequest.of(adjustedPage, size, Sort.by("createdAt").descending());

Page<UserReportProjection> users = userManagementService.searchUsers(userId, name, nickName, userRole, adjustedPage, size);
// pageable을 searchUsers() 메서드에 전달
CustomPageResponse<UserReportProjection> users = userManagementService.searchUsers(userId, name, nickName, userRole, pageable);

if (users.isEmpty()) {
return ResultResponse.<Page<UserReportProjection>>builder()
// 조회된 데이터가 없을 경우 응답 처리
if (users.getContent().isEmpty()) {
return ResultResponse.<CustomPageResponse<UserReportProjection>>builder()
.resultMessage("해당 조건에 맞는 사용자가 없습니다.")
.resultData(null)
.build();
}

return ResultResponse.<Page<UserReportProjection>>builder()
return ResultResponse.<CustomPageResponse<UserReportProjection>>builder()
.resultMessage("사용자 조회 성공")
.resultData(users)
.build();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.green.acamatch.config.constant.UserConst;
import com.green.acamatch.config.exception.CommonErrorCode;
import com.green.acamatch.config.exception.CustomException;
import com.green.acamatch.config.exception.CustomPageResponse;
import com.green.acamatch.config.exception.UserErrorCode;
import com.green.acamatch.config.security.AuthenticationFacade;
import com.green.acamatch.entity.myenum.UserRole;
Expand Down Expand Up @@ -127,9 +128,19 @@ public List<UserReportProjection> getAllUserInfo() {
return users.isEmpty() ? null : users; // 리스트가 비어있으면 null 반환
}

//
// public Page<UserReportProjection> searchUsers(Long userId, String name, String nickName, UserRole userRole, int page, int size) {
// Pageable pageable = (size == 0) ? Pageable.unpaged() : PageRequest.of(page, size, Sort.by("createdAt").descending());
// return userRepository.findUsersWithFilters(userId, name, nickName, userRole, pageable);
// }

public Page<UserReportProjection> searchUsers(Long userId, String name, String nickName, UserRole userRole, int page, int size) {
Pageable pageable = (size == 0) ? Pageable.unpaged() : PageRequest.of(page, size, Sort.by("createdAt").descending());
return userRepository.findUsersWithFilters(userId, name, nickName, userRole, pageable);

public CustomPageResponse<UserReportProjection> searchUsers(
Long userId, String name, String nickName, UserRole userRole, Pageable pageable) {

Page<UserReportProjection> users = userRepository.findUsersWithFilters(userId, name, nickName, userRole, pageable);

return new CustomPageResponse<>(users);
}

}