/// <summary> /// Processes request to upload a CSV file of deals to be imported /// </summary> public static ImportSummary ImportFileData(ImportData fileData) { var jobStartTime = DateTime.Now; var importSummary = new ImportSummary(); IList <ImportError> importErrors = new List <ImportError>(); IList <ImportResult> importResults = new List <ImportResult>(); var importProperties = new Dictionary <string, object> { { "NoOfDeals", fileData.DealData.Count }, { "TotalSales", fileData.DealData.Count }, { "TopCar", fileData.DealData.Count }, { "TopDealership", fileData.DealData.Count } }; LogManager.WriteLog("Starting CSV Import", importProperties); var rowCount = 0; foreach (var deal in fileData.DealData) { rowCount++; try { //We can save Deal in DB here var properties = new Dictionary <string, object>(deal.ColumnData.ToDictionary(k => k.Key, k => (object)k.Value)); LogManager.WriteLog("Created deal from CSV", deal); deal.Status = ImportDealStatus.Success; var newDeal = ImportHelpers.ConvertDictionaryTo <Deal>(deal.ColumnData); ImportHelpers.AddImportResultToList(ref importResults, rowCount, "Import", "Deal Added", newDeal); } catch (Exception e) { deal.Status = ImportDealStatus.Exception; deal.Disposition.Add(e.Message); ImportHelpers.AddImportErrorToList(ref importErrors, 0, "Import", e.Message); } } LogManager.WriteLog("Stopping CSV Import", importProperties); var jobEndTime = DateTime.Now; importSummary.ImportErrors = importErrors; importSummary.ImportResults = importResults; return(importSummary); }
public IActionResult ImportFileData() { var uploadFile = HttpContext.Request.Form.Files.GetFile("file"); var importSummary = new ImportSummary(); IList <ImportError> importErrors = new List <ImportError>(); if (ModelState.IsValid) { if (uploadFile != null) { try { var stream = uploadFile.OpenReadStream(); if (!uploadFile.FileName.EndsWith(".csv")) { //Shows error if uploaded file is not csv file ModelState.AddModelError("File", "This file format is not supported"); ImportHelpers.AddImportErrorToList(ref importErrors, 0, "Reading CSV", "This file format is not supported"); importSummary.ImportErrors = importErrors; return(Ok(importSummary)); } LogManager.WriteLog("CSV importing started..."); //Validate and excecute the complete csv file importSummary = _importService.ImportFileData(stream); LogManager.WriteLog("CSV importing completed..."); //Sending result data to View return(Ok(importSummary)); } catch (Exception e) { LogManager.WriteLog("CSV importing closing with errors...", e); ImportHelpers.AddImportErrorToList(ref importErrors, 0, "Reading CSV", "Could not read the file"); importSummary.ImportErrors = importErrors; return(Ok(importSummary)); } } } ModelState.AddModelError("File", "No file is selected"); ImportHelpers.AddImportErrorToList(ref importErrors, 0, "Reading CSV", "No file is selected"); return(Ok(importSummary)); }
public ImportSummary ImportFileData(Stream inputStream) { var fileData = ImportCSV.Parse(inputStream); var csvColumns = GetColumnSpec(CultureInfo.CurrentCulture.Name); //if (!PreValidate(csvColumns, fileData)) if (!PreValidate(csvColumns, fileData)) { IList <ImportError> importErrors = new List <ImportError>(); try { var rowCount = 0; var strErrors = ""; foreach (var deal in fileData.DealData.Where(l => l.Status == ImportDealStatus.Validation)) { rowCount++; strErrors = deal.Disposition.Aggregate(strErrors, (current, disposition) => current + ". " + disposition); } ImportHelpers.AddImportErrorToList(ref importErrors, rowCount, "Import", strErrors); } catch (Exception e) { LogManager.WriteLog("Error adding import validation erros", e); ImportHelpers.AddImportErrorToList(ref importErrors, 0, "Import", e.Message); } var importSummary = new ImportSummary { ImportErrors = importErrors }; return(importSummary); } else { var importSummary = ImportCSV.ImportFileData(fileData); importSummary.Summary = GetImportDBSummary(importSummary.ImportResults); return(importSummary); } }