private static string GetFilePath(YearAndMonth yearAndMonth) { var dataFolder = AppSettings.DataFolder; return(Path.Combine(dataFolder, $"{SubscribersPrefix}_{yearAndMonth.Year}_{yearAndMonth.Month.Number:00}{SubscribersExtension}")); }
public static IEnumerable <Subscriber> GetAll(YearAndMonth yearAndMonth) { var file = GetFilePath(yearAndMonth); if (!File.Exists(file)) { return(Enumerable.Empty <Subscriber>()); } using var reader = new StreamReader(file, Encoding.UTF8); using var csvReader = new CsvReader(reader, GetCsvConfiguration()); return(csvReader.GetRecords <Subscriber>().ToArray()); }
public static Exceptional <Unit> Save(IEnumerable <Subscriber> subscribers, YearAndMonth yearAndMonth) { if (AppSettings.IsReadonly) { return(Unit()); } try { var file = GetFilePath(yearAndMonth); using var writer = new StreamWriter(file); using var csvWriter = new CsvWriter(writer, GetCsvConfiguration()); csvWriter.WriteHeader <Subscriber>(); csvWriter.NextRecord(); csvWriter.WriteRecords(subscribers); csvWriter.Flush(); } catch (Exception ex) { return(ex); } return(Unit()); }