From cc877259cfc6bd62784092984cef42f8c38d214c Mon Sep 17 00:00:00 2001 From: Andre Beging Date: Wed, 6 Nov 2024 16:26:52 +0100 Subject: [PATCH] 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. --- Server/Data/CustomerData.cs | 21 ++++++++++++++------- Server/Data/SettingsData.cs | 22 +++++++++++++++------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/Server/Data/CustomerData.cs b/Server/Data/CustomerData.cs index 538810f..426f5fc 100644 --- a/Server/Data/CustomerData.cs +++ b/Server/Data/CustomerData.cs @@ -14,11 +14,18 @@ namespace Server.Data #endregion - #region Private Properties + + /// + /// Retrieves the file path for the settings JSON file. + /// + /// The file path of the settings JSON file as a string. + 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 /// 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(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 diff --git a/Server/Data/SettingsData.cs b/Server/Data/SettingsData.cs index e0c52ae..dddccb3 100644 --- a/Server/Data/SettingsData.cs +++ b/Server/Data/SettingsData.cs @@ -18,12 +18,19 @@ namespace Server.Data #endregion - #region Private Properties - private static string FileName => "Settings.json"; - - #endregion + /// + /// Retrieves the file path for the settings JSON file. + /// + /// The file path of the settings JSON file as a string. + private static string GetFilePath() + { + var parentDirectory = Directory.GetParent(Directory.GetCurrentDirectory()); + if (parentDirectory == null) return "Settings.json"; + return Path.Combine(parentDirectory.FullName, "Settings.json"); + } + #region Public Method Load /// @@ -32,8 +39,9 @@ namespace Server.Data /// A task that represents the asynchronous load operation. 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(jsonString)!; Instance = deserialized; @@ -50,7 +58,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