public async Task <IActionResult> Put([FromBody] Applicant inputApplicant) { var applicant = inputApplicant; var applicantValidator = new ApplicantValidator(); var result = await applicantValidator.ValidateAsync(applicant); if (result.IsValid) { var updatedApplicant = applicantManager.Update(applicant); if (updatedApplicant != null) { logger.Information($"updated Apllicant with Id:{applicant.ID}"); return(Ok(updatedApplicant)); } else { logger.Warning("Failed to update Applicant with Put request"); return(BadRequest()); } } else { logger.Warning("Failed to update Applicant with Put request, validation failed!"); return(BadRequest()); } }
public async Task <IActionResult> Post([FromBody] ApplicantViewModel inputApplicant) { // mapping shoud be done using Automapper or similar libraries but for simplicity it is mapped manually var applicant = new Applicant { Name = inputApplicant.Name, FamilyName = inputApplicant.FamilyName, Address = inputApplicant.Address, Age = inputApplicant.Age, CountryOfOrigin = inputApplicant.CountryOfOrigin, EmailAdress = inputApplicant.EmailAdress, Hired = inputApplicant.Hired }; var applicantValidator = new ApplicantValidator(); var result = await applicantValidator.ValidateAsync(applicant); if (result.IsValid) { var addedApplicant = applicantManager.Add(applicant); if (addedApplicant != null) { logger.Information($"Added new Apllicant with Id:{applicant.ID}"); return(CreatedAtAction("Get", new { id = applicant.ID }, applicant)); } else { logger.Warning("Failed to Add a new Applicant with Post request"); return(BadRequest()); } } else { logger.Warning("Failed to Add a new Applicant with Post request, validation failed"); return(BadRequest()); } }
public static async Task <List <KeyValue> > ApplicantValidationErrors(Applicant applicant) { var validationErrors = new List <KeyValue>(); var validator = new ApplicantValidator(); var results = await validator.ValidateAsync(applicant); var validatedResponseForCountry = await HttpClientService.ValidateCountryName(applicant.CountryOfOrigin); if (!results.IsValid) { validationErrors.AddRange(results.Errors.Select(error => new KeyValue { PropertyName = error.PropertyName, PropertyValue = error.ErrorMessage, HasValidationErrors = true })); return(validationErrors); } if (!validatedResponseForCountry.Success) { validationErrors.Add(new KeyValue { PropertyName = "Country", PropertyValue = validatedResponseForCountry.Message, HasValidationErrors = true }); } return(validationErrors); }