diff --git a/api/Hmcr.Domain/Hangfire/RockfallReportJobService.cs b/api/Hmcr.Domain/Hangfire/RockfallReportJobService.cs index 00fa3458..85df8f7f 100644 --- a/api/Hmcr.Domain/Hangfire/RockfallReportJobService.cs +++ b/api/Hmcr.Domain/Hangfire/RockfallReportJobService.cs @@ -487,14 +487,35 @@ private string GetValidationEntityName(RockfallReportCsvDto untypedRow) CsvHelperUtils.Config(errors, csv, false); csv.Configuration.RegisterClassMap(); - var rows = csv.GetRecords().ToList(); - for (var i = 0; i < rows.Count; i++) + var rows = GetRecords(csv); + + return (rows, string.Join(',', csv.Context.HeaderRecord).Replace("\"", "")); + } + + private List GetRecords(CsvReader csv) + { + var rows = new List(); + + while (csv.Read()) { - rows[i].RowNum = i + 2; - rows[i].ServiceArea = _serviceArea.ConvertToServiceAreaString(rows[i].ServiceArea); + RockfallReportCsvDto row = null; + + try + { + row = csv.GetRecord(); + } + catch (Exception ex) + { + _logger.LogError(ex.ToString()); + throw; + } + + row.RowNum = csv.Context.Row; + row.ServiceArea = _serviceArea.ConvertToServiceAreaString(row.ServiceArea); + rows.Add(row); } - return (rows, string.Join(',', csv.Context.HeaderRecord).Replace("\"", "")); + return rows; } private (decimal rowNum, List rows) ParseRowsTyped(string text, Dictionary> errors) diff --git a/api/Hmcr.Domain/Hangfire/WildlifeReportJobService.cs b/api/Hmcr.Domain/Hangfire/WildlifeReportJobService.cs index f76e572f..820a22f1 100644 --- a/api/Hmcr.Domain/Hangfire/WildlifeReportJobService.cs +++ b/api/Hmcr.Domain/Hangfire/WildlifeReportJobService.cs @@ -326,14 +326,35 @@ private string GetValidationEntityName(WildlifeReportCsvDto untypedRow) CsvHelperUtils.Config(errors, csv, false); csv.Configuration.RegisterClassMap(); - var rows = csv.GetRecords().ToList(); - for (var i = 0; i < rows.Count; i++) + var rows = GetRecords(csv); + + return (rows, string.Join(',', csv.Context.HeaderRecord).Replace("\"", "")); + } + + private List GetRecords(CsvReader csv) + { + var rows = new List(); + + while (csv.Read()) { - rows[i].RowNum = i + 2; - rows[i].ServiceArea = _serviceArea.ConvertToServiceAreaString(rows[i].ServiceArea); + WildlifeReportCsvDto row = null; + + try + { + row = csv.GetRecord(); + } + catch (Exception ex) + { + _logger.LogError(ex.ToString()); + throw; + } + + row.RowNum = csv.Context.Row; + row.ServiceArea = _serviceArea.ConvertToServiceAreaString(row.ServiceArea); + rows.Add(row); } - return (rows, string.Join(',', csv.Context.HeaderRecord).Replace("\"", "")); + return rows; } private (decimal rowNum, List rows) ParseRowsTyped(string text, Dictionary> errors) diff --git a/api/Hmcr.Domain/Hangfire/WorkReportJobService.cs b/api/Hmcr.Domain/Hangfire/WorkReportJobService.cs index e4aafd54..f3d561bc 100644 --- a/api/Hmcr.Domain/Hangfire/WorkReportJobService.cs +++ b/api/Hmcr.Domain/Hangfire/WorkReportJobService.cs @@ -1,4 +1,5 @@ using CsvHelper; +using CsvHelper.TypeConversion; using Hmcr.Data.Database; using Hmcr.Data.Database.Entities; using Hmcr.Data.Repositories; @@ -19,6 +20,7 @@ using System.Globalization; using System.IO; using System.Linq; +using System.Reflection.Metadata.Ecma335; using System.Text; using System.Threading.Tasks; @@ -648,14 +650,35 @@ private string GetValidationEntityName(WorkReportCsvDto untypedRow, ActivityCode CsvHelperUtils.Config(errors, csv, false); csv.Configuration.RegisterClassMap(); - var rows = csv.GetRecords().ToList(); - for (var i = 0; i < rows.Count; i++) + var rows = GetRecords(csv); + + return (rows, string.Join(',', csv.Context.HeaderRecord).Replace("\"", "")); + } + + private List GetRecords(CsvReader csv) + { + var rows = new List(); + + while (csv.Read()) { - rows[i].RowNum = i + 2; - rows[i].ServiceArea = _serviceArea.ConvertToServiceAreaString(rows[i].ServiceArea); + WorkReportCsvDto row = null; + + try + { + row = csv.GetRecord(); + } + catch (Exception ex) + { + _logger.LogError(ex.ToString()); + throw; + } + + row.RowNum = csv.Context.Row; + row.ServiceArea = _serviceArea.ConvertToServiceAreaString(row.ServiceArea); + rows.Add(row); } - return (rows, string.Join(',', csv.Context.HeaderRecord).Replace("\"", "")); + return rows; } private (decimal rowNum, List rows) ParseRowsTyped(string text, Dictionary> errors) diff --git a/api/Hmcr.Domain/Services/RockfallReportService.cs b/api/Hmcr.Domain/Services/RockfallReportService.cs index b1bd610b..de337d1b 100644 --- a/api/Hmcr.Domain/Services/RockfallReportService.cs +++ b/api/Hmcr.Domain/Services/RockfallReportService.cs @@ -55,7 +55,6 @@ protected override async Task ParseRowsAsync(SubmissionObjectCreateDto sub var headerValidated = false; var rows = new List(); - var rowNum = 1; while (csv.Read()) { @@ -77,7 +76,6 @@ protected override async Task ParseRowsAsync(SubmissionObjectCreateDto sub } } - row.RowNum = ++rowNum; row.ServiceArea = serviceArea.ConvertToServiceAreaString(row.ServiceArea); rows.Add(row); } diff --git a/api/Hmcr.Domain/Services/WildlifeReportService.cs b/api/Hmcr.Domain/Services/WildlifeReportService.cs index a99253d6..1903b685 100644 --- a/api/Hmcr.Domain/Services/WildlifeReportService.cs +++ b/api/Hmcr.Domain/Services/WildlifeReportService.cs @@ -53,7 +53,6 @@ protected override async Task ParseRowsAsync(SubmissionObjectCreateDto sub var headerValidated = false; var rows = new List(); - var rowNum = 1; while (csv.Read()) { @@ -75,7 +74,6 @@ protected override async Task ParseRowsAsync(SubmissionObjectCreateDto sub } } - row.RowNum = ++rowNum; row.ServiceArea = serviceArea.ConvertToServiceAreaString(row.ServiceArea); rows.Add(row); } diff --git a/api/Hmcr.Domain/Services/WorkReportService.cs b/api/Hmcr.Domain/Services/WorkReportService.cs index 61b594c6..292d5856 100644 --- a/api/Hmcr.Domain/Services/WorkReportService.cs +++ b/api/Hmcr.Domain/Services/WorkReportService.cs @@ -55,7 +55,6 @@ protected override async Task ParseRowsAsync(SubmissionObjectCreateDto sub var headerValidated = false; var rows = new List(); - var rowNum = 1; while (csv.Read()) { @@ -77,7 +76,6 @@ protected override async Task ParseRowsAsync(SubmissionObjectCreateDto sub } } - row.RowNum = ++rowNum; row.ServiceArea = serviceArea.ConvertToServiceAreaString(row.ServiceArea); rows.Add(row); }