private void SetupUploadService_Success(bool processedSynchronously = true) { var errors = new List <string>(); _mockApprenticeshipBulkUploadService.Setup( m => m.ValidateAndUploadCSV(It.IsAny <string>(), It.IsAny <Stream>(), It.IsAny <AuthUserDetails>())) .ReturnsAsync(ApprenticeshipBulkUploadResult.Success(processedSynchronously)); }
private void SetupUploadService_Errors(List <string> errors) { _mockApprenticeshipBulkUploadService.Setup( m => m.ValidateAndUploadCSV(It.IsAny <string>(), It.IsAny <Stream>(), It.IsAny <AuthUserDetails>())) .ReturnsAsync(ApprenticeshipBulkUploadResult.Failed(errors)); }
public async Task <IActionResult> Upload(IFormFile bulkUploadFile) { var sw = Stopwatch.StartNew(); try { var UKPRN = HttpContext.Session.GetInt32("UKPRN"); if (UKPRN == null) { return(RedirectToAction("Index", "Home", new { errmsg = "Please select a Provider." })); } if (!Validate.ValidateFile(bulkUploadFile, out var errorMessage)) { return(View(new BulkUploadViewModel { errors = new[] { errorMessage } })); } await using var ms = new MemoryStream(); bulkUploadFile.CopyTo(ms); ms.Position = 0; if (Validate.IsBinaryStream(ms)) { return(View(new BulkUploadViewModel { errors = new[] { "Invalid file content." } })); } ApprenticeshipBulkUploadResult result = default; try { ms.Position = 0; result = await _apprenticeshipBulkUploadService.ValidateAndUploadCSV( bulkUploadFile.FileName, ms, _currentUserProvider.GetCurrentUser(), UKPRN.Value); } catch (HeaderValidationException he) { var errors = new[] { ApprenticeshipBulkUploadService.FirstSentence(he) }; return(View(new BulkUploadViewModel { errors = errors })); } catch (BadDataException be) { var errors = be.Message.Split(';'); return(View(new BulkUploadViewModel { errors = errors })); } catch (Exception e) { var errors = new[] { e.Message }; return(View(new BulkUploadViewModel { errors = errors })); } if (result.Errors.Count != 0) { var errors = result.Errors; return(RedirectToAction("WhatDoYouWantToDoNext", "BulkUploadApprenticeships", new { message = $"Your file contained {errors.Count} error{(errors.Count > 1 ? "s" : "")}. You must resolve all errors before your apprenticeship training information can be published.", errorCount = errors.Count } )); } if (result.ProcessedSynchronously) { // All good => redirect to BulkCourses action return(RedirectToAction("PublishYourFile", "BulkUploadApprenticeships")); } else { return(RedirectToAction("Pending")); } } finally { sw.Stop(); } }