diff --git a/Melody49Notifier/DataAbstraction/CurrentTheaterScheduleDataFileManager.cs b/Melody49Notifier/DataAbstraction/CurrentTheaterScheduleDataFileManager.cs new file mode 100644 index 0000000..3d24f14 --- /dev/null +++ b/Melody49Notifier/DataAbstraction/CurrentTheaterScheduleDataFileManager.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Melody49Notifier.Models; +using Microsoft.Azure.WebJobs.Host; +using System.IO; +using Newtonsoft.Json; + +namespace Melody49Notifier.DataAbstraction +{ + public class CurrentTheaterScheduleDataFileManager : ICurrentTheaterScheduleDataFileManager + { + private readonly TraceWriter log; + + public CurrentTheaterScheduleDataFileManager(TraceWriter log) + { + this.log = log; + } + + static string HomeDirectory => Environment.GetEnvironmentVariable("HOME") ?? $"{Environment.GetEnvironmentVariable("HOMEDRIVE")}{Environment.GetEnvironmentVariable("HOMEPATH")}"; + static string ApplicationDirectory => $"{HomeDirectory}\\data\\Melody49Notifier"; + static string CurrentTheaterScheduleDataFile => $"{ApplicationDirectory}\\currentTheaterSchedule.json"; + + public TheaterSchedule SelectCurrentTheaterSchedule() + { + if (File.Exists(CurrentTheaterScheduleDataFile)) + { + string currentTheaterScheduleDataFileContents = File.ReadAllText(CurrentTheaterScheduleDataFile); + + log.Verbose($"Obtained Current Theater Schedule Data File Contents: {currentTheaterScheduleDataFileContents}"); + + return JsonConvert.DeserializeObject(currentTheaterScheduleDataFileContents); + } + + log.Verbose($"The Current Theater Schedule Data File Does Not Exist."); + + return null; + } + + public void UpdateCurrentTheaterSchedule(TheaterSchedule theaterSchedule) + { + if (!Directory.Exists(ApplicationDirectory)) + { + log.Verbose($"Creating Application Directory: ({ApplicationDirectory})"); + + Directory.CreateDirectory(ApplicationDirectory); + } + + string serializedTheaterSchedule = JsonConvert.SerializeObject(theaterSchedule); + + log.Verbose($"Saving the Current Theater Schedule: ({serializedTheaterSchedule})"); + + File.WriteAllText(CurrentTheaterScheduleDataFile, serializedTheaterSchedule); + } + } +}