public async Task <IActionResult> Update([FromBody] IEnumerable <UpdateBatchEpaRequest> batchRequest) { var bag = new List <BatchEpaResponse>(); foreach (var request in batchRequest) { Standard standard = null; if (!string.IsNullOrEmpty(request.Version)) { standard = await _mediator.Send( new GetStandardVersionRequest { StandardId = request.GetStandardId(), Version = request.Version }); } else { standard = await _mediator.Send(new GetCalculatedStandardVersionForApprenticeshipRequest { StandardId = request.GetStandardId(), Uln = request.Uln }); } // Get Existing Certificate if it exists if (standard != null) { var existingCertificate = await _mediator.Send(new GetCertificateForUlnRequest { StandardCode = standard.LarsCode, Uln = request.Uln }); request.PopulateMissingFields(standard, existingCertificate); } var validationResult = await _updateValidator.ValidateAsync(request); var isRequestValid = validationResult.IsValid; var validationErrors = validationResult.Errors.Select(error => error.ErrorMessage).ToList(); var epaResponse = new BatchEpaResponse { RequestId = request.RequestId, Uln = request.Uln, StandardCode = request.StandardCode, StandardReference = request.StandardReference, FamilyName = request.FamilyName, ValidationErrors = validationErrors }; if (!validationErrors.Any() && isRequestValid) { epaResponse.EpaDetails = await _mediator.Send(request); } bag.Add(epaResponse); } return(Ok(bag.ToList())); }
public async Task <IActionResult> Create([FromBody] IEnumerable <CreateBatchEpaRequest> batchRequest) { var bag = new List <BatchEpaResponse>(); foreach (var request in batchRequest) { var validationErrors = new List <string>(); var isRequestValid = false; var collatedStandard = request.StandardCode > 0 ? await GetCollatedStandard(request.StandardCode) : await GetCollatedStandard(request.StandardReference); if (collatedStandard != null) { // Only fill in the missing bits... if (request.StandardCode < 1) { request.StandardCode = collatedStandard.StandardId ?? int.MinValue; } else if (string.IsNullOrEmpty(request.StandardReference)) { request.StandardReference = collatedStandard.ReferenceNumber; } } var validationResult = await _createValidator.ValidateAsync(request); isRequestValid = validationResult.IsValid; validationErrors = validationResult.Errors.Select(error => error.ErrorMessage).ToList(); var epaResponse = new BatchEpaResponse { RequestId = request.RequestId, Uln = request.Uln, StandardCode = request.StandardCode, StandardReference = request.StandardReference, FamilyName = request.FamilyName, ValidationErrors = validationErrors }; if (!validationErrors.Any() && isRequestValid) { epaResponse.EpaDetails = await _mediator.Send(request); } bag.Add(epaResponse); } return(Ok(bag.ToList())); }