Skip to content

Commit

Permalink
#237 | Implement the Admin User Registration Application List Endpoint (
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrahimdenizz authored Dec 1, 2023
1 parent 75eb266 commit 92c6694
Show file tree
Hide file tree
Showing 12 changed files with 567 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
package com.ays.admin_user.controller;


import com.ays.admin_user.model.AdminUserRegisterApplication;
import com.ays.admin_user.model.dto.request.AdminUserRegisterApplicationListRequest;
import com.ays.admin_user.model.dto.response.AdminUserRegisterApplicationsResponse;
import com.ays.admin_user.model.mapper.AdminUserRegisterApplicationToAdminUserRegisterApplicationsResponseMapper;
import com.ays.admin_user.service.AdminUserRegisterApplicationService;
import com.ays.common.model.AysPage;
import com.ays.common.model.dto.response.AysPageResponse;
import com.ays.common.model.dto.response.AysResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -11,5 +22,32 @@
@RestController
@RequestMapping("/api/v1/admin")
@RequiredArgsConstructor
public class AdminUserRegisterApplicationController {
class AdminUserRegisterApplicationController {

private final AdminUserRegisterApplicationService adminUserRegisterApplicationService;


private final AdminUserRegisterApplicationToAdminUserRegisterApplicationsResponseMapper adminUserRegisterApplicationToAdminUserRegisterApplicationsResponseMapper = AdminUserRegisterApplicationToAdminUserRegisterApplicationsResponseMapper.initialize();

/**
* Gets a list of admin user register applications in the system.
* Requires SUPER_ADMIN authority.
*
* @param request The request object containing the list criteria.
* @return A response object containing a paginated list of admin user register applications.
*/
@PostMapping("/registration-applications")
@PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
public AysResponse<AysPageResponse<AdminUserRegisterApplicationsResponse>> getRegistrationApplications(@RequestBody AdminUserRegisterApplicationListRequest request) {
final AysPage<AdminUserRegisterApplication> pageOfRegisterApplications = adminUserRegisterApplicationService.getRegistrationApplications(request);
final AysPageResponse<AdminUserRegisterApplicationsResponse> pageResponseOfRegisterApplication = AysPageResponse
.<AdminUserRegisterApplicationsResponse>builder()
.of(pageOfRegisterApplications)
.content(adminUserRegisterApplicationToAdminUserRegisterApplicationsResponseMapper.map(pageOfRegisterApplications.getContent()))
.filteredBy(request.getFilter())
.build();

return AysResponse.successOf(pageResponseOfRegisterApplication);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.ays.admin_user.model.dto.request;

import com.ays.admin_user.model.enums.AdminUserRegisterApplicationStatus;
import com.ays.common.model.AysFiltering;
import com.ays.common.model.dto.request.AysFilteringRequest;
import com.ays.common.model.dto.request.AysPagingRequest;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.validation.Valid;
import jakarta.validation.constraints.AssertTrue;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.data.jpa.domain.Specification;

import java.util.List;
import java.util.Set;

/**
* Represents a request object for fetching a list of admin user registration applications with pagination,sorting
* and filtering options. This class extends the {@link AysPagingRequest} class and adds additional validation rules for sorting.
*/
@Getter
@Setter
public class AdminUserRegisterApplicationListRequest extends AysPagingRequest implements AysFilteringRequest {

@Valid
private Filter filter;

/**
* Represents a filtering configuration for admin user registration applications based on the class fields.
*/
@Getter
@Setter
public static class Filter implements AysFiltering {


/**
* List of admin user registration application's statuses used for filtering.
*/
private List<AdminUserRegisterApplicationStatus> statuses;

}

/**
* Overrides the {@link AysPagingRequest#isSortPropertyAccepted()} method to validate sorting options
* and ensures that no unsupported sorting property is used in the request.
*
* @return true if the sorting property is accepted, false otherwise.
*/
@JsonIgnore
@AssertTrue
@Override
public boolean isSortPropertyAccepted() {
final Set<String> acceptedFilterFields = Set.of("createdAt");
return this.isPropertyAccepted(acceptedFilterFields);
}

/**
* Converts the request into a JPA Specification that filters registration applications based on the specified
* statuses, if they are provided.
*
* @param clazz the class type of the specification.
* @return the generated JPA Specification based on the request filters.
*/
@Override
public <C> Specification<C> toSpecification(Class<C> clazz) {

if (this.filter == null) {
return Specification.allOf();
}

Specification<C> specification = Specification.where(null);

if (!CollectionUtils.isEmpty(this.filter.getStatuses())) {
Specification<C> statusSpecification = this.filter.statuses.stream().map(status ->
(Specification<C>) (root, query, criteriaBuilder) ->
criteriaBuilder.equal(root.get("status"), status))
.reduce(Specification::or).orElse(null);

specification = specification.and(statusSpecification);
}

return specification;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.ays.admin_user.model.dto.response;

import com.ays.admin_user.model.enums.AdminUserRegisterApplicationStatus;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;

/**
* A DTO (Data Transfer Object) representing a list of admin user register applications in a paginated response.
*/
@Getter
@Setter
public class AdminUserRegisterApplicationsResponse {

private String id;
private String reason;
private AdminUserRegisterApplicationStatus status;
private Institution institution;
private AdminUser adminUser;
private String createdUser;
private LocalDateTime createdAt;


/**
* A DTO (Data Transfer Object) representing an admin users in an admin user register application of paginated response.
*/
@Getter
@Setter
public static class AdminUser {
private String id;
private String firstName;
private String lastName;
}

/**
* A DTO (Data Transfer Object) representing an institution in an admin user register application of paginated response.
*/
@Getter
@Setter
public static class Institution {
private String id;
private String name;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* The interface adds no additional mapping methods, but simply defines the types to be used in the mapping process.
*/
@Mapper
public interface AdminUserRegisterApplicationEntityToAdminUserRegisterApplication extends BaseMapper<AdminUserRegisterApplicationEntity, AdminUserRegisterApplication> {
public interface AdminUserRegisterApplicationEntityToAdminUserRegisterApplicationMapper extends BaseMapper<AdminUserRegisterApplicationEntity, AdminUserRegisterApplication> {

static AdminUserRegisterApplicationEntityToAdminUserRegisterApplication initialize() {
return Mappers.getMapper(AdminUserRegisterApplicationEntityToAdminUserRegisterApplication.class);
static AdminUserRegisterApplicationEntityToAdminUserRegisterApplicationMapper initialize() {
return Mappers.getMapper(AdminUserRegisterApplicationEntityToAdminUserRegisterApplicationMapper.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.ays.admin_user.model.mapper;

import com.ays.admin_user.model.AdminUserRegisterApplication;
import com.ays.admin_user.model.dto.response.AdminUserRegisterApplicationsResponse;
import com.ays.common.model.mapper.BaseMapper;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;


/**
* AdminUserRegisterApplicationToAdminUserRegisterApplicationsResponse is an interface that defines the mapping between an {@link AdminUserRegisterApplication} and an {@link AdminUserRegisterApplicationsResponse}.
* This interface uses the MapStruct annotation @Mapper to generate an implementation of this interface at compile-time.
* <p>The class provides a static method {@code initialize()} that returns an instance of the generated mapper implementation.
* <p>The interface extends the MapStruct interface {@link BaseMapper}, which defines basic mapping methods.
* The interface adds no additional mapping methods, but simply defines the types to be used in the mapping process.
*/
@Mapper
public interface AdminUserRegisterApplicationToAdminUserRegisterApplicationsResponseMapper extends BaseMapper<AdminUserRegisterApplication, AdminUserRegisterApplicationsResponse> {

/**
* Initializes the mapper.
*
* @return the initialized mapper object.
*/
static AdminUserRegisterApplicationToAdminUserRegisterApplicationsResponseMapper initialize() {
return Mappers.getMapper(AdminUserRegisterApplicationToAdminUserRegisterApplicationsResponseMapper.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import com.ays.admin_user.model.entity.AdminUserRegisterApplicationEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;


/**
* An interface for the repository managing the verification of admin user registration process in the system.
* Extends the JpaRepository interface, specifying the entity type as {@link AdminUserRegisterApplicationEntity} and the ID type as {@link String}.
*/
public interface AdminUserRegisterApplicationRepository extends JpaRepository<AdminUserRegisterApplicationEntity, String> {
public interface AdminUserRegisterApplicationRepository extends JpaRepository<AdminUserRegisterApplicationEntity, String>, JpaSpecificationExecutor<AdminUserRegisterApplicationEntity> {
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
package com.ays.admin_user.service;

import com.ays.admin_user.model.AdminUserRegisterApplication;
import com.ays.admin_user.model.dto.request.AdminUserRegisterApplicationListRequest;
import com.ays.common.model.AysPage;

/**
* Admin Users Verification application service, which holds the verification information regarding the system user.
*/
public interface AdminUserRegisterApplicationService {

/**
* Get registration applications based on the specified filters in the {@link AdminUserRegisterApplicationListRequest}
*
* @param listRequest covering page and pageSize
* @return Admin User Registration Application list
*/
AysPage<AdminUserRegisterApplication> getRegistrationApplications(AdminUserRegisterApplicationListRequest listRequest);

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package com.ays.admin_user.service.impl;

import com.ays.admin_user.model.AdminUserRegisterApplication;
import com.ays.admin_user.model.dto.request.AdminUserRegisterApplicationListRequest;
import com.ays.admin_user.model.entity.AdminUserRegisterApplicationEntity;
import com.ays.admin_user.model.mapper.AdminUserRegisterApplicationEntityToAdminUserRegisterApplicationMapper;
import com.ays.admin_user.repository.AdminUserRegisterApplicationRepository;
import com.ays.admin_user.service.AdminUserRegisterApplicationService;
import com.ays.common.model.AysPage;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* This class implements the {@link AdminUserRegisterApplicationService} interface and provides verification operations for admin users.
* It is annotated with {@code @Service} to indicate that it is a service component in the application.
Expand All @@ -16,4 +25,29 @@ public class AdminUserRegisterApplicationServiceImpl implements AdminUserRegiste

private final AdminUserRegisterApplicationRepository adminUserRegisterApplicationRepository;


private final AdminUserRegisterApplicationEntityToAdminUserRegisterApplicationMapper adminUserRegisterApplicationEntityToAdminUserRegisterApplicationMapper = AdminUserRegisterApplicationEntityToAdminUserRegisterApplicationMapper.initialize();

/**
* Retrieves a paginated list of admin user registration applications based on the specified filters
*
* @param listRequest covering page, pageSize and filters
* @return a paginated list of admin user registration applications
*/
@Override
public AysPage<AdminUserRegisterApplication> getRegistrationApplications(AdminUserRegisterApplicationListRequest listRequest) {
final Specification<AdminUserRegisterApplicationEntity> filters = listRequest
.toSpecification(AdminUserRegisterApplicationEntity.class);

final Page<AdminUserRegisterApplicationEntity> registerApplicationEntities = adminUserRegisterApplicationRepository
.findAll(filters, listRequest.toPageable());

final List<AdminUserRegisterApplication> registerApplications = adminUserRegisterApplicationEntityToAdminUserRegisterApplicationMapper.map(registerApplicationEntities.getContent());

return AysPage.of(
listRequest.getFilter(),
registerApplicationEntities,
registerApplications
);
}
}
Loading

0 comments on commit 92c6694

Please sign in to comment.