Files
PlantBox/PlantBox.Broker/PlantBoxesManager.cs
2019-04-24 19:27:40 +02:00

54 lines
1.3 KiB
C#

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace PlantBox.Broker
{
class PlantBoxesManager
{
public string FilePath => Path.Combine(Environment.CurrentDirectory, FileName);
public string FileName => "storage.json";
private Dictionary<ulong, PlantBox> _plantBoxes;
public PlantBoxesManager()
{
}
public PlantBox this[ulong id]
{
get => _plantBoxes.GetValueOrDefault(id);
}
public PlantBox GetPlantBox(ulong id) => this[id];
public void Load()
{
Console.WriteLine("Loading storage...");
if (File.Exists(FilePath))
{
_plantBoxes = JsonConvert.DeserializeObject<Dictionary<ulong, PlantBox>>(File.ReadAllText(FilePath));
}
else
{
_plantBoxes = new Dictionary<ulong, PlantBox>();
}
Console.WriteLine("Storage loaded");
}
public void Save()
{
Console.WriteLine("Saving storage...");
File.WriteAllText(FilePath, JsonConvert.SerializeObject(_plantBoxes));
Console.WriteLine("Storage saved");
}
}
}