Merge branch 'feature/documentation' into 'dev'

Resolve "In-code documentation"

Closes #15

See merge request leon.hoppe/hopframe!17
This commit was merged in pull request #55.
This commit is contained in:
2025-01-18 13:48:26 +00:00
13 changed files with 549 additions and 325 deletions

View File

@@ -9,9 +9,17 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="0648788e-7696-4e60-bf12-5d5601f33d8c" name="Changes" comment="Added text area support and DI support for modifier functions">
<list default="true" id="0648788e-7696-4e60-bf12-5d5601f33d8c" name="Changes" comment="Addressed all build warnings">
<change beforePath="$PROJECT_DIR$/.idea/.idea.HopFrame/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.HopFrame/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/HopFrame.Web/Components/Pages/HopFrameTablePage.razor" beforeDir="false" afterPath="$PROJECT_DIR$/src/HopFrame.Web/Components/Pages/HopFrameTablePage.razor" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/HopFrame.Core/Config/DbContextConfig.cs" beforeDir="false" afterPath="$PROJECT_DIR$/src/HopFrame.Core/Config/DbContextConfigurator.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/HopFrame.Core/Config/HopFrameConfig.cs" beforeDir="false" afterPath="$PROJECT_DIR$/src/HopFrame.Core/Config/HopFrameConfig.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/HopFrame.Core/Config/PropertyConfig.cs" beforeDir="false" afterPath="$PROJECT_DIR$/src/HopFrame.Core/Config/PropertyConfigurator.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/HopFrame.Core/Config/TableConfig.cs" beforeDir="false" afterPath="$PROJECT_DIR$/src/HopFrame.Core/Config/TableConfigurator.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/HopFrame.Core/ServiceCollectionExtensions.cs" beforeDir="false" afterPath="$PROJECT_DIR$/src/HopFrame.Core/ServiceCollectionExtensions.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/HopFrame.Core/Services/IHopFrameAuthHandler.cs" beforeDir="false" afterPath="$PROJECT_DIR$/src/HopFrame.Core/Services/IHopFrameAuthHandler.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/HopFrame.Web/Components/Dialogs/HopFrameEditor.razor" beforeDir="false" afterPath="$PROJECT_DIR$/src/HopFrame.Web/Components/Dialogs/HopFrameEditor.razor" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/HopFrame.Web/ServiceCollectionExtensions.cs" beforeDir="false" afterPath="$PROJECT_DIR$/src/HopFrame.Web/ServiceCollectionExtensions.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/testing/HopFrame.Testing/Program.cs" beforeDir="false" afterPath="$PROJECT_DIR$/testing/HopFrame.Testing/Program.cs" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -31,7 +39,7 @@
<component name="Git.Settings">
<option name="RECENT_BRANCH_BY_REPOSITORY">
<map>
<entry key="$PROJECT_DIR$" value="dev" />
<entry key="$PROJECT_DIR$" value="feature/setup" />
</map>
</option>
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
@@ -84,24 +92,24 @@
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">{
&quot;keyToString&quot;: {
&quot;.NET Launch Settings Profile.HopFrame.Testing.executor&quot;: &quot;Run&quot;,
&quot;.NET Launch Settings Profile.HopFrame.Testing: https.executor&quot;: &quot;Run&quot;,
&quot;.NET Project.HopFrame.Testing.executor&quot;: &quot;Run&quot;,
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
&quot;RunOnceActivity.git.unshallow&quot;: &quot;true&quot;,
&quot;git-widget-placeholder&quot;: &quot;feature/setup&quot;,
&quot;list.type.of.created.stylesheet&quot;: &quot;CSS&quot;,
&quot;node.js.detected.package.eslint&quot;: &quot;true&quot;,
&quot;node.js.detected.package.tslint&quot;: &quot;true&quot;,
&quot;node.js.selected.package.eslint&quot;: &quot;(autodetect)&quot;,
&quot;node.js.selected.package.tslint&quot;: &quot;(autodetect)&quot;,
&quot;nodejs_package_manager_path&quot;: &quot;npm&quot;,
&quot;settings.editor.selected.configurable&quot;: &quot;preferences.environmentSetup&quot;,
&quot;vue.rearranger.settings.migration&quot;: &quot;true&quot;
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
".NET Launch Settings Profile.HopFrame.Testing.executor": "Run",
".NET Launch Settings Profile.HopFrame.Testing: https.executor": "Run",
".NET Project.HopFrame.Testing.executor": "Run",
"RunOnceActivity.ShowReadmeOnStart": "true",
"RunOnceActivity.git.unshallow": "true",
"git-widget-placeholder": "!17 on feature/documentation",
"list.type.of.created.stylesheet": "CSS",
"node.js.detected.package.eslint": "true",
"node.js.detected.package.tslint": "true",
"node.js.selected.package.eslint": "(autodetect)",
"node.js.selected.package.tslint": "(autodetect)",
"nodejs_package_manager_path": "npm",
"settings.editor.selected.configurable": "preferences.environmentSetup",
"vue.rearranger.settings.migration": "true"
}
}</component>
}]]></component>
<component name="RunManager" selected=".NET Launch Settings Profile.HopFrame.Testing: https">
<configuration name="HopFrame.Testing: http" type="LaunchSettings" factoryName=".NET Launch Settings Profile">
<option name="LAUNCH_PROFILE_PROJECT_FILE_PATH" value="$PROJECT_DIR$/testing/HopFrame.Testing/HopFrame.Testing.csproj" />
@@ -158,7 +166,7 @@
<workItem from="1737021098746" duration="21112000" />
<workItem from="1737047730756" duration="7678000" />
<workItem from="1737120164342" duration="9351000" />
<workItem from="1737199714142" duration="2964000" />
<workItem from="1737199714142" duration="7872000" />
</task>
<task id="LOCAL-00001" summary="Added basic configuration">
<option name="closed" value="true" />
@@ -272,7 +280,15 @@
<option name="project" value="LOCAL" />
<updated>1737202192471</updated>
</task>
<option name="localTasksCounter" value="15" />
<task id="LOCAL-00015" summary="Addressed all build warnings">
<option name="closed" value="true" />
<created>1737203441319</created>
<option name="number" value="00015" />
<option name="presentableId" value="LOCAL-00015" />
<option name="project" value="LOCAL" />
<updated>1737203441319</updated>
</task>
<option name="localTasksCounter" value="16" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@@ -296,6 +312,7 @@
<MESSAGE value="Added policy validation, ordering and virtual listing properties" />
<MESSAGE value="Added n -&gt; m relation support" />
<MESSAGE value="Added text area support and DI support for modifier functions" />
<option name="LAST_COMMIT_MESSAGE" value="Added text area support and DI support for modifier functions" />
<MESSAGE value="Addressed all build warnings" />
<option name="LAST_COMMIT_MESSAGE" value="Addressed all build warnings" />
</component>
</project>

View File

@@ -1,38 +0,0 @@
using Microsoft.EntityFrameworkCore;
namespace HopFrame.Core.Config;
public class DbContextConfig {
public Type ContextType { get; }
public List<TableConfig> Tables { get; } = new();
public DbContextConfig(Type context) {
ContextType = context;
foreach (var property in ContextType.GetProperties()) {
if (!property.PropertyType.IsGenericType) continue;
var innerType = property.PropertyType.GenericTypeArguments.First();
var setType = typeof(DbSet<>).MakeGenericType(innerType);
if (property.PropertyType != setType) continue;
var table = new TableConfig(this, innerType, property.Name, Tables.Count);
Tables.Add(table);
}
}
}
public class DbContextConfig<TDbContext>(DbContextConfig config) {
public DbContextConfig InnerConfig { get; } = config;
public DbContextConfig<TDbContext> Table<TModel>(Action<TableConfig<TModel>> configurator) where TModel : class {
var table = Table<TModel>();
configurator.Invoke(table);
return this;
}
public TableConfig<TModel> Table<TModel>() where TModel : class {
var table = InnerConfig.Tables.Single(table => table.TableType == typeof(TModel));
return new TableConfig<TModel>(table);
}
}

View File

@@ -0,0 +1,57 @@
using Microsoft.EntityFrameworkCore;
namespace HopFrame.Core.Config;
public class DbContextConfig {
public Type ContextType { get; }
public List<TableConfig> Tables { get; } = new();
public DbContextConfig(Type context) {
ContextType = context;
foreach (var property in ContextType.GetProperties()) {
if (!property.PropertyType.IsGenericType) continue;
var innerType = property.PropertyType.GenericTypeArguments.First();
var setType = typeof(DbSet<>).MakeGenericType(innerType);
if (property.PropertyType != setType) continue;
var table = new TableConfig(this, innerType, property.Name, Tables.Count);
Tables.Add(table);
}
}
}
/// <summary>
/// A helper class for editing the <see cref="DbContextConfig"/>
/// </summary>
public class DbContextConfigurator<TDbContext>(DbContextConfig config) {
/// <summary>
/// The Internal DbContext configuration that's modified by the helper functions
/// </summary>
public DbContextConfig InnerConfig { get; } = config;
/// <summary>
/// Configures the table of the DbContext using the provided configurator
/// </summary>
/// <param name="configurator">Used for configuring the table</param>
/// <typeparam name="TModel">The model of the table for identifying the correct one</typeparam>
/// <seealso cref="TableConfigurator{TModel}"/>
public DbContextConfigurator<TDbContext> Table<TModel>(Action<TableConfigurator<TModel>> configurator) where TModel : class {
var table = Table<TModel>();
configurator.Invoke(table);
return this;
}
/// <summary>
/// Configures the table of the DbContext
/// </summary>
/// <typeparam name="TModel">The model of the table for identifying the correct one</typeparam>
/// <returns>The configurator for the specified table</returns>
/// <seealso cref="TableConfigurator{TModel}"/>
public TableConfigurator<TModel> Table<TModel>() where TModel : class {
var table = InnerConfig.Tables.Single(table => table.TableType == typeof(TModel));
return new TableConfigurator<TModel>(table);
}
}

View File

@@ -10,31 +10,59 @@ public class HopFrameConfig {
public string? LoginPageRewrite { get; set; }
}
/// <summary>
/// A helper class for editing the <see cref="HopFrameConfig"/>
/// </summary>
public class HopFrameConfigurator(HopFrameConfig config) {
/// <summary>
/// The Internal HopFrame configuration that's modified by the helper functions
/// </summary>
public HopFrameConfig InnerConfig { get; } = config;
public HopFrameConfigurator AddDbContext<TDbContext>(Action<DbContextConfig<TDbContext>> configurator) where TDbContext : DbContext {
/// <summary>
/// Adds all tables defined in the DbContext to the HopFrame ui and configures it using the provided configurator
/// </summary>
/// <param name="configurator">Used for configuring the DbContext</param>
/// <typeparam name="TDbContext">The DbContext from which all tables should be added</typeparam>
/// <seealso cref="DbContextConfigurator{TDbContext}"/>
public HopFrameConfigurator AddDbContext<TDbContext>(Action<DbContextConfigurator<TDbContext>> configurator) where TDbContext : DbContext {
var context = AddDbContext<TDbContext>();
configurator.Invoke(context);
return this;
}
public DbContextConfig<TDbContext> AddDbContext<TDbContext>() where TDbContext : DbContext {
/// <summary>
/// Adds all tables defined in the DbContext to the HopFrame ui and configures it using the provided configurator
/// </summary>
/// <typeparam name="TDbContext">The DbContext from which all tables should be added</typeparam>
/// <returns>The configurator used for the DbContext</returns>
/// <seealso cref="DbContextConfigurator{TDbContext}"/>
public DbContextConfigurator<TDbContext> AddDbContext<TDbContext>() where TDbContext : DbContext {
var context = new DbContextConfig(typeof(TDbContext));
InnerConfig.Contexts.Add(context);
return new DbContextConfig<TDbContext>(context);
return new DbContextConfigurator<TDbContext>(context);
}
/// <summary>
/// Determines if the name of the currently logged-in user should be displayed in the top right corner of the admin ui
/// </summary>
public HopFrameConfigurator DisplayUserInfo(bool display) {
InnerConfig.DisplayUserInfo = display;
return this;
}
/// <summary>
/// Sets a default policy that every user needs to have in order to access the admin ui
/// </summary>
public HopFrameConfigurator SetBasePolicy(string basePolicy) {
InnerConfig.BasePolicy = basePolicy;
return this;
}
/// <summary>
/// Sets a custom login page to redirect to if the request to the admin ui was unauthorized
/// </summary>
public HopFrameConfigurator SetLoginPage(string url) {
InnerConfig.LoginPageRewrite = url;
return this;

View File

@@ -1,114 +0,0 @@
using System.Collections;
using System.Linq.Expressions;
using System.Reflection;
namespace HopFrame.Core.Config;
public class PropertyConfig(PropertyInfo info, TableConfig table, int nthProperty) {
public PropertyInfo Info { get; } = info;
public TableConfig Table { get; } = table;
public string Name { get; set; } = info.Name;
public bool List { get; set; } = true;
public bool Sortable { get; set; } = true;
public bool Searchable { get; set; } = true;
public PropertyInfo? DisplayedProperty { get; set; }
public Func<object, IServiceProvider, string>? Formatter { get; set; }
public Func<object, IServiceProvider, string>? EnumerableFormatter { get; set; }
public Func<string, IServiceProvider, object>? Parser { get; set; }
public Func<object?, IServiceProvider, Task<IEnumerable<string>>>? Validator { get; set; }
public bool Editable { get; set; } = true;
public bool Creatable { get; set; } = true;
public bool DisplayValue { get; set; } = true;
public bool TextArea { get; set; }
public int TextAreaRows { get; set; } = 16;
public bool IsRelation { get; set; }
public bool IsRequired { get; set; }
public bool IsEnumerable { get; set; }
public bool IsListingProperty { get; set; }
public int Order { get; set; } = nthProperty;
}
public class PropertyConfig<TProp>(PropertyConfig config) {
public PropertyConfig InnerConfig { get; } = config;
public PropertyConfig<TProp> SetDisplayName(string displayName) {
InnerConfig.Name = displayName;
return this;
}
public PropertyConfig<TProp> List(bool list) {
InnerConfig.List = list;
InnerConfig.Searchable = !list;
return this;
}
public PropertyConfig<TProp> IsSortable(bool sortable) {
InnerConfig.Sortable = sortable;
return this;
}
public PropertyConfig<TProp> IsSearchable(bool searchable) {
InnerConfig.Searchable = searchable;
return this;
}
public PropertyConfig<TProp> SetDisplayedProperty<TInnerProp>(Expression<Func<TProp, TInnerProp>> propertyExpression) {
InnerConfig.DisplayedProperty = TableConfig<TProp>.GetPropertyInfo(propertyExpression);
return this;
}
public PropertyConfig<TProp> Format(Func<TProp, IServiceProvider, string> formatter) {
InnerConfig.Formatter = (obj, provider) => formatter.Invoke((TProp)obj, provider);
return this;
}
public PropertyConfig<TProp> FormatEach<TInnerProp>(Func<TInnerProp, IServiceProvider, string> formatter) {
InnerConfig.EnumerableFormatter = (obj, provider) => formatter.Invoke((TInnerProp)obj, provider);
return this;
}
public PropertyConfig<TProp> SetParser(Func<string, IServiceProvider, TProp> parser) {
InnerConfig.Parser = (str, provider) => parser.Invoke(str, provider)!;
return this;
}
public PropertyConfig<TProp> SetEditable(bool editable) {
InnerConfig.Editable = editable;
return this;
}
public PropertyConfig<TProp> SetCreatable(bool creatable) {
InnerConfig.Creatable = creatable;
return this;
}
public PropertyConfig<TProp> DisplayValue(bool display) {
InnerConfig.DisplayValue = display;
return this;
}
public PropertyConfig<TProp> IsTextArea(bool textField) {
InnerConfig.TextArea = textField;
return this;
}
public PropertyConfig<TProp> SetTextAreaRows(int rows) {
InnerConfig.TextAreaRows = rows;
return this;
}
public PropertyConfig<TProp> SetValidator(Func<TProp?, IServiceProvider, IEnumerable<string>> validator) {
InnerConfig.Validator = (obj, provider) => Task.FromResult(validator.Invoke((TProp?)obj, provider));
return this;
}
public PropertyConfig<TProp> SetValidator(Func<TProp?, IServiceProvider, Task<IEnumerable<string>>> validator) {
InnerConfig.Validator = (obj, provider) => validator.Invoke((TProp?)obj, provider);
return this;
}
public PropertyConfig<TProp> SetOrderIndex(int index) {
InnerConfig.Order = index;
return this;
}
}

View File

@@ -0,0 +1,178 @@
using System.Collections;
using System.Linq.Expressions;
using System.Reflection;
namespace HopFrame.Core.Config;
public class PropertyConfig(PropertyInfo info, TableConfig table, int nthProperty) {
public PropertyInfo Info { get; } = info;
public TableConfig Table { get; } = table;
public string Name { get; set; } = info.Name;
public bool List { get; set; } = true;
public bool Sortable { get; set; } = true;
public bool Searchable { get; set; } = true;
public PropertyInfo? DisplayedProperty { get; set; }
public Func<object, IServiceProvider, string>? Formatter { get; set; }
public Func<object, IServiceProvider, string>? EnumerableFormatter { get; set; }
public Func<string, IServiceProvider, object>? Parser { get; set; }
public Func<object?, IServiceProvider, Task<IEnumerable<string>>>? Validator { get; set; }
public bool Editable { get; set; } = true;
public bool Creatable { get; set; } = true;
public bool DisplayValue { get; set; } = true;
public bool TextArea { get; set; }
public int TextAreaRows { get; set; } = 16;
public bool IsRelation { get; set; }
public bool IsRequired { get; set; }
public bool IsEnumerable { get; set; }
public bool IsListingProperty { get; set; }
public int Order { get; set; } = nthProperty;
}
/// <summary>
/// A helper class for editing the <see cref="PropertyConfig"/>
/// </summary>
public class PropertyConfigurator<TProp>(PropertyConfig config) {
/// <summary>
/// The Internal property configuration that's modified by the helper functions
/// </summary>
public PropertyConfig InnerConfig { get; } = config;
/// <summary>
/// Sets the title displayed in the table header and edit dialog
/// </summary>
public PropertyConfigurator<TProp> SetDisplayName(string displayName) {
InnerConfig.Name = displayName;
return this;
}
/// <summary>
/// Determines if the property should appear in the table, if not the property is also set to be not searchable
/// </summary>
/// <seealso cref="IsSearchable"/>
public PropertyConfigurator<TProp> List(bool list) {
InnerConfig.List = list;
InnerConfig.Searchable = !list;
return this;
}
/// <summary>
/// Determines if the table can be sorted by the property
/// </summary>
public PropertyConfigurator<TProp> IsSortable(bool sortable) {
InnerConfig.Sortable = sortable;
return this;
}
/// <summary>
/// Determines if the property get taken into account for search results
/// </summary>
public PropertyConfigurator<TProp> IsSearchable(bool searchable) {
InnerConfig.Searchable = searchable;
return this;
}
/// <summary>
/// Determines if the value that should be displayed instead of the string representation of the type
/// </summary>
/// <seealso cref="Format"/>
public PropertyConfigurator<TProp> SetDisplayedProperty<TInnerProp>(Expression<Func<TProp, TInnerProp>> propertyExpression) {
InnerConfig.DisplayedProperty = TableConfigurator<TProp>.GetPropertyInfo(propertyExpression);
return this;
}
/// <summary>
/// Determines the value that's displayed in the admin ui
/// </summary>
/// <seealso cref="FormatEach{TInnerProp}"/>
/// <seealso cref="SetDisplayedProperty{TInnerProp}"/>
public PropertyConfigurator<TProp> Format(Func<TProp, IServiceProvider, string> formatter) {
InnerConfig.Formatter = (obj, provider) => formatter.Invoke((TProp)obj, provider);
return this;
}
/// <summary>
/// Determines the value that's displayed for each entry in the list
/// </summary>
public PropertyConfigurator<TProp> FormatEach<TInnerProp>(Func<TInnerProp, IServiceProvider, string> formatter) {
InnerConfig.EnumerableFormatter = (obj, provider) => formatter.Invoke((TInnerProp)obj, provider);
return this;
}
/// <summary>
/// Determines the function used for parsing the value provided in the editor dialog to the actual property value
/// </summary>
public PropertyConfigurator<TProp> SetParser(Func<string, IServiceProvider, TProp> parser) {
InnerConfig.Parser = (str, provider) => parser.Invoke(str, provider)!;
return this;
}
/// <summary>
/// Determines if the value can be edited in the admin ui. If true, the value can still be initially set, but not modified
/// </summary>
/// <seealso cref="SetCreatable"/>
public PropertyConfigurator<TProp> SetEditable(bool editable) {
InnerConfig.Editable = editable;
return this;
}
/// <summary>
/// Determines if the initial value can be edited in the admin ui. If true the value will not be visible in the create dialog
/// </summary>
/// <seealso cref="SetEditable"/>
public PropertyConfigurator<TProp> SetCreatable(bool creatable) {
InnerConfig.Creatable = creatable;
return this;
}
/// <summary>
/// Determines if the value should be displayed in the admin ui (useful for secrets like passwords etc.)
/// </summary>
public PropertyConfigurator<TProp> DisplayValue(bool display) {
InnerConfig.DisplayValue = display;
return this;
}
/// <summary>
/// Determines if the admin ui should use a text area for modifying the value
/// </summary>
/// <seealso cref="SetTextAreaRows"/>
public PropertyConfigurator<TProp> IsTextArea(bool textField) {
InnerConfig.TextArea = textField;
return this;
}
/// <summary>
/// Determines the initial size of the text area field
/// </summary>
/// <seealso cref="IsTextArea"/>
public PropertyConfigurator<TProp> SetTextAreaRows(int rows) {
InnerConfig.TextAreaRows = rows;
return this;
}
/// <summary>
/// Determines the validator used for the property value before saving
/// </summary>
public PropertyConfigurator<TProp> SetValidator(Func<TProp?, IServiceProvider, IEnumerable<string>> validator) {
InnerConfig.Validator = (obj, provider) => Task.FromResult(validator.Invoke((TProp?)obj, provider));
return this;
}
/// <summary>
/// Determines the validator used for the property value before saving
/// </summary>
public PropertyConfigurator<TProp> SetValidator(Func<TProp?, IServiceProvider, Task<IEnumerable<string>>> validator) {
InnerConfig.Validator = (obj, provider) => validator.Invoke((TProp?)obj, provider);
return this;
}
/// <summary>
/// Determines the order index for the property in the admin ui
/// </summary>
/// <seealso cref="TableConfigurator{TModel}.SetOrderIndex"/>
public PropertyConfigurator<TProp> SetOrderIndex(int index) {
InnerConfig.Order = index;
return this;
}
}

View File

@@ -1,144 +0,0 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq.Expressions;
using System.Reflection;
namespace HopFrame.Core.Config;
public class TableConfig {
public Type TableType { get; }
public string PropertyName { get; }
public string DisplayName { get; set; }
public string? Description { get; set; }
public DbContextConfig ContextConfig { get; }
public bool Ignored { get; set; }
public int Order { get; set; }
internal bool Seeded { get; set; }
public string? ViewPolicy { get; set; }
public string? CreatePolicy { get; set; }
public string? UpdatePolicy { get; set; }
public string? DeletePolicy { get; set; }
public List<PropertyConfig> Properties { get; } = new();
public TableConfig(DbContextConfig config, Type tableType, string propertyName, int nthTable) {
TableType = tableType;
PropertyName = propertyName;
ContextConfig = config;
DisplayName = PropertyName;
Order = nthTable;
var properties = tableType.GetProperties();
for (var i = 0; i < properties.Length; i++) {
var info = properties[i];
var propConfig = new PropertyConfig(info, this, i);
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);
}
}
}
public class TableConfig<TModel>(TableConfig config) {
public TableConfig InnerConfig { get; } = config;
public TableConfig<TModel> Ignore() {
InnerConfig.Ignored = true;
return this;
}
public PropertyConfig<TProp> Property<TProp>(Expression<Func<TModel, TProp>> propertyExpression) {
var info = GetPropertyInfo(propertyExpression);
var prop = InnerConfig.Properties
.Single(prop => prop.Info.Name == info.Name);
return new PropertyConfig<TProp>(prop);
}
public TableConfig<TModel> Property<TProp>(Expression<Func<TModel, TProp>> propertyExpression, Action<PropertyConfig<TProp>> configurator) {
var prop = Property(propertyExpression);
configurator.Invoke(prop);
return this;
}
public PropertyConfig<string> AddListingProperty(string name, Func<TModel, IServiceProvider, string> template) {
var prop = new PropertyConfig(InnerConfig.Properties.First().Info, InnerConfig, InnerConfig.Properties.Count);
prop.Name = name;
prop.IsListingProperty = true;
prop.Formatter = (obj, provider) => template.Invoke((TModel)obj, provider);
InnerConfig.Properties.Add(prop);
return new PropertyConfig<string>(prop);
}
public TableConfig<TModel> AddListingProperty(string name, Func<TModel, IServiceProvider, string> template, Action<PropertyConfig<string>> configurator) {
var prop = AddListingProperty(name, template);
configurator.Invoke(prop);
return this;
}
public TableConfig<TModel> SetDisplayName(string name) {
InnerConfig.DisplayName = name;
return this;
}
public TableConfig<TModel> SetDescription(string description) {
InnerConfig.Description = description;
return this;
}
public TableConfig<TModel> SetOrderIndex(int index) {
InnerConfig.Order = index;
return this;
}
public TableConfig<TModel> SetViewPolicy(string policy) {
InnerConfig.ViewPolicy = policy;
return this;
}
public TableConfig<TModel> SetUpdatePolicy(string policy) {
InnerConfig.UpdatePolicy = policy;
return this;
}
public TableConfig<TModel> SetCreatePolicy(string policy) {
InnerConfig.CreatePolicy = policy;
return this;
}
public TableConfig<TModel> SetDeletePolicy(string policy) {
InnerConfig.DeletePolicy = policy;
return this;
}
internal static PropertyInfo GetPropertyInfo<TSource, TProperty>(Expression<Func<TSource, TProperty>> propertyLambda) {
if (propertyLambda.Body is not MemberExpression member) {
throw new ArgumentException($"Expression '{propertyLambda}' refers to a method, not a property.");
}
if (member.Member is not PropertyInfo propInfo) {
throw new ArgumentException($"Expression '{propertyLambda}' refers to a field, not a property.");
}
Type type = typeof(TSource);
if (propInfo.ReflectedType != null && type != propInfo.ReflectedType &&
!type.IsSubclassOf(propInfo.ReflectedType)) {
throw new ArgumentException($"Expression '{propertyLambda}' refers to a property that is not from type {type}.");
}
if (propInfo.Name is null)
throw new ArgumentException($"Expression '{propertyLambda}' refers a not existing property.");
return propInfo;
}
}

View File

@@ -0,0 +1,202 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq.Expressions;
using System.Reflection;
namespace HopFrame.Core.Config;
public class TableConfig {
public Type TableType { get; }
public string PropertyName { get; }
public string DisplayName { get; set; }
public string? Description { get; set; }
public DbContextConfig ContextConfig { get; }
public bool Ignored { get; set; }
public int Order { get; set; }
internal bool Seeded { get; set; }
public string? ViewPolicy { get; set; }
public string? CreatePolicy { get; set; }
public string? UpdatePolicy { get; set; }
public string? DeletePolicy { get; set; }
public List<PropertyConfig> Properties { get; } = new();
public TableConfig(DbContextConfig config, Type tableType, string propertyName, int nthTable) {
TableType = tableType;
PropertyName = propertyName;
ContextConfig = config;
DisplayName = PropertyName;
Order = nthTable;
var properties = tableType.GetProperties();
for (var i = 0; i < properties.Length; i++) {
var info = properties[i];
var propConfig = new PropertyConfig(info, this, i);
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);
}
}
}
/// <summary>
/// A helper class for editing the <see cref="TableConfig"/>
/// </summary>
public class TableConfigurator<TModel>(TableConfig config) {
/// <summary>
/// The Internal property configuration that's modified by the helper functions
/// </summary>
public TableConfig InnerConfig { get; } = config;
/// <summary>
/// Determines if the table should be ignored in the admin ui
/// </summary>
public TableConfigurator<TModel> Ignore(bool ignore) {
InnerConfig.Ignored = ignore;
return this;
}
/// <summary>
/// Configures the property of the table
/// </summary>
/// <param name="propertyExpression">Used for determining the property</param>
/// <returns>The configurator for the specified property</returns>
/// <seealso cref="PropertyConfigurator{TProp}"/>
public PropertyConfigurator<TProp> Property<TProp>(Expression<Func<TModel, TProp>> propertyExpression) {
var info = GetPropertyInfo(propertyExpression);
var prop = InnerConfig.Properties
.Single(prop => prop.Info.Name == info.Name);
return new PropertyConfigurator<TProp>(prop);
}
/// <summary>
/// Configures the property of the table using the provided configurator
/// </summary>
/// <param name="propertyExpression">Used for determining the property</param>
/// <param name="configurator">Used for configuring the property</param>
/// <seealso cref="PropertyConfigurator{TProp}"/>
public TableConfigurator<TModel> Property<TProp>(Expression<Func<TModel, TProp>> propertyExpression, Action<PropertyConfigurator<TProp>> configurator) {
var prop = Property(propertyExpression);
configurator.Invoke(prop);
return this;
}
/// <summary>
/// Adds a virtual property to the table view (this property will not appear in the editor)
/// </summary>
/// <param name="name">The name of the virtual property</param>
/// <param name="template">The template used for generating the property value</param>
/// <returns>The configurator for the virtual property</returns>
/// <seealso cref="PropertyConfigurator{TProp}"/>
public PropertyConfigurator<string> AddVirtualProperty(string name, Func<TModel, IServiceProvider, string> template) {
var prop = new PropertyConfig(InnerConfig.Properties.First().Info, InnerConfig, InnerConfig.Properties.Count);
prop.Name = name;
prop.IsListingProperty = true;
prop.Formatter = (obj, provider) => template.Invoke((TModel)obj, provider);
InnerConfig.Properties.Add(prop);
return new PropertyConfigurator<string>(prop);
}
/// <summary>
/// Adds a virtual property to the table view and configures it using the provided configurator (this property will not appear in the editor)
/// </summary>
/// <param name="name">The name of the virtual property</param>
/// <param name="template">The template used for generating the property value</param>
/// <param name="configurator">Used for configuring the virtual property</param>
/// <seealso cref="PropertyConfigurator{TProp}"/>
public TableConfigurator<TModel> AddVirtualProperty(string name, Func<TModel, IServiceProvider, string> template, Action<PropertyConfigurator<string>> configurator) {
var prop = AddVirtualProperty(name, template);
configurator.Invoke(prop);
return this;
}
/// <summary>
/// Determines the name for the table used in the admin ui and url for the table page
/// </summary>
public TableConfigurator<TModel> SetDisplayName(string name) {
InnerConfig.DisplayName = name;
return this;
}
/// <summary>
/// Determines the description displayed in the admin ui
/// </summary>
public TableConfigurator<TModel> SetDescription(string description) {
InnerConfig.Description = description;
return this;
}
/// <summary>
/// Determines the order index for the table in the admin ui
/// </summary>
/// <seealso cref="PropertyConfigurator{TProp}.SetOrderIndex"/>
public TableConfigurator<TModel> SetOrderIndex(int index) {
InnerConfig.Order = index;
return this;
}
/// <summary>
/// Determines the policy needed by a user in order to view the table
/// </summary>
public TableConfigurator<TModel> SetViewPolicy(string policy) {
InnerConfig.ViewPolicy = policy;
return this;
}
/// <summary>
/// Determines the policy needed by a user in order to edit the entries
/// </summary>
public TableConfigurator<TModel> SetUpdatePolicy(string policy) {
InnerConfig.UpdatePolicy = policy;
return this;
}
/// <summary>
/// Determines the policy needed by a user in order to create entries
/// </summary>
public TableConfigurator<TModel> SetCreatePolicy(string policy) {
InnerConfig.CreatePolicy = policy;
return this;
}
/// <summary>
/// Determines the policy needed by a user in order to delete entries
/// </summary>
public TableConfigurator<TModel> SetDeletePolicy(string policy) {
InnerConfig.DeletePolicy = policy;
return this;
}
internal static PropertyInfo GetPropertyInfo<TSource, TProperty>(Expression<Func<TSource, TProperty>> propertyLambda) {
if (propertyLambda.Body is not MemberExpression member) {
throw new ArgumentException($"Expression '{propertyLambda}' refers to a method, not a property.");
}
if (member.Member is not PropertyInfo propInfo) {
throw new ArgumentException($"Expression '{propertyLambda}' refers to a field, not a property.");
}
Type type = typeof(TSource);
if (propInfo.ReflectedType != null && type != propInfo.ReflectedType &&
!type.IsSubclassOf(propInfo.ReflectedType)) {
throw new ArgumentException($"Expression '{propertyLambda}' refers to a property that is not from type {type}.");
}
if (propInfo.Name is null)
throw new ArgumentException($"Expression '{propertyLambda}' refers a not existing property.");
return propInfo;
}
}

View File

@@ -7,9 +7,14 @@ namespace HopFrame.Core;
public static class ServiceCollectionExtensions {
/// <summary>
/// Adds all internal HopFrame services used by the web ui including the default insecure auth handler if not already provided
/// </summary>
/// <param name="services">The service collection to add the services to</param>
/// <returns>The same service collection that is passed in</returns>
public static IServiceCollection AddHopFrameServices(this IServiceCollection services) {
services.AddTransient<IContextExplorer, ContextExplorer>();
services.TryAddTransient<IHopFrameAuthHandler, DefaultAuthHandler>();
services.AddScoped<IContextExplorer, ContextExplorer>();
services.TryAddScoped<IHopFrameAuthHandler, DefaultAuthHandler>();
return services;
}

View File

@@ -1,6 +1,24 @@
namespace HopFrame.Core.Services;
/// <summary>
/// This handler is used by the HopFrame for authenticating the user that wants to access the admin ui.
/// By default, everyone is allowed to access the admin ui, but you can implement and register this interface
/// yourself as a scoped dependency if you only want some users to access the admin ui (recommended)
/// </summary>
public interface IHopFrameAuthHandler {
/// <summary>
/// Gets called by the admin ui in order to verify if the user has all necessary policies to perform an action
/// </summary>
/// <param name="policy">If specified, the policy the user needs to perform the action</param>
/// <returns>True: The user is permitted to perform the action<br /> False: The user is not permitted to perform the action</returns>
public Task<bool> IsAuthenticatedAsync(string? policy);
/// <summary>
/// If enabled, this method is used to get the name of the currently logged-in user in order to display it in the admin ui
/// </summary>
/// <returns>The display name of the currently logged-in user</returns>
/// <seealso cref="HopFrame.Core.Config.HopFrameConfigurator.DisplayUserInfo"/>
public Task<string> GetCurrentUserDisplayNameAsync();
}

View File

@@ -140,6 +140,7 @@
Value="@(GetPropertyValue<string>(property))"
Style="width: 100%;"
Rows="@property.TextAreaRows"
Resize="TextAreaResize.Vertical"
Disabled="@(_currentlyEditing && !property.Editable)"
Required="@property.IsRequired"
ValueChanged="@(async v => await SetPropertyValue(property, v, InputType.Text))" />

View File

@@ -7,12 +7,26 @@ namespace HopFrame.Web;
public static class ServiceCollectionExtensions {
/// <summary>
/// Configures the HopFrame using the provided configurator and adds all internal HopFrame services including the default insecure auth handler if not already provided
/// </summary>
/// <param name="services">The service collection to add the services to</param>
/// <param name="configurator">The configurator used to build the HopFrame configuration</param>
/// <param name="fluentUiLibraryConfiguration">The configuration for the FluentUI components</param>
/// <returns>The same service collection that is passed in</returns>
public static IServiceCollection AddHopFrame(this IServiceCollection services, Action<HopFrameConfigurator> configurator, LibraryConfiguration? fluentUiLibraryConfiguration = null) {
var config = new HopFrameConfig();
configurator.Invoke(new HopFrameConfigurator(config));
return AddHopFrame(services, config, fluentUiLibraryConfiguration);
}
/// <summary>
/// Configures the HopFrame using the provided configurator and adds all internal HopFrame services including the default insecure auth handler if not already provided
/// </summary>
/// <param name="services">The service collection to add the services to</param>
/// <param name="config">The config used for the HopFrame admin ui</param>
/// <param name="fluentUiLibraryConfiguration">The configuration for the FluentUI components</param>
/// <returns>The same service collection that is passed in</returns>
public static IServiceCollection AddHopFrame(this IServiceCollection services, HopFrameConfig config, LibraryConfiguration? fluentUiLibraryConfiguration = null) {
services.AddSingleton(config);
services.AddHopFrameServices();

View File

@@ -35,7 +35,7 @@ builder.Services.AddHopFrame(options => {
.IsSortable(false)
.SetOrderIndex(3);
table.AddListingProperty("Name", (user, _) => $"{user.FirstName} {user.LastName}")
table.AddVirtualProperty("Name", (user, _) => $"{user.FirstName} {user.LastName}")
.SetOrderIndex(2);
table.SetDisplayName("Benutzer");