public async Task <ActionResult <ImportDataValidationResult> > Validate([FromBody] ImportDataValidationRequest request) { if (request.FilePath.IsNullOrEmpty()) { return(BadRequest($"{nameof(request.FilePath)} can not be null or empty.")); } var result = await _csvCustomerDataValidator.ValidateAsync(request.DataType, request.FilePath); return(Ok(result)); }
public virtual async Task ImportAsync(ImportDataRequest request, Action <ImportProgressInfo> progressCallback, ICancellationToken cancellationToken) { ValidateParameters(request, progressCallback, cancellationToken); var errorsContext = new ImportErrorsContext(); var csvPriceDataValidationResult = await _dataValidator.ValidateAsync <TCsvMember>(request.FilePath); if (csvPriceDataValidationResult.Errors.Any()) { throw new InvalidDataException(); } var configuration = new ImportConfiguration(); var reportFilePath = GetReportFilePath(request.FilePath); await using var importReporter = await _importReporterFactory.CreateAsync(reportFilePath, configuration.Delimiter); cancellationToken.ThrowIfCancellationRequested(); var importProgress = new ImportProgressInfo { Description = "Import has started" }; using var dataSource = await _dataSourceFactory.CreateAsync <TCsvMember, TMember>(request.FilePath, ModuleConstants.Settings.PageSize, configuration); var headerRaw = dataSource.GetHeaderRaw(); if (!headerRaw.IsNullOrEmpty()) { await importReporter.WriteHeaderAsync(headerRaw); } importProgress.TotalCount = dataSource.GetTotalCount(); progressCallback(importProgress); const string importDescription = "{0} out of {1} have been imported."; SetupErrorHandlers(progressCallback, configuration, errorsContext, importProgress, importReporter); try { importProgress.Description = "Fetching..."; progressCallback(importProgress); while (await dataSource.FetchAsync()) { await ProcessChunkAsync(request, progressCallback, dataSource, errorsContext, importProgress, importReporter); if (importProgress.ProcessedCount != importProgress.TotalCount) { importProgress.Description = string.Format(importDescription, importProgress.ProcessedCount, importProgress.TotalCount); progressCallback(importProgress); } } } catch (Exception e) { HandleError(progressCallback, importProgress, e.Message); } finally { var completedMessage = importProgress.ErrorCount > 0 ? "Import completed with errors" : "Import completed"; importProgress.Description = $"{completedMessage}: {string.Format(importDescription, importProgress.ProcessedCount, importProgress.TotalCount)}"; if (importReporter.ReportIsNotEmpty) { importProgress.ReportUrl = _blobUrlResolver.GetAbsoluteUrl(reportFilePath); } progressCallback(importProgress); } }