Skip to content

Commit

Permalink
AYS-29 | Pagination Infrastructure Has Been Refactored and Pagination…
Browse files Browse the repository at this point in the history
… Validations Have Been Fixed (#271)
  • Loading branch information
agitrubard authored Dec 28, 2023
1 parent 1d79cd2 commit ba81d24
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -55,28 +55,35 @@ class AdminUserRegisterApplicationController {
*/
@PostMapping("/registration-applications")
@PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
public AysResponse<AysPageResponse<AdminUserRegisterApplicationsResponse>> getRegistrationApplications(@RequestBody AdminUserRegisterApplicationListRequest request) {
public AysResponse<AysPageResponse<AdminUserRegisterApplicationsResponse>> getRegistrationApplications(
@RequestBody @Valid AdminUserRegisterApplicationListRequest request) {

final AysPage<AdminUserRegisterApplication> pageOfRegisterApplications = adminUserRegisterApplicationService.getRegistrationApplications(request);
final AysPageResponse<AdminUserRegisterApplicationsResponse> pageResponseOfRegisterApplication = AysPageResponse
.<AdminUserRegisterApplicationsResponse>builder()
.of(pageOfRegisterApplications)
.content(adminUserRegisterApplicationToAdminUserRegisterApplicationsResponseMapper.map(pageOfRegisterApplications.getContent()))
.content(
adminUserRegisterApplicationToAdminUserRegisterApplicationsResponseMapper
.map(pageOfRegisterApplications.getContent())
)
.filteredBy(request.getFilter())
.build();

return AysResponse.successOf(pageResponseOfRegisterApplication);
}

/**
* 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.
* @return A response with the register application detail.
*/
@GetMapping("/registration-application/{id}")
@PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
public AysResponse<AdminUserRegisterApplicationResponse> getRegistrationApplicationById(@PathVariable String id) {
public AysResponse<AdminUserRegisterApplicationResponse> getRegistrationApplicationById(
@PathVariable @UUID String id) {

final AdminUserRegisterApplication registerApplication = adminUserRegisterApplicationService
.getRegistrationApplicationById(id);

Expand All @@ -93,7 +100,9 @@ public AysResponse<AdminUserRegisterApplicationResponse> getRegistrationApplicat
* @return A response with the register application summary.
*/
@GetMapping("/registration-application/{id}/summary")
public AysResponse<AdminUserRegisterApplicationSummaryResponse> getRegistrationApplicationSummaryById(@PathVariable @UUID String id) {
public AysResponse<AdminUserRegisterApplicationSummaryResponse> getRegistrationApplicationSummaryById(
@PathVariable @UUID String id) {

final AdminUserRegisterApplication registerApplication = adminUserRegisterApplicationService
.getRegistrationApplicationSummaryById(id);

Expand All @@ -106,15 +115,19 @@ public AysResponse<AdminUserRegisterApplicationSummaryResponse> 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<AdminUserRegisterApplicationCreateResponse> createRegistrationApplication(@RequestBody @Valid AdminUserRegisterApplicationCreateRequest request) {
AdminUserRegisterApplication registerApplication = adminUserRegisterApplicationService.createRegistrationApplication(request);
public AysResponse<AdminUserRegisterApplicationCreateResponse> createRegistrationApplication(
@RequestBody @Valid AdminUserRegisterApplicationCreateRequest createRequest) {

AdminUserRegisterApplication registerApplication = adminUserRegisterApplicationService
.createRegistrationApplication(createRequest);
return AysResponse.successOf(
adminUserRegisterApplicationToAdminUserRegisterApplicationCreateResponseMapper.map(registerApplication)
adminUserRegisterApplicationToAdminUserRegisterApplicationCreateResponseMapper
.map(registerApplication)
);
}

Expand Down
55 changes: 16 additions & 39 deletions src/main/java/com/ays/common/model/AysPaging.java
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* 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.
* <p>
* 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.
* <p>
* 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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/com/ays/common/model/AysPagingBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit ba81d24

Please sign in to comment.