diff --git a/Server/Data/InvoiceData.cs b/Server/Data/InvoiceData.cs index 1662aa7..944719f 100644 --- a/Server/Data/InvoiceData.cs +++ b/Server/Data/InvoiceData.cs @@ -8,7 +8,7 @@ namespace Server.Data { #region Private Properties - private static string DirectoryName => "invoices"; + private static string DirectoryName => "Invoices"; #endregion @@ -19,11 +19,10 @@ namespace Server.Data /// /// The ID of the invoice to be deleted. /// A task representing the asynchronous delete operation. - public static Task DeleteAsync(string invoiceId) + public static async Task DeleteAsync(string invoiceId) { - var fileName = BuildFileName(invoiceId); + var fileName = await BuildFileName(invoiceId); if (File.Exists(fileName)) File.Delete(fileName); - return Task.CompletedTask; } #endregion @@ -35,7 +34,7 @@ namespace Server.Data /// /// The ID of the invoice to check for existence. /// A task representing the asynchronous check operation, with a boolean result indicating whether the file exists. - public static Task ExistsAsync(string invoiceId) => Task.FromResult(File.Exists(BuildFileName(invoiceId))); + public static async Task ExistsAsync(string invoiceId) => File.Exists(await BuildFileName(invoiceId)); #endregion @@ -49,7 +48,7 @@ namespace Server.Data { var invoices = new List(); - var invoiceFiles = Directory.GetFiles(DirectoryName, "Invoice_*.json"); + var invoiceFiles = Directory.GetFiles(await GetDirectoryAsync(), "Invoice_*.json"); foreach (var file in invoiceFiles) { var invoice = await LoadFileAsync(file); @@ -70,7 +69,7 @@ namespace Server.Data /// The deserialized instance if the file exists; otherwise, null. public static async Task LoadAsync(string invoiceId) { - var fileName = BuildFileName(invoiceId); + var fileName = await BuildFileName(invoiceId); return await LoadFileAsync(fileName); } @@ -89,18 +88,19 @@ namespace Server.Data if (string.IsNullOrWhiteSpace(invoice.InvoiceId)) invoice.InvoiceId = Guid.NewGuid().ToString(); - // Ensure directory exists - if (!Directory.Exists(DirectoryName)) Directory.CreateDirectory(DirectoryName); - var jsonString = JsonSerializer.Serialize(invoice, new JsonSerializerOptions { WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }); - await File.WriteAllTextAsync(BuildFileName(invoice.InvoiceId), jsonString); + await File.WriteAllTextAsync(await BuildFileName(invoice.InvoiceId), jsonString); } #endregion - + #region Private Method BuildFileName - private static string BuildFileName(string invoiceId) => $"{DirectoryName}/Invoice_{invoiceId}.json"; + private static async Task BuildFileName(string invoiceId) + { + var targetDirectory = await GetDirectoryAsync(); + return Path.Combine(targetDirectory, $"Invoice_{invoiceId}.json"); + } #endregion @@ -114,5 +114,19 @@ namespace Server.Data } #endregion + + /// + /// Ensures that the directory for storing invoices exists asynchronously. + /// + /// A task representing the asynchronous operation to ensure the directory exists. + private static Task GetDirectoryAsync() + { + var parentDirectory = Directory.GetParent(Directory.GetCurrentDirectory()); + if (parentDirectory == null) return Task.FromResult(string.Empty); + var targetDirectory = Path.Combine(parentDirectory.FullName, DirectoryName); + + if(!Directory.Exists(targetDirectory)) Directory.CreateDirectory(targetDirectory); + return Task.FromResult(targetDirectory); + } } } \ No newline at end of file diff --git a/Server/Server.csproj b/Server/Server.csproj index 765cc18..fcd1e15 100644 --- a/Server/Server.csproj +++ b/Server/Server.csproj @@ -4,7 +4,6 @@ net8.0 enable enable - true