Skip to content
This repository has been archived by the owner on Jul 3, 2020. It is now read-only.

Commit

Permalink
Implemented the CurrentTheaterScheduleDataFileManager
Browse files Browse the repository at this point in the history
  • Loading branch information
bofirial committed Jun 10, 2017
1 parent ff8488b commit afef9e8
Showing 1 changed file with 58 additions and 0 deletions.
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);
}
}
}

0 comments on commit afef9e8

Please sign in to comment.