public static string TransformPayload <T>(string payload, IPIDMapper mapper, ILogger log) { if (string.IsNullOrEmpty(payload)) { throw new ArgumentException("Utility.TransformPayload : Payload is empty ", nameof(payload)); } var output = default(string); var buffer = new MemoryStream(Encoding.ASCII.GetBytes(payload.ToCharArray())); using (var bufferReader = new StreamReader(buffer)) { using (var csvReader = new CsvReader(bufferReader)) { var counter = 0; var records = new List <T>(); SetupCsvReader(csvReader); counter = ReadCsv(mapper, log, csvReader, counter, records); log.LogInformation($"Utility.TransformPayload : Number or records in CSV {counter}" + $" Number of records that are processed and to be imported {records.Count}"); if (records.Count != 0) { output = ConvertToJson(records); } } } return(output); }
private static int ReadCsv <T>(IPIDMapper mapper, ILogger log, CsvReader csvReader, int index, List <T> records) { while (csvReader.Read()) { index++; try { var record = (T)mapper.Map(csvReader); records.Add(record); } catch (Exception exception) { log.LogWarning($"Utility.TransformPayload : Unable to map or read data at record number {index}. More information are here : {exception.Message}"); } } return(index); }