End Broker
This commit is contained in:
53
PlantBox.Broker/PlantBoxesManager.cs
Normal file
53
PlantBox.Broker/PlantBoxesManager.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user