예제 #1
0
        private static string GetFilePath(YearAndMonth yearAndMonth)
        {
            var dataFolder = AppSettings.DataFolder;

            return(Path.Combine(dataFolder,
                                $"{SubscribersPrefix}_{yearAndMonth.Year}_{yearAndMonth.Month.Number:00}{SubscribersExtension}"));
        }
예제 #2
0
        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());
        }
예제 #3
0
        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());
        }