Skip to content

Commit 4494513

Browse files
Refactor InvokeAsync method and clean up directives
1 parent 4faf947 commit 4494513

File tree

1 file changed

+44
-54
lines changed

1 file changed

+44
-54
lines changed
Lines changed: 44 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#nullable enable
2-
3-
using Grand.Data;
1+
using Grand.Data;
42
using Grand.Domain.Common;
53
using Grand.Infrastructure;
64
using Grand.Infrastructure.Caching;
@@ -18,7 +16,6 @@ public class InstallUrlMiddleware
1816

1917
private readonly RequestDelegate _next;
2018
private readonly ICacheBase _cacheBase;
21-
private const string InstallUrl = "/install";
2219

2320
#endregion
2421

@@ -34,69 +31,62 @@ public InstallUrlMiddleware(RequestDelegate next, ICacheBase cacheBase)
3431

3532
#region Methods
3633

34+
/// <summary>
35+
/// Invoke middleware actions
36+
/// </summary>
37+
/// <param name="context">HTTP context</param>
38+
/// <returns>Task</returns>
3739
public async Task InvokeAsync(HttpContext context)
3840
{
39-
if (!DataSettingsManager.DatabaseIsInstalled())
41+
var databaseIsInstalled = DataSettingsManager.DatabaseIsInstalled();
42+
const string installUrl = "/install";
43+
//whether database is installed
44+
var version = _cacheBase.Get(CacheKey.GRAND_NODE_VERSION, () =>
4045
{
41-
await HandleInstallationAsync(context);
42-
return;
43-
}
46+
if (databaseIsInstalled)
47+
return context.RequestServices.GetRequiredService<IRepository<GrandNodeVersion>>().Table.FirstOrDefault();
4448

45-
var version = GetDatabaseVersion(context);
49+
return null;
50+
}, int.MaxValue);
4651

4752
if (version == null)
4853
{
49-
await HandleInstallationAsync(context);
50-
return;
51-
}
52-
53-
if (!version.DataBaseVersion.Equals(GrandVersion.SupportedDBVersion))
54-
{
55-
await context.Response.WriteAsync($"The database version is not supported in this software version. Supported version: {GrandVersion.SupportedDBVersion}, your version: {version.DataBaseVersion}");
56-
return;
54+
var featureManager = context.RequestServices.GetRequiredService<IFeatureManager>();
55+
var isInstallerModuleEnabled = await featureManager.IsEnabledAsync("Grand.Module.Installer");
56+
if (!isInstallerModuleEnabled)
57+
{
58+
// Return a response indicating the installer module is not enabled
59+
context.Response.StatusCode = StatusCodes.Status403Forbidden;
60+
await context.Response.WriteAsync("The installation module is not enabled.");
61+
return;
62+
}
63+
64+
if (!context.Request.GetEncodedPathAndQuery().StartsWith(installUrl, StringComparison.OrdinalIgnoreCase))
65+
{
66+
//redirect
67+
context.Response.Redirect(installUrl);
68+
return;
69+
}
5770
}
58-
59-
if (IsInstallUrl(context.Request))
71+
else
6072
{
61-
context.Response.Redirect("/");
62-
return;
73+
if (!version.DataBaseVersion.Equals(GrandVersion.SupportedDBVersion))
74+
{
75+
await context.Response.WriteAsync("The database version is not supported in this software version. " +
76+
$"Supported version: {GrandVersion.SupportedDBVersion} , your version: {version.DataBaseVersion}");
77+
return;
78+
}
79+
if (context.Request.GetEncodedPathAndQuery().StartsWith(installUrl, StringComparison.OrdinalIgnoreCase))
80+
{
81+
//redirect
82+
context.Response.Redirect("/");
83+
return;
84+
}
6385
}
6486

65-
// Call the next middleware in the pipeline
87+
//call the next middleware in the request pipeline
6688
await _next(context);
6789
}
6890

69-
private async Task HandleInstallationAsync(HttpContext context)
70-
{
71-
var featureManager = context.RequestServices.GetRequiredService<IFeatureManager>();
72-
var isInstallerModuleEnabled = await featureManager.IsEnabledAsync("Grand.Module.Installer");
73-
74-
if (!isInstallerModuleEnabled)
75-
{
76-
context.Response.StatusCode = StatusCodes.Status403Forbidden;
77-
await context.Response.WriteAsync("The installation module is not enabled.");
78-
return;
79-
}
80-
81-
if (!IsInstallUrl(context.Request))
82-
{
83-
context.Response.Redirect(InstallUrl);
84-
}
85-
}
86-
87-
private GrandNodeVersion? GetDatabaseVersion(HttpContext context)
88-
{
89-
return _cacheBase.Get(CacheKey.GRAND_NODE_VERSION, () =>
90-
{
91-
var repository = context.RequestServices.GetRequiredService<IRepository<GrandNodeVersion>>();
92-
return repository.Table.FirstOrDefault();
93-
}, int.MaxValue);
94-
}
95-
96-
private bool IsInstallUrl(HttpRequest request)
97-
{
98-
return request.GetEncodedPathAndQuery().StartsWith(InstallUrl, StringComparison.OrdinalIgnoreCase);
99-
}
100-
10191
#endregion
10292
}

0 commit comments

Comments
 (0)