From bae6120e3b832bc905df270503349dd9237190ff Mon Sep 17 00:00:00 2001 From: Shaun Walker Date: Tue, 11 May 2021 15:56:41 -0400 Subject: [PATCH] added DatabaseService to get list of database types from server --- Oqtane.Client/Installer/Installer.razor | 83 ++--- Oqtane.Client/Modules/Admin/Sites/Add.razor | 285 ++++++++---------- Oqtane.Client/Services/DatabaseService.cs | 28 ++ .../Services/Interfaces/IDatabaseService.cs | 11 + .../Controllers/DatabaseController.cs | 52 ++++ Oqtane.Server/Startup.cs | 3 +- 6 files changed, 243 insertions(+), 219 deletions(-) create mode 100644 Oqtane.Client/Services/DatabaseService.cs create mode 100644 Oqtane.Client/Services/Interfaces/IDatabaseService.cs create mode 100644 Oqtane.Server/Controllers/DatabaseController.cs diff --git a/Oqtane.Client/Installer/Installer.razor b/Oqtane.Client/Installer/Installer.razor index bebc148b..80d91d65 100644 --- a/Oqtane.Client/Installer/Installer.razor +++ b/Oqtane.Client/Installer/Installer.razor @@ -1,11 +1,10 @@ @namespace Oqtane.Installer @using Oqtane.Interfaces -@using Oqtane.Installer.Controls - @inject NavigationManager NavigationManager @inject IInstallationService InstallationService @inject ISiteService SiteService @inject IUserService UserService +@inject IDatabaseService DatabaseService @inject IJSRuntime JSRuntime @inject IStringLocalizer Localizer @@ -22,25 +21,28 @@

@Localizer["Database Configuration"]


- - - - - @{ - if (_databaseConfigType != null) - { - @DatabaseConfigComponent; + + + + + @{ + if (_databaseConfigType != null) + { + @DatabaseConfigComponent; + } } - }
- - - -
+ + + +
@@ -95,7 +97,7 @@ @code { - private IList _databases; + private List _databases; private string _databaseName = "LocalDB"; private Type _databaseConfigType; private object _databaseConfig; @@ -108,44 +110,9 @@ private string _message = string.Empty; private string _loadingDisplay = "display: none;"; - protected override void OnInitialized() + protected override async Task OnInitializedAsync() { - base.OnInitialized(); - - _databases = new List - { - new() - { - Name = "LocalDB", - FriendlyName = "Local Database", - Type = "Oqtane.Installer.Controls.LocalDBConfig, Oqtane.Client" - }, - new() - { - Name = "SqlServer", - FriendlyName = "SQL Server", - Type = "Oqtane.Installer.Controls.SqlServerConfig, Oqtane.Client" - }, - new() - { - Name = "Sqlite", - FriendlyName = "Sqlite", - Type = "Oqtane.Installer.Controls.SqliteConfig, Oqtane.Client" - }, - new() - { - Name = "MySQL", - FriendlyName = "MySQL", - Type = "Oqtane.Installer.Controls.MySQLConfig, Oqtane.Client" - }, - new() - { - Name = "PostgreSQL", - FriendlyName = "PostgreSQL", - Type = "Oqtane.Installer.Controls.PostgreSQLConfig, Oqtane.Client" - } - }; - + _databases = await DatabaseService.GetDatabasesAsync(); LoadDatabaseConfigComponent(); } diff --git a/Oqtane.Client/Modules/Admin/Sites/Add.razor b/Oqtane.Client/Modules/Admin/Sites/Add.razor index c2cb8549..c1b7cc1e 100644 --- a/Oqtane.Client/Modules/Admin/Sites/Add.razor +++ b/Oqtane.Client/Modules/Admin/Sites/Add.razor @@ -9,6 +9,7 @@ @inject ISiteTemplateService SiteTemplateService @inject IUserService UserService @inject IInstallationService InstallationService +@inject IDatabaseService DatabaseService @inject IStringLocalizer Localizer @inject IEnumerable Databases @@ -18,151 +19,151 @@ } else { - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @if (_tenantid == "+") - { +
- - - -
- - - -
- - - -
- - - -
- - - -
- - - -
- - - -
- + - if (_databaseConfigType != null) + + + + + + + + + + + + + + + + + @if (_tenantid == "+") { - @DatabaseConfigComponent; + + + + + + + + + + + + if (_databaseConfigType != null) + { + @DatabaseConfigComponent; + } + + + + + + + + } - - - - - - - - - } -
-
+
+ + +
- + - +
- + - + + @foreach (var theme in _themes) { - + }
+ + + +
+ + + +
+ + + +
+ + + +
+
+
+ + + +
+ + + +
+ + + +
+ + + +
- - - -
- - - -
+ @Localizer["Cancel"] } @code { - private IList _databases; + private List _databases; private string _databaseName = "LocalDB"; private Type _databaseConfigType; private object _databaseConfig; @@ -177,7 +178,7 @@ else private string _tenantid = "-"; private string _tenantName = string.Empty; - + private string _hostUserName = UserNames.Host; private string _hostpassword = string.Empty; @@ -197,44 +198,10 @@ else _themeList = await ThemeService.GetThemesAsync(); _themes = ThemeService.GetThemeControls(_themeList); _siteTemplates = await SiteTemplateService.GetSiteTemplatesAsync(); - - _databases = new List - { - new() - { - Name = "LocalDB", - FriendlyName = "Local Database", - Type = "Oqtane.Installer.Controls.LocalDBConfig, Oqtane.Client" - }, - new() - { - Name = "SqlServer", - FriendlyName = "SQL Server", - Type = "Oqtane.Installer.Controls.SqlServerConfig, Oqtane.Client" - }, - new() - { - Name = "Sqlite", - FriendlyName = "Sqlite", - Type = "Oqtane.Installer.Controls.SqliteConfig, Oqtane.Client" - }, - new() - { - Name = "MySQL", - FriendlyName = "MySQL", - Type = "Oqtane.Installer.Controls.MySQLConfig, Oqtane.Client" - }, - new() - { - Name = "PostgreSQL", - FriendlyName = "PostgreSQL", - Type = "Oqtane.Installer.Controls.PostGreSQLConfig, Oqtane.Client" - } - }; - + _databases = await DatabaseService.GetDatabasesAsync(); LoadDatabaseConfigComponent(); } - + private void DatabaseChanged(ChangeEventArgs eventArgs) { try @@ -365,12 +332,12 @@ else if (tenant != null) { config.TenantName = tenant.Name; - config.ConnectionString= tenant.DBConnectionString; + config.ConnectionString = tenant.DBConnectionString; config.IsNewTenant = false; } } - if (!string.IsNullOrEmpty(config.TenantName)) + if (!string.IsNullOrEmpty(config.TenantName)) { config.SiteName = _name; config.Aliases = _urls; diff --git a/Oqtane.Client/Services/DatabaseService.cs b/Oqtane.Client/Services/DatabaseService.cs new file mode 100644 index 00000000..db108d74 --- /dev/null +++ b/Oqtane.Client/Services/DatabaseService.cs @@ -0,0 +1,28 @@ +using Oqtane.Models; +using System.Threading.Tasks; +using System.Net.Http; +using System.Linq; +using System.Collections.Generic; +using Oqtane.Shared; + +namespace Oqtane.Services +{ + public class DatabaseService : ServiceBase, IDatabaseService + { + + private readonly SiteState _siteState; + + public DatabaseService(HttpClient http, SiteState siteState) : base(http) + { + _siteState = siteState; + } + + private string Apiurl => CreateApiUrl("Database", _siteState.Alias); + + public async Task> GetDatabasesAsync() + { + List databases = await GetJsonAsync>(Apiurl); + return databases.OrderBy(item => item.FriendlyName).ToList(); + } + } +} diff --git a/Oqtane.Client/Services/Interfaces/IDatabaseService.cs b/Oqtane.Client/Services/Interfaces/IDatabaseService.cs new file mode 100644 index 00000000..9d6b8bf0 --- /dev/null +++ b/Oqtane.Client/Services/Interfaces/IDatabaseService.cs @@ -0,0 +1,11 @@ +using Oqtane.Models; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace Oqtane.Services +{ + public interface IDatabaseService + { + Task> GetDatabasesAsync(); + } +} diff --git a/Oqtane.Server/Controllers/DatabaseController.cs b/Oqtane.Server/Controllers/DatabaseController.cs new file mode 100644 index 00000000..418a5885 --- /dev/null +++ b/Oqtane.Server/Controllers/DatabaseController.cs @@ -0,0 +1,52 @@ +using System.Collections.Generic; +using Microsoft.AspNetCore.Mvc; +using Oqtane.Shared; + +namespace Oqtane.Controllers +{ + [Route(ControllerRoutes.ApiRoute)] + public class DatabaseController : Controller + { + public DatabaseController() { } + + // GET: api/ + [HttpGet] + public IEnumerable Get() + { + var databases = new List + { + new() + { + Name = "LocalDB", + FriendlyName = "Local Database", + Type = "Oqtane.Installer.Controls.LocalDBConfig, Oqtane.Client" + }, + new() + { + Name = "SqlServer", + FriendlyName = "SQL Server", + Type = "Oqtane.Installer.Controls.SqlServerConfig, Oqtane.Client" + }, + new() + { + Name = "Sqlite", + FriendlyName = "Sqlite", + Type = "Oqtane.Installer.Controls.SqliteConfig, Oqtane.Client" + }, + new() + { + Name = "MySQL", + FriendlyName = "MySQL", + Type = "Oqtane.Installer.Controls.MySQLConfig, Oqtane.Client" + }, + new() + { + Name = "PostgreSQL", + FriendlyName = "PostgreSQL", + Type = "Oqtane.Installer.Controls.PostGreSQLConfig, Oqtane.Client" + } + }; + return databases; + } + } +} diff --git a/Oqtane.Server/Startup.cs b/Oqtane.Server/Startup.cs index 5bfbd329..f33a6e91 100644 --- a/Oqtane.Server/Startup.cs +++ b/Oqtane.Server/Startup.cs @@ -1,5 +1,4 @@ using System; -using System.Collections; using System.IO; using System.Linq; using System.Net.Http; @@ -10,7 +9,6 @@ using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Identity; -using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -126,6 +124,7 @@ namespace Oqtane services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); services.AddSingleton();