58 lines
1.8 KiB
C#
58 lines
1.8 KiB
C#
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
|
|
|
|
/// <summary>
|
|
/// Asynchronously loads the settings from a JSON file and updates the current instance of <see cref="SettingsData" />.
|
|
/// </summary>
|
|
/// <returns>A task that represents the asynchronous load operation.</returns>
|
|
public static async Task LoadAsync()
|
|
{
|
|
if (!File.Exists(FileName)) return;
|
|
var jsonString = await File.ReadAllTextAsync(FileName);
|
|
var deserialized = JsonSerializer.Deserialize<SettingsData>(jsonString)!;
|
|
|
|
Instance = deserialized;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Public Method SaveAsync
|
|
|
|
/// <summary>
|
|
/// Serializes the current instance of <see cref="SettingsData" /> and saves it to a JSON file asynchronously.
|
|
/// </summary>
|
|
/// <returns>A task that represents the asynchronous save operation.</returns>
|
|
public static async Task SaveAsync()
|
|
{
|
|
var jsonString = JsonSerializer.Serialize(Instance, new JsonSerializerOptions { WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull});
|
|
await File.WriteAllTextAsync(FileName, jsonString);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |