private bool AreValuesValid(Csv csv) { var result = true; foreach (var row in csv.Values) { foreach (var c in _configuration.Columns) { var column = ColumnFactory.GetColumn(c); result = column.IsValueValid(row.ContainsKey(c.Header) ? row[c.Header] as string : null, _apiClient) && result; } } return result; }
private bool IsCsvValid(Csv csv) { if (!csv.Values.Any()) { WriteWarning("No applicants found."); return false; } if (csv.Headers.Count() <= 1) { WriteWarning("Less than two columns."); return false; } return true; }
public Csv ReadAndVerify(out bool hasErrors, out bool hasWarnings) { hasWarnings = false; // read the csv file Csv csv; try { var reader = new CsvReader(new StreamReader(_commandLineArguments.CsvFile, Encoding.UTF8), GetDefaultCsvConfiguration()); csv = new Csv { Values = reader.GetRecords <dynamic>().Select(x => x as IDictionary <string, object>).ToList(), Headers = reader.FieldHeaders }; } catch (Exception ex) { WriteError($"Unable to read CSV: {ex.Message}"); hasErrors = true; return(null); } hasErrors = !IsCsvValid(csv); if (hasErrors) { return(csv); } hasWarnings = !AreAllColumnsFromConfigurationInCsv(csv); hasWarnings = !AreAllColumnsFromCsvInConfiguration(csv) || hasWarnings; hasWarnings = !AreValuesValid(csv) || hasWarnings; return(csv); }
public Csv ReadAndVerify(out bool hasErrors, out bool hasWarnings) { hasWarnings = false; // read the csv file Csv csv; try { var reader = new CsvReader(new StreamReader(_commandLineArguments.CsvFile, Encoding.UTF8), GetDefaultCsvConfiguration()); csv = new Csv { Values = reader.GetRecords<dynamic>().Select(x => x as IDictionary<string, object>).ToList(), Headers = reader.FieldHeaders }; } catch (Exception ex) { WriteError($"Unable to read CSV: {ex.Message}"); hasErrors = true; return null; } hasErrors = !IsCsvValid(csv); if (hasErrors) { return csv; } hasWarnings = !AreAllColumnsFromConfigurationInCsv(csv); hasWarnings = !AreAllColumnsFromCsvInConfiguration(csv) || hasWarnings; hasWarnings = !AreValuesValid(csv) || hasWarnings; return csv; }
public ImportService(Configuration configuration, Csv csv) { _csv = csv; _configuration = configuration; }
private bool AreAllColumnsFromConfigurationInCsv(Csv csv) { var result = true; foreach (var column in _configuration.Columns) { if (!csv.Headers.Any(x => x.Is(column.Header))) { WriteWarning($"The column '{column.Header}' is specified in configuration, but not found in CSV."); result = false; } } return result; }
private bool AreAllColumnsFromCsvInConfiguration(Csv csv) { var result = true; foreach (var column in csv.Headers) { if (!_configuration.Columns.Any(x => x.Header.Is(column))) { WriteWarning($"The column '{column}' is found in CSV, but not specified in configuration."); result = false; } } return result; }