Restructure solution into folders and fix persistence issues
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
37987a394a
commit
7c39cb5562
62 files changed
+219
-151
No files matched your search
@@ -0,0 +1,52 @@
|
||||
using Flypaper.Core.Settings;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Flypaper.Storage.Settings;
|
||||
|
||||
internal sealed class DbConfigurationProvider(string connectionString) : ConfigurationProvider
|
||||
{
|
||||
public override void Load() => Data = TryRead() ?? new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
public bool TryReload()
|
||||
{
|
||||
if (TryRead() is not { } data)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Data = data;
|
||||
OnReload();
|
||||
return true;
|
||||
}
|
||||
|
||||
private Dictionary<string, string?>? TryRead()
|
||||
{
|
||||
var builder = new SqliteConnectionStringBuilder(connectionString) { Mode = SqliteOpenMode.ReadOnly };
|
||||
|
||||
try
|
||||
{
|
||||
using var connection = new SqliteConnection(builder.ConnectionString);
|
||||
connection.Open();
|
||||
using var command = connection.CreateCommand();
|
||||
command.CommandText = "SELECT \"Key\", \"Value\" FROM \"Settings\"";
|
||||
using var reader = command.ExecuteReader();
|
||||
|
||||
var data = new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase);
|
||||
while (reader.Read())
|
||||
{
|
||||
var key = reader.GetString(0);
|
||||
if (SettingKeys.All.Contains(key))
|
||||
{
|
||||
data[key] = reader.GetString(1);
|
||||
}
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
catch (SqliteException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Flypaper.Storage.Settings;
|
||||
|
||||
public sealed class DbConfigurationSource(string connectionString) : IConfigurationSource
|
||||
{
|
||||
private DbConfigurationProvider? _provider;
|
||||
|
||||
public IConfigurationProvider Build(IConfigurationBuilder builder) =>
|
||||
_provider = new DbConfigurationProvider(connectionString);
|
||||
|
||||
public bool TryReload() => _provider?.TryReload() ?? false;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using Flypaper.Core.Settings;
|
||||
using Flypaper.Storage.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Flypaper.Storage.Settings;
|
||||
|
||||
public sealed partial class RuntimeSettingsStore(
|
||||
IDbContextFactory<FlypaperDbContext> dbContextFactory,
|
||||
DbConfigurationSource source,
|
||||
ILogger<RuntimeSettingsStore> logger)
|
||||
{
|
||||
public async Task SaveAsync(RuntimeSettings settings, CancellationToken ct = default)
|
||||
{
|
||||
await using var db = await dbContextFactory.CreateDbContextAsync(ct);
|
||||
await using var transaction = await db.Database.BeginTransactionAsync(ct);
|
||||
await db.Settings.ExecuteDeleteAsync(ct);
|
||||
db.Settings.AddRange(settings.ToConfiguration().Select(pair => new SettingEntity { Key = pair.Key, Value = pair.Value }));
|
||||
await db.SaveChangesAsync(ct);
|
||||
await transaction.CommitAsync(ct);
|
||||
Reload();
|
||||
}
|
||||
|
||||
public async Task ResetAsync(CancellationToken ct = default)
|
||||
{
|
||||
await using var db = await dbContextFactory.CreateDbContextAsync(ct);
|
||||
await db.Settings.ExecuteDeleteAsync(ct);
|
||||
Reload();
|
||||
}
|
||||
|
||||
public async Task<bool> HasOverridesAsync(CancellationToken ct = default)
|
||||
{
|
||||
await using var db = await dbContextFactory.CreateDbContextAsync(ct);
|
||||
return await db.Settings.AnyAsync(ct);
|
||||
}
|
||||
|
||||
private void Reload()
|
||||
{
|
||||
if (!source.TryReload())
|
||||
{
|
||||
LogReloadFailed();
|
||||
}
|
||||
}
|
||||
|
||||
[LoggerMessage(Level = LogLevel.Warning, Message = "Settings were saved but could not be reloaded from the database; previous values stay active")]
|
||||
private partial void LogReloadFailed();
|
||||
}
|
||||
Reference in new issue
Block a user