Added database loading logic

This commit is contained in:
2025-01-14 14:05:15 +01:00
parent 313f6e046a
commit 6115dcf8e1
13 changed files with 139 additions and 35 deletions

View File

@@ -4,5 +4,6 @@ namespace HopFrame.Core.Services;
public interface IContextExplorer {
public IEnumerable<string> GetTableNames();
public TableConfig? GetTable(string name);
public TableConfig? GetTable(string tableName);
public ITableManager? GetTableManager(string tableName);
}

View File

@@ -0,0 +1,5 @@
namespace HopFrame.Core.Services;
public interface ITableManager {
public Task<IEnumerable<object>> LoadPage(int page, int perPage = 25);
}

View File

@@ -1,8 +1,9 @@
using HopFrame.Core.Config;
using Microsoft.EntityFrameworkCore;
namespace HopFrame.Core.Services.Implementations;
internal sealed class ContextExplorer(HopFrameConfig config) : IContextExplorer {
internal sealed class ContextExplorer(HopFrameConfig config, IServiceProvider provider) : IContextExplorer {
public IEnumerable<string> GetTableNames() {
foreach (var context in config.Contexts) {
foreach (var table in context.Tables) {
@@ -12,13 +13,28 @@ internal sealed class ContextExplorer(HopFrameConfig config) : IContextExplorer
}
}
public TableConfig? GetTable(string name) {
public TableConfig? GetTable(string tableName) {
foreach (var context in config.Contexts) {
var table = context.Tables.FirstOrDefault(table => table.PropertyName == name);
var table = context.Tables.FirstOrDefault(table => table.PropertyName == tableName);
if (table is not null)
return table;
}
return null;
}
public ITableManager? GetTableManager(string tableName) {
foreach (var context in config.Contexts) {
var table = context.Tables.FirstOrDefault(table => table.PropertyName == tableName);
if (table is null) continue;
var dbContext = provider.GetService(context.ContextType) as DbContext;
if (dbContext is null) return null;
var type = typeof(TableManager<>).MakeGenericType(table.TableType);
return Activator.CreateInstance(type, dbContext) as ITableManager;
}
return null;
}
}

View File

@@ -0,0 +1,15 @@
using Microsoft.EntityFrameworkCore;
namespace HopFrame.Core.Services.Implementations;
internal sealed class TableManager<TModel>(DbContext context) : ITableManager where TModel : class {
public async Task<IEnumerable<object>> LoadPage(int page, int perPage = 25) {
var table = context.Set<TModel>();
return await table
.Skip(page * perPage)
.Take(perPage)
.ToArrayAsync();
}
}

View File

@@ -1,5 +1,6 @@
@using HopFrame.Core.Config
@using HopFrame.Core.Services
@using Microsoft.Extensions.DependencyInjection
@inherits LayoutComponentBase
<link rel="stylesheet" type='text/css' href="https://cdn.jsdelivr.net/gh/devicons/devicon@latest/devicon.min.css" />
@@ -29,19 +30,16 @@
</FluentFooter>
</FluentLayout>
@inject IServiceProvider Provider
@inject IHopFrameAuthHandler Handler
@inject HopFrameConfig Config
@inject NavigationManager Navigator
@code {
protected override async Task OnInitializedAsync() {
if (!string.IsNullOrEmpty(Config.BasePolicy)) {
var handler = Provider.GetService(Config.AuthHandler!) as IHopFrameAuthHandler;
var authorized = await handler!.IsAuthenticatedAsync(Config.BasePolicy);
if (!authorized) {
Navigator.NavigateTo((Config.LoginPageRewrite ?? "/login") + "?redirect=" + Navigator.Uri);
}
var authorized = await Handler.IsAuthenticatedAsync(Config.BasePolicy);
if (!authorized) {
Navigator.NavigateTo((Config.LoginPageRewrite ?? "/login") + "?redirect=/" + Navigator.ToBaseRelativePath(Navigator.Uri), true);
}
}

View File

@@ -1,28 +1,28 @@
@using System.Text
@using HopFrame.Core.Config
@using HopFrame.Core.Services
<FluentHeader Class="hopframe-header">
<a href="/admin" style="text-decoration: none; color: @Color.Neutral.ToAttributeValue()">HopFrame</a>
<FluentSpacer/>
<a href="/admin" style="text-decoration: none; color: @Color.Neutral.ToAttributeValue();">HopFrame</a>
@if (Config.DisplayUserInfo) {
<FluentPersona Name="@_displayName" Initials="@_initials" ImageSize="32px" TextPosition="TextPosition.Start"/>
<FluentPersona Name="@_displayName" Initials="@_initials" ImageSize="32px" TextPosition="TextPosition.Start" Style="margin-left: auto"/>
}
</FluentHeader>
@inject HopFrameConfig Config
@inject IServiceProvider Provider
@inject IHopFrameAuthHandler Handler
@code {
private string? _displayName;
private string? _initials;
private string? _searchValue;
protected override async Task OnInitializedAsync() {
if (Config.DisplayUserInfo) {
var handler = Provider.GetService(Config.AuthHandler!) as IHopFrameAuthHandler;
_displayName = await handler!.GetCurrentUserDisplayNameAsync();
_displayName = await Handler.GetCurrentUserDisplayNameAsync();
_initials = GetInitials(_displayName);
}
}