Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor InstallController and add DatabaseConfigured attribute #532

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 10 additions & 19 deletions src/Modules/Grand.Module.Installer/Controllers/InstallController.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Grand.Data;
using Grand.Infrastructure.Caching;
using Grand.Infrastructure.Configuration;
using Grand.Infrastructure.Migrations;
using Grand.Infrastructure.Plugins;
using Grand.Module.Installer.Filters;
using Grand.Module.Installer.Interfaces;
using Grand.Module.Installer.Models;
using Grand.SharedKernel.Extensions;
Expand All @@ -16,6 +16,7 @@

namespace Grand.Module.Installer.Controllers;

[DatabaseConfigured]
public class InstallController : Controller
{

Expand All @@ -24,8 +25,6 @@ public class InstallController : Controller
private readonly ICacheBase _cacheBase;
private readonly IHostApplicationLifetime _applicationLifetime;
private readonly DatabaseConfig _dbConfig;
private readonly IDatabaseFactoryContext _databaseFactory;
private readonly IInstallationLocalizedService _installationLocalizedService;
private readonly ILogger<InstallController> _logger;

/// <summary>
Expand All @@ -40,16 +39,12 @@ public class InstallController : Controller
public InstallController(
ICacheBase cacheBase,
IHostApplicationLifetime applicationLifetime,
IDatabaseFactoryContext databaseFactory,
IInstallationLocalizedService installationLocalizedService,
DatabaseConfig dbConfig,
ILogger<InstallController> logger)
{
_cacheBase = cacheBase;
_applicationLifetime = applicationLifetime;
_installationLocalizedService = installationLocalizedService;
_dbConfig = dbConfig;
_databaseFactory = databaseFactory;
_logger = logger;
}

Expand All @@ -70,6 +65,8 @@ public InstallController(

private InstallModel PrepareModel(InstallModel? model)
{
var installationLocalizedService = HttpContext.RequestServices.GetRequiredService<IInstallationLocalizedService>();

model ??= new InstallModel {
AdminEmail = "[email protected]",
InstallSampleData = false,
Expand All @@ -85,10 +82,10 @@ private InstallModel PrepareModel(InstallModel? model)

model.SelectedLanguage = GetLanguage();

foreach (var lang in _installationLocalizedService.GetAvailableLanguages())
foreach (var lang in installationLocalizedService.GetAvailableLanguages())
{
var selected = false;
if (_installationLocalizedService.GetCurrentLanguage(model.SelectedLanguage).Code == lang.Code)
if (installationLocalizedService.GetCurrentLanguage(model.SelectedLanguage).Code == lang.Code)
{
selected = true;
model.SelectedLanguage = lang.Code;
Expand All @@ -102,21 +99,18 @@ private InstallModel PrepareModel(InstallModel? model)
}

//prepare collation list
foreach (var col in _installationLocalizedService.GetAvailableCollations())
foreach (var col in installationLocalizedService.GetAvailableCollations())
model.AvailableCollation.Add(new SelectListItem {
Value = col.Value,
Text = col.Name,
Selected = _installationLocalizedService.GetCurrentLanguage().Code == col.Value
Selected = installationLocalizedService.GetCurrentLanguage().Code == col.Value
});

return model;
}

public virtual async Task<IActionResult> Index()
{
if (DataSettingsManager.DatabaseIsInstalled())
return RedirectToRoute("HomePage");

{
var installed = await _cacheBase.GetAsync("Installed", async () => await Task.FromResult(false));
return View(installed ? new InstallModel { Installed = true, AdminEmail = "", AdminPassword = "", ConfirmPassword = "" } : PrepareModel(null));
}
Expand Down Expand Up @@ -185,7 +179,7 @@ protected async Task CheckConnectionString(IInstallationLocalizedService locServ
{
if (model.DataProvider != DbProvider.LiteDB)
{
var mdb = _databaseFactory.GetDatabaseContext(connectionString, DbProvider.MongoDB);
var mdb = HttpContext.RequestServices.GetRequiredService<IDatabaseFactoryContext>().GetDatabaseContext(connectionString, DbProvider.MongoDB);
if (await mdb.DatabaseExist())
ModelState.AddModelError("",
locService.GetResource(model.SelectedLanguage, "AlreadyInstalled"));
Expand All @@ -202,9 +196,6 @@ protected async Task CheckConnectionString(IInstallationLocalizedService locServ
[HttpPost]
public virtual async Task<IActionResult> Index(InstallModel model)
{
if (DataSettingsManager.DatabaseIsInstalled())
return RedirectToRoute("HomePage");

var installed = await _cacheBase.GetAsync("Installed", async () => await Task.FromResult(false));
if (installed)
return View(new InstallModel { Installed = true, AdminEmail = "", AdminPassword = "", ConfirmPassword = "" });
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Grand.Data;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace Grand.Module.Installer.Filters;

public class DatabaseConfiguredAttribute : Attribute, IAsyncActionFilter
{
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
if (!DataSettingsManager.DatabaseIsInstalled())
{
await next();
}
else
{
context.Result = new NotFoundResult();
}
}
}