Refactor file path retrieval for settings and customer data.

Replaced hardcoded file paths with a method that retrieves the parent directory, ensuring consistency and maintainability. This change affects methods for loading and saving JSON data in both SettingsData and CustomerData classes.
This commit is contained in:
Andre Beging
2024-11-06 16:26:52 +01:00
parent 415ca59575
commit cc877259cf
2 changed files with 29 additions and 14 deletions

View File

@@ -14,11 +14,18 @@ namespace Server.Data
#endregion
#region Private Properties
/// <summary>
/// Retrieves the file path for the settings JSON file.
/// </summary>
/// <returns>The file path of the settings JSON file as a string.</returns>
private static string GetFilePath()
{
var parentDirectory = Directory.GetParent(Directory.GetCurrentDirectory());
if (parentDirectory == null) return "Customers.json";
private static string FileName => "Customers.json";
#endregion
return Path.Combine(parentDirectory.FullName, "Customers.json");
}
#region Public Method LoadAsync
@@ -30,8 +37,8 @@ namespace Server.Data
/// </return>
public static async Task LoadAsync()
{
if (!File.Exists(FileName)) return;
var jsonString = await File.ReadAllTextAsync(FileName);
if (!File.Exists(GetFilePath())) return;
var jsonString = await File.ReadAllTextAsync(GetFilePath());
var deserialized = JsonSerializer.Deserialize<CustomerData>(jsonString)!;
Instance = deserialized;
@@ -50,7 +57,7 @@ namespace Server.Data
public static async Task SaveAsync()
{
var jsonString = JsonSerializer.Serialize(Instance, new JsonSerializerOptions { WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull });
await File.WriteAllTextAsync(FileName, jsonString);
await File.WriteAllTextAsync(GetFilePath(), jsonString);
}
#endregion