58 lines
1.6 KiB
C#
58 lines
1.6 KiB
C#
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using Server.Model;
|
|
|
|
namespace Server.Data
|
|
{
|
|
public class CustomerData
|
|
{
|
|
#region Public Properties
|
|
|
|
public List<Address> 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
|
|
|
|
/// <summary>
|
|
/// Loads customer data asynchronously from a JSON file if it exists.
|
|
/// </summary>
|
|
/// <return>
|
|
/// A Task representing the asynchronous operation.
|
|
/// </return>
|
|
public static async Task LoadAsync()
|
|
{
|
|
if (!File.Exists(FileName)) return;
|
|
var jsonString = await File.ReadAllTextAsync(FileName);
|
|
var deserialized = JsonSerializer.Deserialize<CustomerData>(jsonString)!;
|
|
|
|
Instance = deserialized;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Public Method SaveAsync
|
|
|
|
/// <summary>
|
|
/// Saves the current customer data asynchronously to a JSON file.
|
|
/// </summary>
|
|
/// <return>
|
|
/// A Task representing the asynchronous operation.
|
|
/// </return>
|
|
public static async Task SaveAsync()
|
|
{
|
|
var jsonString = JsonSerializer.Serialize(Instance, new JsonSerializerOptions { WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull });
|
|
await File.WriteAllTextAsync(FileName, jsonString);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |