Init
This commit is contained in:
58
Server/Data/CustomerData.cs
Normal file
58
Server/Data/CustomerData.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Server.Model;
|
||||
|
||||
namespace Server.Data
|
||||
{
|
||||
public class CustomerData
|
||||
{
|
||||
#region Public Properties
|
||||
|
||||
public List<Address> Customers { get; set; } = [];
|
||||
|
||||
public static CustomerData Instance { get; set; } = new();
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Properties
|
||||
|
||||
private static string FileName => "Customers.json";
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Method LoadAsync
|
||||
|
||||
/// <summary>
|
||||
/// Loads customer data asynchronously from a JSON file if it exists.
|
||||
/// </summary>
|
||||
/// <return>
|
||||
/// A Task representing the asynchronous operation.
|
||||
/// </return>
|
||||
public static async Task LoadAsync()
|
||||
{
|
||||
if (!File.Exists(FileName)) return;
|
||||
var jsonString = await File.ReadAllTextAsync(FileName);
|
||||
var deserialized = JsonSerializer.Deserialize<CustomerData>(jsonString)!;
|
||||
|
||||
Instance = deserialized;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Method SaveAsync
|
||||
|
||||
/// <summary>
|
||||
/// Saves the current customer data asynchronously to a JSON file.
|
||||
/// </summary>
|
||||
/// <return>
|
||||
/// A Task representing the asynchronous operation.
|
||||
/// </return>
|
||||
public static async Task SaveAsync()
|
||||
{
|
||||
var jsonString = JsonSerializer.Serialize(Instance, new JsonSerializerOptions { WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull });
|
||||
await File.WriteAllTextAsync(FileName, jsonString);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
118
Server/Data/InvoiceData.cs
Normal file
118
Server/Data/InvoiceData.cs
Normal file
@@ -0,0 +1,118 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Server.Model;
|
||||
|
||||
namespace Server.Data
|
||||
{
|
||||
public static class InvoiceData
|
||||
{
|
||||
#region Private Properties
|
||||
|
||||
private static string DirectoryName => "invoices";
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Method DeleteAsync
|
||||
|
||||
/// <summary>
|
||||
/// Deletes an invoice file asynchronously by its ID.
|
||||
/// </summary>
|
||||
/// <param name="invoiceId">The ID of the invoice to be deleted.</param>
|
||||
/// <return>A task representing the asynchronous delete operation.</return>
|
||||
public static Task DeleteAsync(string invoiceId)
|
||||
{
|
||||
var fileName = BuildFileName(invoiceId);
|
||||
if (File.Exists(fileName)) File.Delete(fileName);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Method ExistsAsync
|
||||
|
||||
/// <summary>
|
||||
/// Checks if an invoice file exists asynchronously by its ID.
|
||||
/// </summary>
|
||||
/// <param name="invoiceId">The ID of the invoice to check for existence.</param>
|
||||
/// <returns>A task representing the asynchronous check operation, with a boolean result indicating whether the file exists.</returns>
|
||||
public static Task<bool> ExistsAsync(string invoiceId) => Task.FromResult(File.Exists(BuildFileName(invoiceId)));
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Method LoadAllAsync
|
||||
|
||||
/// <summary>
|
||||
/// Loads all invoices asynchronously.
|
||||
/// </summary>
|
||||
/// <return>A task representing the asynchronous operation, with a list of <see cref="InvoiceModel" /> as the result.</return>
|
||||
public static async Task<List<InvoiceModel>> LoadAllAsync()
|
||||
{
|
||||
var invoices = new List<InvoiceModel>();
|
||||
|
||||
var invoiceFiles = Directory.GetFiles(DirectoryName, "Invoice_*.json");
|
||||
foreach (var file in invoiceFiles)
|
||||
{
|
||||
var invoice = await LoadFileAsync(file);
|
||||
if (invoice != null) invoices.Add(invoice);
|
||||
}
|
||||
|
||||
return invoices;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Method LoadAsync
|
||||
|
||||
/// <summary>
|
||||
/// Loads an invoice asynchronously by its ID.
|
||||
/// </summary>
|
||||
/// <param name="invoiceId">The ID of the invoice to be loaded.</param>
|
||||
/// <return>The deserialized <see cref="InvoiceModel" /> instance if the file exists; otherwise, null.</return>
|
||||
public static async Task<InvoiceModel?> LoadAsync(string invoiceId)
|
||||
{
|
||||
var fileName = BuildFileName(invoiceId);
|
||||
return await LoadFileAsync(fileName);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Method SaveAsync
|
||||
|
||||
/// <summary>
|
||||
/// Saves an invoice asynchronously.
|
||||
/// </summary>
|
||||
/// <param name="invoice">The invoice model to be saved.</param>
|
||||
/// <return>A task representing the asynchronous save operation.</return>
|
||||
public static async Task SaveAsync(InvoiceModel invoice)
|
||||
{
|
||||
// Ensure id is set
|
||||
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);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Method BuildFileName
|
||||
|
||||
private static string BuildFileName(string invoiceId) => $"{DirectoryName}/Invoice_{invoiceId}.json";
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Method LoadFileAsync
|
||||
|
||||
private static async Task<InvoiceModel?> LoadFileAsync(string fileName)
|
||||
{
|
||||
if (!File.Exists(fileName)) return null;
|
||||
var jsonString = await File.ReadAllTextAsync(fileName);
|
||||
return JsonSerializer.Deserialize<InvoiceModel>(jsonString)!;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
58
Server/Data/SettingsData.cs
Normal file
58
Server/Data/SettingsData.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user