diff --git a/src/main/java/page/clab/api/domain/book/api/BookLoanRecordController.java b/src/main/java/page/clab/api/domain/book/api/BookLoanRecordController.java index 6c4c4993f..0e4afcbae 100644 --- a/src/main/java/page/clab/api/domain/book/api/BookLoanRecordController.java +++ b/src/main/java/page/clab/api/domain/book/api/BookLoanRecordController.java @@ -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; @@ -17,6 +19,8 @@ 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; @@ -24,6 +28,8 @@ 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") @@ -94,9 +100,13 @@ public ApiResponse> 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> sortBy, + @RequestParam(name = "sortDirection", required = false) Optional> sortDirection + ) throws SortingArgumentException { + List sortByList = sortBy.orElse(List.of("borrowedAt", "createdAt")); + List sortDirectionList = sortDirection.orElse(List.of("desc", "asc")); + Pageable pageable = PageableUtils.createPageable(page, size, sortByList, sortDirectionList, BookLoanRecord.class); PagedResponseDto bookLoanRecords = bookLoanRecordService.getBookLoanRecordsByConditions(bookId, borrowerId, status, pageable); return ApiResponse.success(bookLoanRecords); } @@ -106,9 +116,13 @@ public ApiResponse> getBookLoanRecor @GetMapping("/overdue") public ApiResponse> 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> sortBy, + @RequestParam(name = "sortDirection", required = false) Optional> sortDirection + ) throws SortingArgumentException { + List sortByList = sortBy.orElse(List.of("dueDate")); + List sortDirectionList = sortDirection.orElse(List.of("asc")); + Pageable pageable = PageableUtils.createPageable(page, size, sortByList, sortDirectionList, BookLoanRecord.class); PagedResponseDto overdueRecords = bookLoanRecordService.getOverdueBookLoanRecords(pageable); return ApiResponse.success(overdueRecords); } diff --git a/src/main/java/page/clab/api/domain/book/dao/BookLoanRecordRepositoryImpl.java b/src/main/java/page/clab/api/domain/book/dao/BookLoanRecordRepositoryImpl.java index e09df3d35..ac79295ff 100644 --- a/src/main/java/page/clab/api/domain/book/dao/BookLoanRecordRepositoryImpl.java +++ b/src/main/java/page/clab/api/domain/book/dao/BookLoanRecordRepositoryImpl.java @@ -15,6 +15,7 @@ import java.time.LocalDateTime; import java.util.List; +import page.clab.api.global.util.OrderSpecifierUtil; @Repository @RequiredArgsConstructor @@ -50,7 +51,7 @@ public Page 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 @@ -83,7 +84,7 @@ public Page 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