Added relation support
This commit is contained in:
@@ -5,5 +5,6 @@ namespace HopFrame.Core.Services;
|
||||
public interface IContextExplorer {
|
||||
public IEnumerable<string> GetTableNames();
|
||||
public TableConfig? GetTable(string tableName);
|
||||
public TableConfig? GetTable(Type tableEntity);
|
||||
public ITableManager? GetTableManager(string tableName);
|
||||
}
|
||||
@@ -1,8 +1,13 @@
|
||||
namespace HopFrame.Core.Services;
|
||||
using System.Reflection;
|
||||
using HopFrame.Core.Config;
|
||||
|
||||
namespace HopFrame.Core.Services;
|
||||
|
||||
public interface ITableManager {
|
||||
public IQueryable<object> LoadPage(int page, int perPage = 20);
|
||||
public (IEnumerable<object>, int) Search(string searchTerm, int page = 0, int perPage = 20);
|
||||
public int TotalPages(int perPage = 20);
|
||||
public Task DeleteItem(object item);
|
||||
|
||||
public string DisplayProperty(object item, PropertyInfo info, TableConfig? tableConfig);
|
||||
}
|
||||
@@ -23,6 +23,16 @@ internal sealed class ContextExplorer(HopFrameConfig config, IServiceProvider pr
|
||||
return null;
|
||||
}
|
||||
|
||||
public TableConfig? GetTable(Type tableEntity) {
|
||||
foreach (var context in config.Contexts) {
|
||||
var table = context.Tables.FirstOrDefault(table => table.TableType == tableEntity);
|
||||
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);
|
||||
@@ -32,9 +42,10 @@ internal sealed class ContextExplorer(HopFrameConfig config, IServiceProvider pr
|
||||
if (dbContext is null) return null;
|
||||
|
||||
var type = typeof(TableManager<>).MakeGenericType(table.TableType);
|
||||
return Activator.CreateInstance(type, dbContext, table) as ITableManager;
|
||||
return Activator.CreateInstance(type, dbContext, table, this) as ITableManager;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,20 +1,23 @@
|
||||
using HopFrame.Core.Config;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Reflection;
|
||||
using HopFrame.Core.Config;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace HopFrame.Core.Services.Implementations;
|
||||
|
||||
internal sealed class TableManager<TModel>(DbContext context, TableConfig config) : ITableManager where TModel : class {
|
||||
internal sealed class TableManager<TModel>(DbContext context, TableConfig config, IContextExplorer explorer) : ITableManager where TModel : class {
|
||||
|
||||
public IQueryable<object> LoadPage(int page, int perPage = 20) {
|
||||
var table = context.Set<TModel>();
|
||||
return table
|
||||
var data = IncludeForgeinKeys(table);
|
||||
return data
|
||||
.Skip(page * perPage)
|
||||
.Take(perPage);
|
||||
}
|
||||
|
||||
public (IEnumerable<object>, int) Search(string searchTerm, int page = 0, int perPage = 20) {
|
||||
var table = context.Set<TModel>();
|
||||
var all = table
|
||||
var all = IncludeForgeinKeys(table)
|
||||
.AsEnumerable()
|
||||
.Where(item => ItemSearched(item, searchTerm))
|
||||
.ToList();
|
||||
@@ -46,4 +49,34 @@ internal sealed class TableManager<TModel>(DbContext context, TableConfig config
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public string DisplayProperty(object item, PropertyInfo info, TableConfig? tableConfig) {
|
||||
if (item is null) return string.Empty;
|
||||
var prop = tableConfig?.Properties.Find(prop => prop.Info.Name == info.Name);
|
||||
if (prop is null) return item.ToString() ?? string.Empty;
|
||||
|
||||
var propValue = prop.Info.GetValue(item);
|
||||
if (propValue is null || prop.DisplayedProperty is null)
|
||||
return propValue?.ToString() ?? string.Empty;
|
||||
|
||||
var innerConfig = explorer.GetTable(propValue.GetType());
|
||||
return DisplayProperty(propValue, prop.DisplayedProperty, innerConfig);
|
||||
}
|
||||
|
||||
private IQueryable<TModel> IncludeForgeinKeys(IQueryable<TModel> query) {
|
||||
var pendingQuery = query;
|
||||
|
||||
foreach (var property in config.Properties) {
|
||||
var attr = property.Info
|
||||
.GetCustomAttributes(true)
|
||||
.FirstOrDefault(att => att is ForeignKeyAttribute) as ForeignKeyAttribute;
|
||||
|
||||
if (attr is null) continue;
|
||||
|
||||
pendingQuery = pendingQuery.Include(property.Info.Name);
|
||||
}
|
||||
|
||||
return pendingQuery;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user