using System.Text.Json;
using System.Text.Json.Serialization;
using Server.Model;
namespace Server.Data
{
public class SettingsData
{
#region Public Properties
public static SettingsData Instance { get; set; } = new();
public Address SellerAddress { get; set; } = new();
public PaymentData PaymentData { get; set; } = new();
public string? Comment { get; set; }
#endregion
#region Private Properties
private static string FileName => "Settings.json";
#endregion
#region Public Method Load
///
/// Asynchronously loads the settings from a JSON file and updates the current instance of .
///
/// A task that represents the asynchronous load 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
///
/// Serializes the current instance of and saves it to a JSON file asynchronously.
///
/// A task that represents the asynchronous save operation.
public static async Task SaveAsync()
{
var jsonString = JsonSerializer.Serialize(Instance, new JsonSerializerOptions { WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull});
await File.WriteAllTextAsync(FileName, jsonString);
}
#endregion
}
}