106 lines
2.9 KiB
C#
106 lines
2.9 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Serilog;
|
|
using TriliumMind.Data;
|
|
using TriliumMind.Data.Entities;
|
|
|
|
namespace TriliumMind.Services;
|
|
|
|
public class AppDbService
|
|
{
|
|
private readonly Serilog.ILogger _log;
|
|
private readonly IAppDbContext _db;
|
|
public AppDbService(IAppDbContext dbContext)
|
|
{
|
|
_log = Log.ForContext<AppDbService>();
|
|
_db = dbContext;
|
|
}
|
|
|
|
public async Task InitializeDatabaseAsync()
|
|
{
|
|
try
|
|
{
|
|
_log.Information("Checking database...");
|
|
|
|
// Create db if not exists
|
|
var created = await _db.Database.EnsureCreatedAsync();
|
|
if (created)
|
|
{
|
|
_log.Information("Database created successfully");
|
|
}
|
|
else
|
|
{
|
|
_log.Information("Database already exists");
|
|
}
|
|
|
|
// Migration
|
|
var pendingMigrations = await _db.Database.GetPendingMigrationsAsync();
|
|
if (pendingMigrations.Any())
|
|
{
|
|
_log.Information("Applying pending migrations...");
|
|
await _db.Database.MigrateAsync();
|
|
_log.Information("Migrations applied successfully");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_log.Error(ex, "Failed to initialize database");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public async Task<Dictionary<string, string>> LoadConfigAsync()
|
|
{
|
|
try
|
|
{
|
|
_log.Information("Loading configuration from database...");
|
|
var configs = await _db.Configs.ToDictionaryAsync(c => c.Key, c => c.Value);
|
|
_log.Information("Loaded {count} configuration entries", configs.Count);
|
|
return configs;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_log.Error(ex, "Failed to load configuration from database");
|
|
throw;
|
|
}
|
|
}
|
|
public async Task<string?> GetConfigAsync(string key)
|
|
{
|
|
try
|
|
{
|
|
var config = await _db.Configs.FindAsync(key);
|
|
return config?.Value;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_log.Error(ex, "Failed to get config value for key: {key}", key);
|
|
throw;
|
|
}
|
|
}
|
|
public async Task SetConfigAsync(string key, string value)
|
|
{
|
|
try
|
|
{
|
|
var config = await _db.Configs.FindAsync(key);
|
|
if (config == null)
|
|
{
|
|
config = new Config { Key = key, Value = value };
|
|
_db.Configs.Add(config);
|
|
}
|
|
else
|
|
{
|
|
config.Value = value;
|
|
_db.Configs.Update(config);
|
|
}
|
|
await _db.SaveChangesAsync();
|
|
_log.Debug("Config saved: {key} = {value}", key, value);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_log.Error(ex, "Failed to set config value for key: {key}", key);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
|
|
}
|