This repository has been archived by the owner on Jul 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented the CurrentTheaterScheduleDataFileManager
- Loading branch information
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
Melody49Notifier/DataAbstraction/CurrentTheaterScheduleDataFileManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<TheaterSchedule>(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); | ||
} | ||
} | ||
} |