Added database loading logic
This commit is contained in:
@@ -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);
|
||||
}
|
||||
5
src/HopFrame.Core/Services/ITableManager.cs
Normal file
5
src/HopFrame.Core/Services/ITableManager.cs
Normal file
@@ -0,0 +1,5 @@
|
||||
namespace HopFrame.Core.Services;
|
||||
|
||||
public interface ITableManager {
|
||||
public Task<IEnumerable<object>> LoadPage(int page, int perPage = 25);
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
15
src/HopFrame.Core/Services/Implementations/TableManager.cs
Normal file
15
src/HopFrame.Core/Services/Implementations/TableManager.cs
Normal 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();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user