@page "/Settings/Database" @using LANCommander.SDK @using LANCommander.Server.Settings @using LANCommander.Server.Settings.Enums @using Microsoft.Data.Sqlite @using Microsoft.Extensions.Options @using MySqlConnector @using Npgsql @inject IOptions Settings @inject SettingsProvider SettingsProvider @inject SetupService SetupService @inject IMessageService MessageService @inject ILogger Logger @attribute [Authorize(Roles = RoleService.AdministratorRoleName)]
@code { bool Loading = false; void OnDatabaseProviderChanged() { switch (Settings.Value.Server.Database.Provider) { case DatabaseProvider.SQLite: var dbPath = AppPaths.GetConfigPath("LANCommander.db"); Settings.Value.Server.Database.ConnectionString = $"Data Source={dbPath};Cache=Shared"; break; case DatabaseProvider.MySQL: Settings.Value.Server.Database.ConnectionString = "Server=localhost;Uid=root;Pwd=password;Database=LANCommander"; break; case DatabaseProvider.PostgreSQL: Settings.Value.Server.Database.ConnectionString = "Host=localhost;Port=5432;Database=LANCommander;User Id=postgres;Password=password"; break; } } bool TestConnection() { try { SetupService.ValidateConnectionString(Settings.Value.Server.Database.Provider, Settings.Value.Server.Database.ConnectionString); return true; } catch (SqliteException ex) { Logger?.LogError(ex, "Could not use SQLite database connection"); MessageService.Error($"Could not use SQLite database: {ex.Message}", 10); } catch (MySqlException ex) { Logger?.LogError(ex, "Could not connect to MySQL database"); MessageService.Error($"Could not connect to MySQL database: {ex.Message}", 10); } catch (NpgsqlException ex) { Logger?.LogError(ex, "Could not connect to PostgreSQL database"); MessageService.Error($"Could not connect to PostgreSQL database: {ex.Message}", 10); } catch (Exception ex) { Logger?.LogError(ex, "An unknown error occurred trying to access the database"); MessageService.Error("Could not validate the connection string!"); } return false; } async Task TestDatabaseConnection() { Loading = true; StateHasChanged(); await Task.Yield(); if (TestConnection()) MessageService.Success("Database connection successful!"); Loading = false; } async Task ValidateDatabaseConnection() { Loading = true; StateHasChanged(); await Task.Yield(); if (TestConnection()) { try { await SetupService.ChangeProviderAsync(Settings.Value.Server.Database.Provider, Settings.Value.Server.Database.ConnectionString); SettingsProvider.Update(s => { s.Server.Database.Provider = Settings.Value.Server.Database.Provider; s.Server.Database.ConnectionString = Settings.Value.Server.Database.ConnectionString; }); MessageService.Success("Database settings saved!"); } catch (Exception ex) { Logger?.LogError(ex, "Could not initialize database!"); MessageService.Error($"Could not initialize database: {ex.Message}", 10); } } Loading = false; } }