using System.Text.Json;
using System.Text.Json.Serialization;
using Server.Model;
namespace Server.Data
{
public class CustomerData
{
#region Public Properties
public List
Customers { get; set; } = [];
public static CustomerData Instance { get; set; } = new();
#endregion
#region Private Properties
private static string FileName => "Customers.json";
#endregion
#region Public Method LoadAsync
///
/// Loads customer data asynchronously from a JSON file if it exists.
///
///
/// A Task representing the asynchronous operation.
///
public static async Task LoadAsync()
{
if (!File.Exists(FileName)) return;
var jsonString = await File.ReadAllTextAsync(FileName);
var deserialized = JsonSerializer.Deserialize(jsonString)!;
Instance = deserialized;
}
#endregion
#region Public Method SaveAsync
///
/// Saves the current customer data asynchronously to a JSON file.
///
///
/// A Task representing the asynchronous operation.
///
public static async Task SaveAsync()
{
var jsonString = JsonSerializer.Serialize(Instance, new JsonSerializerOptions { WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull });
await File.WriteAllTextAsync(FileName, jsonString);
}
#endregion
}
}