From ba81d24e1407abbe34334946ef2d6f0a5a9e24eb Mon Sep 17 00:00:00 2001 From: Agit Rubar Demir <61833677+agitrubard@users.noreply.github.com> Date: Thu, 28 Dec 2023 13:03:55 +0300 Subject: [PATCH] AYS-29 | Pagination Infrastructure Has Been Refactored and Pagination Validations Have Been Fixed (#271) --- ...dminUserRegisterApplicationController.java | 33 +++++++---- .../java/com/ays/common/model/AysPaging.java | 55 ++++++------------- .../model/dto/request/AysPagingRequest.java | 8 +-- src/main/resources/application.yml | 2 +- .../ays/common/model/AysPagingBuilder.java | 8 +-- 5 files changed, 48 insertions(+), 58 deletions(-) diff --git a/src/main/java/com/ays/admin_user/controller/AdminUserRegisterApplicationController.java b/src/main/java/com/ays/admin_user/controller/AdminUserRegisterApplicationController.java index a7ecf605a..5a04e2602 100644 --- a/src/main/java/com/ays/admin_user/controller/AdminUserRegisterApplicationController.java +++ b/src/main/java/com/ays/admin_user/controller/AdminUserRegisterApplicationController.java @@ -31,10 +31,10 @@ /** * Admin Register Application controller to perform register application api operations for admins. */ +@Validated @RestController @RequestMapping("/api/v1/admin") @RequiredArgsConstructor -@Validated class AdminUserRegisterApplicationController { private final AdminUserRegisterApplicationService adminUserRegisterApplicationService; @@ -55,12 +55,17 @@ class AdminUserRegisterApplicationController { */ @PostMapping("/registration-applications") @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')") - public AysResponse> getRegistrationApplications(@RequestBody AdminUserRegisterApplicationListRequest request) { + public AysResponse> getRegistrationApplications( + @RequestBody @Valid AdminUserRegisterApplicationListRequest request) { + final AysPage pageOfRegisterApplications = adminUserRegisterApplicationService.getRegistrationApplications(request); final AysPageResponse pageResponseOfRegisterApplication = AysPageResponse .builder() .of(pageOfRegisterApplications) - .content(adminUserRegisterApplicationToAdminUserRegisterApplicationsResponseMapper.map(pageOfRegisterApplications.getContent())) + .content( + adminUserRegisterApplicationToAdminUserRegisterApplicationsResponseMapper + .map(pageOfRegisterApplications.getContent()) + ) .filteredBy(request.getFilter()) .build(); @@ -68,7 +73,7 @@ public AysResponse> getRe } /** - * Gets an admin user register application detail in the system. + * Gets an admin user registers application detail in the system. * Requires SUPER_ADMIN authority. * * @param id The id of the register application. @@ -76,7 +81,9 @@ public AysResponse> getRe */ @GetMapping("/registration-application/{id}") @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')") - public AysResponse getRegistrationApplicationById(@PathVariable String id) { + public AysResponse getRegistrationApplicationById( + @PathVariable @UUID String id) { + final AdminUserRegisterApplication registerApplication = adminUserRegisterApplicationService .getRegistrationApplicationById(id); @@ -93,7 +100,9 @@ public AysResponse getRegistrationApplicat * @return A response with the register application summary. */ @GetMapping("/registration-application/{id}/summary") - public AysResponse getRegistrationApplicationSummaryById(@PathVariable @UUID String id) { + public AysResponse getRegistrationApplicationSummaryById( + @PathVariable @UUID String id) { + final AdminUserRegisterApplication registerApplication = adminUserRegisterApplicationService .getRegistrationApplicationSummaryById(id); @@ -106,15 +115,19 @@ public AysResponse getRegistrationA * Creates a new admin user register application. * Requires SUPER_ADMIN authority. * - * @param request The request object containing the register application details. + * @param createRequest The request object containing the register application details. * @return A response object containing the created register application. */ @PostMapping("/registration-application") @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')") - public AysResponse createRegistrationApplication(@RequestBody @Valid AdminUserRegisterApplicationCreateRequest request) { - AdminUserRegisterApplication registerApplication = adminUserRegisterApplicationService.createRegistrationApplication(request); + public AysResponse createRegistrationApplication( + @RequestBody @Valid AdminUserRegisterApplicationCreateRequest createRequest) { + + AdminUserRegisterApplication registerApplication = adminUserRegisterApplicationService + .createRegistrationApplication(createRequest); return AysResponse.successOf( - adminUserRegisterApplicationToAdminUserRegisterApplicationCreateResponseMapper.map(registerApplication) + adminUserRegisterApplicationToAdminUserRegisterApplicationCreateResponseMapper + .map(registerApplication) ); } diff --git a/src/main/java/com/ays/common/model/AysPaging.java b/src/main/java/com/ays/common/model/AysPaging.java index ea8ed2fb7..13b8c9e09 100644 --- a/src/main/java/com/ays/common/model/AysPaging.java +++ b/src/main/java/com/ays/common/model/AysPaging.java @@ -1,58 +1,35 @@ package com.ays.common.model; -import jakarta.validation.constraints.Max; -import jakarta.validation.constraints.Min; -import jakarta.validation.constraints.NotNull; import lombok.Getter; import lombok.Setter; import org.hibernate.validator.constraints.Range; /** - * A class representing paging parameters for a query. It includes the page number and page size. - * The page number is 1-based, but internally, it is stored as 0-based to comply with Spring Data's pagination system. + * The class `Paging` is a data model used for pagination operations. It includes essential + * pagination parameters such as page number and page size. + *

+ * Instances of the `Paging` class can be used in pagination operations, and using this class + * to validate pagination parameters from user input can be beneficial. + *

+ * Note: This class is designed to assist in proper pagination operations and can be used + * to ensure that parameters like page number or page size stay within certain bounds. */ @Getter @Setter public class AysPaging { /** - * Represents the page number for pagination. - * The page number must be greater than or equal to 1. - *

- * This field is annotated with {@code @NotNull} to indicate that it cannot be null. - * Additionally, it is annotated with {@code @Min(1)} to specify that the value must be at least 1, - * and {@code @Max(Long.MAX_VALUE)} to set the maximum allowed value to Long.MAX_VALUE. + * Represents the page number. + * This value should be between 1 and 99999999. */ - @NotNull - @Min(1) - @Max(Long.MAX_VALUE) - public Long page; + @Range(min = 1, max = 99999999) + public int page; /** - * Represents the number of elements to be displayed per page in a paginated result set. - * The page size should be exactly 10. - * - * This field is annotated with {@code @NotNull} to indicate that it cannot be null. - * Additionally, it is annotated with a custom or library-specific {@code @Range} annotation, - * specifying that the valid page size is exactly 10. + * Represents the page size. + * This value should be between 10 and 10, specifying the number of items per page. */ - @NotNull - @Range(min = 10, max = 10) - public Long pageSize; - - /** - * Retrieves the 0-based page number, which is utilized internally by Spring Data's pagination system. - * If the original page number is null, the method returns null. - * - * @return The 0-based page number or null if the original page number is null. - */ - public Long getPage() { - - if (this.page == null) { - return null; - } - - return this.page - 1; - } + @Range(min = 1, max = 99999999) + public int pageSize; } diff --git a/src/main/java/com/ays/common/model/dto/request/AysPagingRequest.java b/src/main/java/com/ays/common/model/dto/request/AysPagingRequest.java index 449bef6b8..a6174ef97 100644 --- a/src/main/java/com/ays/common/model/dto/request/AysPagingRequest.java +++ b/src/main/java/com/ays/common/model/dto/request/AysPagingRequest.java @@ -35,15 +35,15 @@ public Pageable toPageable() { if (super.isSortable()) { return PageRequest.of( - Math.toIntExact(pagination.getPage()), - Math.toIntExact(pagination.getPageSize()), + this.pagination.getPage() - 1, + this.pagination.getPageSize(), super.toSort() ); } return PageRequest.of( - Math.toIntExact(pagination.getPage()), - Math.toIntExact(pagination.getPageSize()) + this.pagination.getPage() - 1, + this.pagination.getPageSize() ); } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 5a6e6a86c..845bc6e4c 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -10,7 +10,7 @@ spring: datasource: username: ${AYS_DB_USERNAME:ays} password: ${AYS_DB_PASSWORD:ayspass} - url: jdbc:mysql://${AYS_DB_IP:localhost}:${AYS_DB_PORT:3306}/ays + url: jdbc:mysql://${AYS_DB_IP:localhost}:${AYS_DB_PORT:3307}/ays driver-class-name: com.mysql.cj.jdbc.Driver hikari: username: ${spring.datasource.username} diff --git a/src/test/java/com/ays/common/model/AysPagingBuilder.java b/src/test/java/com/ays/common/model/AysPagingBuilder.java index 355f8039b..29d7addaf 100644 --- a/src/test/java/com/ays/common/model/AysPagingBuilder.java +++ b/src/test/java/com/ays/common/model/AysPagingBuilder.java @@ -8,16 +8,16 @@ public AysPagingBuilder() { public AysPagingBuilder withValidValues() { return this - .withPage(1L) - .withPageSize(10L); + .withPage(1) + .withPageSize(10); } - public AysPagingBuilder withPage(Long page) { + public AysPagingBuilder withPage(int page) { data.setPage(page); return this; } - public AysPagingBuilder withPageSize(Long pageSize) { + public AysPagingBuilder withPageSize(int pageSize) { data.setPageSize(pageSize); return this; }