Skip to content

Commit

Permalink
fix: BookLoanRecord 페이지네이션 개선 #354
Browse files Browse the repository at this point in the history
  • Loading branch information
mingmingmon committed May 26, 2024
1 parent 3d139e4 commit 0bb2dea
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
Expand All @@ -17,13 +19,17 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import page.clab.api.domain.book.application.BookLoanRecordService;
import page.clab.api.domain.book.domain.Book;
import page.clab.api.domain.book.domain.BookLoanRecord;
import page.clab.api.domain.book.domain.BookLoanStatus;
import page.clab.api.domain.book.dto.request.BookLoanRecordRequestDto;
import page.clab.api.domain.book.dto.response.BookLoanRecordOverdueResponseDto;
import page.clab.api.domain.book.dto.response.BookLoanRecordResponseDto;
import page.clab.api.global.common.dto.PagedResponseDto;
import page.clab.api.global.common.dto.ApiResponse;
import page.clab.api.global.exception.CustomOptimisticLockingFailureException;
import page.clab.api.global.exception.SortingArgumentException;
import page.clab.api.global.util.PageableUtils;

@RestController
@RequestMapping("/api/v1/book-loan-records")
Expand Down Expand Up @@ -94,9 +100,13 @@ public ApiResponse<PagedResponseDto<BookLoanRecordResponseDto>> getBookLoanRecor
@RequestParam(name = "borrowerId", required = false) String borrowerId,
@RequestParam(name = "status", required = false) BookLoanStatus status,
@RequestParam(name = "page", defaultValue = "0") int page,
@RequestParam(name = "size", defaultValue = "20") int size
) {
Pageable pageable = PageRequest.of(page, size);
@RequestParam(name = "size", defaultValue = "20") int size,
@RequestParam(name = "sortBy", required = false) Optional<List<String>> sortBy,
@RequestParam(name = "sortDirection", required = false) Optional<List<String>> sortDirection
) throws SortingArgumentException {
List<String> sortByList = sortBy.orElse(List.of("borrowedAt", "createdAt"));
List<String> sortDirectionList = sortDirection.orElse(List.of("desc", "asc"));
Pageable pageable = PageableUtils.createPageable(page, size, sortByList, sortDirectionList, BookLoanRecord.class);
PagedResponseDto<BookLoanRecordResponseDto> bookLoanRecords = bookLoanRecordService.getBookLoanRecordsByConditions(bookId, borrowerId, status, pageable);
return ApiResponse.success(bookLoanRecords);
}
Expand All @@ -106,9 +116,13 @@ public ApiResponse<PagedResponseDto<BookLoanRecordResponseDto>> getBookLoanRecor
@GetMapping("/overdue")
public ApiResponse<PagedResponseDto<BookLoanRecordOverdueResponseDto>> getOverdueBookLoanRecords(
@RequestParam(name = "page", defaultValue = "0") int page,
@RequestParam(name = "size", defaultValue = "20") int size
) {
Pageable pageable = PageRequest.of(page, size);
@RequestParam(name = "size", defaultValue = "20") int size,
@RequestParam(name = "sortBy", required = false) Optional<List<String>> sortBy,
@RequestParam(name = "sortDirection", required = false) Optional<List<String>> sortDirection
) throws SortingArgumentException {
List<String> sortByList = sortBy.orElse(List.of("dueDate"));
List<String> sortDirectionList = sortDirection.orElse(List.of("asc"));
Pageable pageable = PageableUtils.createPageable(page, size, sortByList, sortDirectionList, BookLoanRecord.class);
PagedResponseDto<BookLoanRecordOverdueResponseDto> overdueRecords = bookLoanRecordService.getOverdueBookLoanRecords(pageable);
return ApiResponse.success(overdueRecords);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import java.time.LocalDateTime;
import java.util.List;
import page.clab.api.global.util.OrderSpecifierUtil;

@Repository
@RequiredArgsConstructor
Expand Down Expand Up @@ -50,7 +51,7 @@ public Page<BookLoanRecordResponseDto> findByConditions(Long bookId, String borr
.where(builder)
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.orderBy(bookLoanRecord.borrowedAt.desc(), bookLoanRecord.createdAt.asc())
.orderBy(OrderSpecifierUtil.getOrderSpecifiers(pageable, bookLoanRecord))
.fetch();

long total = queryFactory
Expand Down Expand Up @@ -83,7 +84,7 @@ public Page<BookLoanRecordOverdueResponseDto> findOverdueBookLoanRecords(Pageabl
.and(bookLoanRecord.dueDate.lt(now)))
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.orderBy(bookLoanRecord.dueDate.asc())
.orderBy(OrderSpecifierUtil.getOrderSpecifiers(pageable, bookLoanRecord))
.fetch();

long total = queryFactory
Expand Down

0 comments on commit 0bb2dea

Please sign in to comment.