Added edit modal

This commit is contained in:
Leon Hoppe
2025-01-15 14:58:15 +01:00
parent ad4d9c65d6
commit d4018cceec
12 changed files with 179 additions and 19 deletions

View File

@@ -10,6 +10,9 @@ public class PropertyConfig(PropertyInfo info) {
public bool Sortable { get; set; } = true;
public bool Searchable { get; set; } = true;
public PropertyInfo? DisplayedProperty { get; set; }
public Func<object, string>? Formatter { get; set; }
public bool Editable { get; set; } = true;
public bool Creatable { get; set; } = true;
}
public class PropertyConfig<TProp>(PropertyConfig config) {
@@ -39,5 +42,20 @@ public class PropertyConfig<TProp>(PropertyConfig config) {
config.DisplayedProperty = TableConfig<TProp>.GetPropertyInfo(propertyExpression);
return this;
}
public PropertyConfig<TProp> Format(Func<TProp, string> formatter) {
config.Formatter = obj => formatter.Invoke((TProp)obj);
return this;
}
public PropertyConfig<TProp> Editable(bool editable) {
config.Editable = editable;
return this;
}
public PropertyConfig<TProp> Creatable(bool creatable) {
config.Creatable = creatable;
return this;
}
}

View File

@@ -1,4 +1,6 @@
using System.Linq.Expressions;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq.Expressions;
using System.Reflection;
namespace HopFrame.Core.Config;
@@ -17,7 +19,18 @@ public class TableConfig {
ContextConfig = config;
foreach (var info in tableType.GetProperties()) {
Properties.Add(new PropertyConfig(info));
var propConfig = new PropertyConfig(info);
if (info.GetCustomAttributes(true).Any(a => a is DatabaseGeneratedAttribute)) {
propConfig.Creatable = false;
propConfig.Editable = false;
}
if (info.GetCustomAttributes(true).Any(a => a is KeyAttribute)) {
propConfig.Editable = false;
}
Properties.Add(propConfig);
}
}
}

View File

@@ -9,5 +9,5 @@ public interface ITableManager {
public int TotalPages(int perPage = 20);
public Task DeleteItem(object item);
public string DisplayProperty(object item, PropertyInfo info, TableConfig? tableConfig);
public string DisplayProperty(object? item, PropertyInfo info, TableConfig? tableConfig);
}

View File

@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Reflection;
using HopFrame.Core.Config;
using Microsoft.EntityFrameworkCore;
@@ -50,14 +51,27 @@ internal sealed class TableManager<TModel>(DbContext context, TableConfig config
return false;
}
public string DisplayProperty(object item, PropertyInfo info, TableConfig? tableConfig) {
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;
if (propValue is null)
return string.Empty;
if (prop.Formatter is not null) {
return prop.Formatter.Invoke(propValue);
}
if (prop.DisplayedProperty is null) {
var key = prop.Info.PropertyType
.GetProperties()
.Where(p => p.GetCustomAttributes(true).Any(a => a is KeyAttribute))
.FirstOrDefault();
return key?.GetValue(propValue)?.ToString() ?? propValue.ToString() ?? string.Empty;
}
var innerConfig = explorer.GetTable(propValue.GetType());
return DisplayProperty(propValue, prop.DisplayedProperty, innerConfig);