Files
cfs-Tracker/Data/AppDatabase.cs
T
Djagor d85f2e0fb3 Farbpalette vorladen lassen
Bootstraper läd jetzt die farbeinstellung sofort und nicht nachträglich

cleanup and rework

PDF switch to HTML

export fix

Colorsystem

Farben für knöpfe hinzugefügt als anpassung

button color

button color primary and secondary change
2026-03-24 21:59:59 +01:00

74 lines
2.3 KiB
C#

using cfs_tracker.Models;
using cfs_tracker.Services;
using SQLite;
using System.Threading;
namespace cfs_tracker.Data;
public sealed class AppDatabase
{
private readonly DatabaseLocationService _databaseLocationService;
private readonly SemaphoreSlim _initLock = new(1, 1);
private SQLiteAsyncConnection? _connection;
private bool _initialized;
public AppDatabase(DatabaseLocationService databaseLocationService)
{
_databaseLocationService = databaseLocationService;
}
public async Task InitAsync()
{
if (_initialized)
return;
await _initLock.WaitAsync();
try
{
if (_initialized)
return;
var dbPath = _databaseLocationService.ResolveDatabasePath();
var directory = Path.GetDirectoryName(dbPath);
if (!string.IsNullOrWhiteSpace(directory))
Directory.CreateDirectory(directory);
_connection = new SQLiteAsyncConnection(dbPath);
await _connection.ExecuteAsync("PRAGMA foreign_keys = ON;");
await _connection.CreateTableAsync<ActivityItem>();
await _connection.CreateTableAsync<SymptomItem>();
await _connection.CreateTableAsync<HealthEntry>();
await _connection.CreateTableAsync<HealthEntrySymptom>();
await _connection.CreateTableAsync<AppearanceSetting>();
await _connection.CreateTableAsync<NotificationSchedule>();
await _connection.ExecuteAsync(@"
CREATE TABLE IF NOT EXISTS HealthEntrySymptom (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
HealthEntryId INTEGER NOT NULL,
SymptomItemId INTEGER NOT NULL
);");
await _connection.ExecuteAsync(
"CREATE INDEX IF NOT EXISTS IX_HealthEntrySymptom_HealthEntryId ON HealthEntrySymptom(HealthEntryId);");
await _connection.ExecuteAsync(
"CREATE INDEX IF NOT EXISTS IX_HealthEntrySymptom_SymptomItemId ON HealthEntrySymptom(SymptomItemId);");
_initialized = true;
}
finally
{
_initLock.Release();
}
}
public string CurrentPath => _databaseLocationService.ResolveDatabasePath();
public SQLiteAsyncConnection Connection =>
_connection ?? throw new InvalidOperationException("Database has not been initialized yet.");
}