126 lines
5.3 KiB
C#
126 lines
5.3 KiB
C#
using FinlyticAssets.Database;
|
|
using FinlyticAssets.Entities;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace FinlyticAssets.Services;
|
|
|
|
/// <summary>
|
|
/// Defines the business logic for managing global application settings.
|
|
/// Supports retrieving and updating (upserting) the central single-row configuration record.
|
|
/// </summary>
|
|
public interface ISettingsDbService
|
|
{
|
|
/// <summary>
|
|
/// Retrieves the current global settings from the database.
|
|
/// </summary>
|
|
/// <returns>
|
|
/// A task that represents the asynchronous operation. The task result contains the current <see cref="Settings"/>.
|
|
/// If no settings exist in the database yet, a new instance initialized with default values is returned.
|
|
/// </returns>
|
|
public Task<Settings> GetSettings();
|
|
|
|
/// <summary>
|
|
/// Persists the provided settings by updating the existing record or inserting the first one if the table is empty.
|
|
/// </summary>
|
|
/// <param name="settings">The new configuration values to be persisted.</param>
|
|
/// <returns>
|
|
/// A task that represents the asynchronous operation. The task result contains the freshly saved
|
|
/// and tracked <see cref="Settings"/> instance.
|
|
/// </returns>
|
|
public Task<Settings> SaveSettings(Settings settings);
|
|
|
|
/// <summary>
|
|
/// Updates settings from a key-value dictionary received via Admin Panel MQTT events.
|
|
/// </summary>
|
|
public Task UpdateSettingsFromDictionary(Dictionary<string, string> dictionary);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Implements the <see cref="ISettingsDbService"/> utilizing Entity Framework Core.
|
|
/// This service is designed for a single-row table architecture to maintain stateful global configurations.
|
|
/// </summary>
|
|
public class SettingsDbService : ISettingsDbService
|
|
{
|
|
private readonly AssetsDbContext _context;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="SettingsDbService"/> class with the required database context.
|
|
/// </summary>
|
|
/// <param name="context">The EF Core context used to access the assets database.</param>
|
|
public SettingsDbService(AssetsDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
/// <summary>Inherits documentation from interface.</summary>
|
|
public async Task<Settings> GetSettings()
|
|
{
|
|
var settings = await _context.Settings.AsNoTracking().FirstOrDefaultAsync();
|
|
|
|
if (settings == null)
|
|
{
|
|
settings = new Settings { Id = Guid.NewGuid() };
|
|
_context.Settings.Add(settings);
|
|
await _context.SaveChangesAsync();
|
|
_context.ChangeTracker.Clear();
|
|
}
|
|
|
|
return settings;
|
|
}
|
|
|
|
/// <summary>Inherits documentation from interface.</summary>
|
|
public async Task<Settings> SaveSettings(Settings settings)
|
|
{
|
|
var existing = await _context.Settings.FirstOrDefaultAsync();
|
|
|
|
if (existing == null)
|
|
{
|
|
if (settings.Id == Guid.Empty)
|
|
{
|
|
settings.Id = Guid.NewGuid();
|
|
}
|
|
_context.Settings.Add(settings);
|
|
await _context.SaveChangesAsync();
|
|
return settings;
|
|
}
|
|
else
|
|
{
|
|
existing.FinishedInitialScan = settings.FinishedInitialScan;
|
|
existing.TradeRepublicMaxRequestPageSize = settings.TradeRepublicMaxRequestPageSize;
|
|
existing.AssetUpdateTypeDelay = settings.AssetUpdateTypeDelay;
|
|
existing.InitAssetUpdateTypeDelay = settings.InitAssetUpdateTypeDelay;
|
|
existing.InitBatchAssetUpdateDelay = settings.InitBatchAssetUpdateDelay;
|
|
existing.BatchAssetUpdateDelay = settings.BatchAssetUpdateDelay;
|
|
existing.CurrentScanningType = settings.CurrentScanningType;
|
|
existing.CurrentScanningPage = settings.CurrentScanningPage;
|
|
_context.Settings.Update(existing);
|
|
await _context.SaveChangesAsync();
|
|
return existing;
|
|
}
|
|
}
|
|
|
|
/// <summary>Inherits documentation from interface.</summary>
|
|
public async Task UpdateSettingsFromDictionary(Dictionary<string, string> dictionary)
|
|
{
|
|
var settings = await GetSettings();
|
|
|
|
foreach (var (key, value) in dictionary)
|
|
{
|
|
if (string.Equals(key, "TradeRepublicMaxRequestPageSize", StringComparison.OrdinalIgnoreCase) && int.TryParse(value, out var ps))
|
|
settings.TradeRepublicMaxRequestPageSize = ps;
|
|
else if (string.Equals(key, "AssetUpdateTypeDelay", StringComparison.OrdinalIgnoreCase) && int.TryParse(value, out var autd))
|
|
settings.AssetUpdateTypeDelay = autd;
|
|
else if (string.Equals(key, "InitAssetUpdateTypeDelay", StringComparison.OrdinalIgnoreCase) && int.TryParse(value, out var iautd))
|
|
settings.InitAssetUpdateTypeDelay = iautd;
|
|
else if (string.Equals(key, "BatchAssetUpdateDelay", StringComparison.OrdinalIgnoreCase) && int.TryParse(value, out var baud))
|
|
settings.BatchAssetUpdateDelay = baud;
|
|
else if (string.Equals(key, "InitBatchAssetUpdateDelay", StringComparison.OrdinalIgnoreCase) && int.TryParse(value, out var ibaud))
|
|
settings.InitBatchAssetUpdateDelay = ibaud;
|
|
else if (string.Equals(key, "FinishedInitialScan", StringComparison.OrdinalIgnoreCase) && bool.TryParse(value, out var fis))
|
|
settings.FinishedInitialScan = fis;
|
|
}
|
|
|
|
await SaveSettings(settings);
|
|
}
|
|
}
|