Implementing Service layer
Request handlers in the Controller layer call upon the methods defined in the Service layer to perform business logic on the RequestData and prepare the Response to be returned back to the client.
For this guide, to create the service layer -
Create a new folder within digit folder called service.
Create a new class in this folder by the name
VoterRegistrationService
.Annotate the class with
@Service
annotation.Add the following content to the class -
@Service
public class VoterRegistrationService {
@Autowired
private VoterApplicationValidator validator;
@Autowired
private VoterApplicationEnrichment enrichmentUtil;
@Autowired
private UserService userService;
@Autowired
private WorkflowService workflowService;
@Autowired
private VoterRegistrationRepository voterRegistrationRepository;
@Autowired
private Producer producer;
public List<VoterRegistrationApplication> registerVtRequest(VoterRegistrationRequest voterRegistrationRequest) {
// Validate applications
validator.validateVoterApplication(voterRegistrationRequest);
// Enrich applications
enrichmentUtil.enrichVoterApplication(voterRegistrationRequest);
// Enrich/Upsert user in upon voter registration
userService.callUserService(voterRegistrationRequest);
// Initiate workflow for the new application
workflowService.updateWorkflowStatus(voterRegistrationRequest);
// Push the application to the topic for persister to listen and persist
producer.push("save-vt-application", voterRegistrationRequest);
// Return the response back to user
return voterRegistrationRequest.getVoterRegistrationApplications();
}
public List<VoterRegistrationApplication> searchVtApplications(RequestInfo requestInfo, VoterApplicationSearchCriteria voterApplicationSearchCriteria) {
// Fetch applications from database according to the given search criteria
List<VoterRegistrationApplication> applications = voterRegistrationRepository.getApplications(voterApplicationSearchCriteria);
// If no applications are found matching the given criteria, return an empty list
if(CollectionUtils.isEmpty(applications))
return new ArrayList<>();
// Otherwise return the found applications
return applications;
}
public VoterRegistrationApplication updateVtApplication(VoterRegistrationRequest voterRegistrationRequest) {
// Validate whether the application that is being requested for update indeed exists
VoterRegistrationApplication existingApplication = validator.validateApplicationExistence(voterRegistrationRequest.getVoterRegistrationApplications().get(0));
existingApplication.setWorkflow(voterRegistrationRequest.getVoterRegistrationApplications().get(0).getWorkflow());
voterRegistrationRequest.setVoterRegistrationApplications(Collections.singletonList(existingApplication));
// Enrich application upon update
enrichmentUtil.enrichVoterApplicationUponUpdate(voterRegistrationRequest);
workflowService.updateWorkflowStatus(voterRegistrationRequest);
// Just like create request, update request will be handled asynchronously by the persister
producer.push("update-vt-application", voterRegistrationRequest.getVoterRegistrationApplications().get(0));
return voterRegistrationRequest.getVoterRegistrationApplications().get(0);
}
}
*** NOTE: At this point, your IDE must be showing a lot of errors but do not worry we will add all dependent layers as we progress through this guide and the errors will go away.