public IActionResult ReadCSVFile(VisualizationViewModel visualizationModel)
        {
            if (ModelState.IsValid)
            {
                if (!_context.CheckIfNameOfFileIsUnique(visualizationModel.NameOfFile))
                {
                    return(RedirectToAction("Index"));
                }

                var folderName = "Upload";
                ReadFromCSVCollectionWithErrors records = new ReadFromCSVCollectionWithErrors();

                var savePath = Path.Combine(_environment.WebRootPath, folderName, visualizationModel.CsvFile.FileName);

                using (var fileStream = new FileStream(savePath, FileMode.Create))
                {
                    visualizationModel.CsvFile.CopyTo(fileStream);
                }

                records = _csvReader.ReadContentOfCsvFile(savePath);

                if (records.Errors.Count == 0)
                {
                    try
                    {
                        _context.AddDataFromNewCSVFile(records.CollectionOfRecords, visualizationModel.NameOfFile);
                        return(RedirectToAction("Index", new { ModelNotValid = false }));
                    }
                    catch (Exception e)
                    {
                        records.Errors.Add("There was an error during saving data to database");
                    }
                }

                return(RedirectToAction("Index", new { ModelNotValid = true, Errors = records.Errors }));
            }
            else
            {
                return(RedirectToAction("Index", new { ModelNotValid = true }));
            }
        }
Beispiel #2
0
        public ReadFromCSVCollectionWithErrors ReadContentOfCsvFile(string path)
        {
            ReadFromCSVCollectionWithErrors records = new ReadFromCSVCollectionWithErrors
            {
                CollectionOfRecords = new List <ReadFromCSVViewModel>(),
                Errors = new List <string>()
            };

            using (var reader = new StreamReader(path))
            {
                try
                {
                    using (var csv = new CsvReader(reader))
                    {
                        csv.Configuration.PrepareHeaderForMatch = (string header, int index) => header.ToLower();
                        csv.Configuration.HasHeaderRecord       = true;
                        csv.Configuration.RegisterClassMap <ReadFromCSVViewModelMapper>();
                        csv.Configuration.Delimiter         = ",";
                        csv.Configuration.MissingFieldFound = null;

                        try
                        {
                            records.CollectionOfRecords = csv.GetRecords <ReadFromCSVViewModel>().ToList();
                        }

                        catch (ReaderException ex)
                        {
                            records.Errors.Add("Property: '" + IndexOfProperty.GetValueOrDefault(ex.ReadingContext.CurrentIndex) + "' - failed validation. Error was: " + ex.InnerException.Message);
                            records.Errors.Add("Wrong value of input: '" + ex.ReadingContext.Record[ex.ReadingContext.CurrentIndex] + "'.");
                            records.Errors.Add("Row number in file: " + ex.ReadingContext.Row);
                        }

                        ReadFromCSVValidator validator = new ReadFromCSVValidator();

                        int rowNumber = 1;
                        foreach (var record in records.CollectionOfRecords)
                        {
                            ValidationResult results = validator.Validate(record);

                            if (!results.IsValid)
                            {
                                foreach (var failure in results.Errors)
                                {
                                    records.Errors.Add("Property: '" + failure.PropertyName + "' - failed validation. Error was: " + failure.ErrorMessage);
                                    records.Errors.Add("Wrong value of input: '" + failure.AttemptedValue + "'.");
                                    records.Errors.Add("Row number in file: " + rowNumber);
                                }
                            }
                            rowNumber++;
                        }
                    }
                }

                catch (TypeConverterException ex)
                {
                    records.Errors.Add("Property: '" + IndexOfProperty.GetValueOrDefault(ex.ReadingContext.CurrentIndex) + "' - failed validation. Error was: " + ex.Message);
                    records.Errors.Add("Wrong value of input: '" + ex.Text + "'.");
                    records.Errors.Add("Row number in file: " + ex.ReadingContext.Row);
                }
            }

            return(records);
        }