Implement backup and restore functionality; add IBackupService and BackupService; refactor services to use DbContextFactory

This commit is contained in:
2026-04-03 10:53:53 +02:00
parent 0923c037eb
commit 387c18e834
15 changed files with 411 additions and 71 deletions
+54
View File
@@ -1,6 +1,9 @@
@page "/"
@inject IAccountService AccountService
@inject IBackupService BackupService
@inject ISettingsService SettingsService
@inject IJSRuntime JsRuntime
@inject NavigationManager NavigationManager
<div class="container-fluid">
@@ -19,8 +22,22 @@
<button class="btn-nav btn-primary" @onclick="() => showAddAccount = true">
<i class="bi bi-plus-lg"></i> Neues Konto
</button>
|
<button class="btn-nav btn-success" @onclick="HandleBackupAsync">
<i class="bi bi-save"></i> Backup
</button>
<button class="btn-nav btn-warning" @onclick="HandleRestoreAsync">
<i class="bi bi-arrow-counterclockwise"></i> Restore
</button>
</div>
@if (!string.IsNullOrWhiteSpace(operationMessage))
{
<div class="alert @operationMessageClass mb-3" role="alert">
@operationMessage
</div>
}
@if (accounts == null)
{
<div class="text-center py-5">
@@ -56,6 +73,8 @@
private bool showAddAccount;
private bool showEditClubName;
private string clubName = string.Empty;
private string? operationMessage;
private string operationMessageClass = "alert-info";
private string DisplayClubName => string.IsNullOrWhiteSpace(clubName) ? "Mein Verein" : clubName;
protected override async Task OnInitializedAsync()
@@ -87,4 +106,39 @@
showEditClubName = false;
await LoadClubName();
}
private async Task HandleBackupAsync()
{
var message = await BackupService.CreateBackupAsync();
if(message.Contains("fehlgeschlagen", StringComparison.OrdinalIgnoreCase)) {
SetOperationMessage(message, false);
}
}
private async Task HandleRestoreAsync()
{
var confirmed = await JsRuntime.InvokeAsync<bool>("confirm",
"Restore überschreibt die aktuelle Datenbank. Möchten Sie fortfahren?");
if (!confirmed) return;
var message = await BackupService.RestoreBackupAsync();
var isSuccess = message.StartsWith("Wiederherstellung erfolgreich", StringComparison.OrdinalIgnoreCase);
if (isSuccess)
{
// Reload the Blazor app so all components/services re-query from restored DB.
NavigationManager.NavigateTo("/", forceLoad: true);
} else {
SetOperationMessage(message, isSuccess);
}
}
private void SetOperationMessage(string message, bool success)
{
operationMessage = message;
operationMessageClass = success ? "alert-success" : "alert-danger";
}
}