Created static object provider + added some properties
This commit is contained in:
@@ -4,6 +4,6 @@ namespace HopFrame.Web.Admin;
|
||||
|
||||
public abstract class AdminPagesContext {
|
||||
|
||||
public abstract void OnModelCreating(IAdminContextGenerator generator);
|
||||
public virtual void OnModelCreating(IAdminContextGenerator generator) {}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace HopFrame.Web.Admin.Attributes;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property)]
|
||||
public class AdminDescriptionAttribute(string description) : Attribute {
|
||||
public sealed class AdminDescriptionAttribute(string description) : Attribute {
|
||||
public string Description { get; set; } = description;
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace HopFrame.Web.Admin.Attributes;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property)]
|
||||
public class AdminNameAttribute(string name) : Attribute {
|
||||
public sealed class AdminNameAttribute(string name) : Attribute {
|
||||
public string Name { get; set; } = name;
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
namespace HopFrame.Web.Admin.Attributes.Classes;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
public class AdminButtonConfigAttribute(bool showCreateButton = true, bool showDeleteButton = true, bool showUpdateButton = true) : Attribute {
|
||||
public sealed class AdminButtonConfigAttribute(bool showCreateButton = true, bool showDeleteButton = true, bool showUpdateButton = true) : Attribute {
|
||||
public bool ShowCreateButton { get; set; } = showCreateButton;
|
||||
public bool ShowDeleteButton { get; set; } = showDeleteButton;
|
||||
public bool ShowUpdateButton { get; set; } = showUpdateButton;
|
||||
|
||||
@@ -3,6 +3,11 @@ using HopFrame.Web.Admin.Models;
|
||||
namespace HopFrame.Web.Admin.Attributes.Classes;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
public class AdminPermissionsAttribute(AdminPagePermissions permissions) : Attribute {
|
||||
public AdminPagePermissions Permissions { get; set; } = permissions;
|
||||
public sealed class AdminPermissionsAttribute(string view = null, string create = null, string update = null, string delete = null) : Attribute {
|
||||
public AdminPagePermissions Permissions { get; set; } = new() {
|
||||
Create = create,
|
||||
Update = update,
|
||||
Delete = delete,
|
||||
View = view
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace HopFrame.Web.Admin.Attributes.Members;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Property)]
|
||||
public class AdminBoldAttribute : Attribute;
|
||||
public sealed class AdminBoldAttribute : Attribute;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace HopFrame.Web.Admin.Attributes.Members;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Property)]
|
||||
public class AdminHideValueAttribute : Attribute;
|
||||
public sealed class AdminHideValueAttribute : Attribute;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace HopFrame.Web.Admin.Attributes.Members;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Property)]
|
||||
public class AdminIgnoreAttribute(bool onlyForListing = false) : Attribute {
|
||||
public sealed class AdminIgnoreAttribute(bool onlyForListing = false) : Attribute {
|
||||
public bool OnlyForListing { get; set; } = onlyForListing;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace HopFrame.Web.Admin.Attributes.Members;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Property)]
|
||||
public sealed class AdminPrefixAttribute(string prefix) : Attribute {
|
||||
public string Prefix { get; set; } = prefix;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace HopFrame.Web.Admin.Attributes.Members;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Property)]
|
||||
public class AdminUneditableAttribute : Attribute;
|
||||
public sealed class AdminUneditableAttribute : Attribute;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace HopFrame.Web.Admin.Attributes.Members;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Property)]
|
||||
public class AdminUnsortableAttribute : Attribute;
|
||||
public sealed class AdminUnsortableAttribute : Attribute;
|
||||
|
||||
@@ -7,8 +7,10 @@ public interface IAdminPropertyGenerator {
|
||||
IAdminPropertyGenerator DisplayValueWhileEditing(bool display);
|
||||
IAdminPropertyGenerator DisplayInListing(bool display = true);
|
||||
IAdminPropertyGenerator Bold(bool isBold = true);
|
||||
IAdminPropertyGenerator Ignore(bool ignore = true);
|
||||
|
||||
IAdminPropertyGenerator DisplayName(string displayName);
|
||||
IAdminPropertyGenerator Description(string description);
|
||||
IAdminPropertyGenerator Prefix(string prefix);
|
||||
|
||||
}
|
||||
@@ -2,6 +2,7 @@ using System.Reflection;
|
||||
using HopFrame.Web.Admin.Attributes;
|
||||
using HopFrame.Web.Admin.Attributes.Classes;
|
||||
using HopFrame.Web.Admin.Models;
|
||||
using HopFrame.Web.Admin.Providers;
|
||||
|
||||
namespace HopFrame.Web.Admin.Generators.Implementation;
|
||||
|
||||
@@ -40,7 +41,7 @@ internal class AdminContextGenerator : IAdminContextGenerator {
|
||||
foreach (var property in properties) {
|
||||
var propertyType = property.PropertyType.GenericTypeArguments[0];
|
||||
var pageGeneratorType = typeof(AdminPageGenerator<>).MakeGenericType(propertyType);
|
||||
var generatorInstance = Activator.CreateInstance(pageGeneratorType, [propertyType.Name]);
|
||||
var generatorInstance = Activator.CreateInstance(pageGeneratorType, [property.Name]); // Calls constructor with title attribute
|
||||
|
||||
var populatorMethod = typeof(AdminContextGenerator)
|
||||
.GetMethod(nameof(ApplyConfigurationFromAttributes))?
|
||||
@@ -88,4 +89,15 @@ internal class AdminContextGenerator : IAdminContextGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
public static void RegisterPages(AdminPagesContext context, IAdminPagesProvider provider) {
|
||||
var properties = context.GetType().GetProperties();
|
||||
|
||||
foreach (var property in properties) {
|
||||
var page = property.GetValue(context) as AdminPage;
|
||||
if (page is null) continue;
|
||||
|
||||
provider.RegisterAdminPage(page.Title.ToLower(), page);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -24,9 +24,6 @@ internal sealed class AdminPageGenerator<TModel> : IAdminPageGenerator<TModel>,
|
||||
|
||||
foreach (var property in properties) {
|
||||
var attributes = property.GetCustomAttributes(false);
|
||||
var ignoreProperty = attributes
|
||||
.SingleOrDefault(a => a is AdminIgnoreAttribute) as AdminIgnoreAttribute;
|
||||
if (ignoreProperty?.OnlyForListing == false) continue;
|
||||
|
||||
var generator = Activator.CreateInstance(typeof(AdminPropertyGenerator), [property.Name, property.PropertyType]) as AdminPropertyGenerator;
|
||||
|
||||
@@ -139,8 +136,12 @@ internal sealed class AdminPageGenerator<TModel> : IAdminPageGenerator<TModel>,
|
||||
if (attributes.Any(a => a is AdminBoldAttribute))
|
||||
generator.Bold();
|
||||
|
||||
if (attributes.Any(a => a is AdminIgnoreAttribute))
|
||||
if (attributes.Any(a => a is AdminIgnoreAttribute)) {
|
||||
var attribute = attributes.Single(a => a is AdminIgnoreAttribute) as AdminIgnoreAttribute;
|
||||
generator.DisplayInListing(false);
|
||||
generator.Sortable(false);
|
||||
generator.Ignore(attribute?.OnlyForListing == false);
|
||||
}
|
||||
|
||||
if (attributes.Any(a => a is AdminHideValueAttribute))
|
||||
generator.DisplayValueWhileEditing(false);
|
||||
@@ -154,6 +155,11 @@ internal sealed class AdminPageGenerator<TModel> : IAdminPageGenerator<TModel>,
|
||||
var attribute = attributes.Single(a => a is AdminDescriptionAttribute) as AdminDescriptionAttribute;
|
||||
generator.Description(attribute?.Description);
|
||||
}
|
||||
|
||||
if (attributes.Any(a => a is AdminPrefixAttribute)) {
|
||||
var attribute = attributes.Single(a => a is AdminPrefixAttribute) as AdminPrefixAttribute;
|
||||
generator.Prefix(attribute?.Prefix);
|
||||
}
|
||||
}
|
||||
|
||||
private static PropertyInfo GetPropertyInfo<TSource, TProperty>(Expression<Func<TSource, TProperty>> propertyLambda) {
|
||||
|
||||
@@ -26,6 +26,7 @@ internal sealed class AdminPropertyGenerator(string name, Type type) : IAdminPro
|
||||
|
||||
public IAdminPropertyGenerator DisplayInListing(bool display = true) {
|
||||
_property.DisplayInListing = display;
|
||||
_property.Sortable = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -34,6 +35,11 @@ internal sealed class AdminPropertyGenerator(string name, Type type) : IAdminPro
|
||||
return this;
|
||||
}
|
||||
|
||||
public IAdminPropertyGenerator Ignore(bool ignore = false) {
|
||||
_property.Ignore = ignore;
|
||||
return this;
|
||||
}
|
||||
|
||||
public IAdminPropertyGenerator DisplayName(string displayName) {
|
||||
_property.DisplayName = displayName;
|
||||
return this;
|
||||
@@ -44,6 +50,11 @@ internal sealed class AdminPropertyGenerator(string name, Type type) : IAdminPro
|
||||
return this;
|
||||
}
|
||||
|
||||
public IAdminPropertyGenerator Prefix(string prefix) {
|
||||
_property.Prefix = prefix;
|
||||
return this;
|
||||
}
|
||||
|
||||
public AdminPageProperty Compile() {
|
||||
_property.DisplayName ??= _property.Name;
|
||||
|
||||
|
||||
@@ -3,11 +3,14 @@ using System.Text.Json.Serialization;
|
||||
|
||||
namespace HopFrame.Web.Admin.Models;
|
||||
|
||||
public class AdminPage<TModel> : IAdminPageEntry {
|
||||
public sealed class AdminPage<TModel> : AdminPage;
|
||||
|
||||
public class AdminPage {
|
||||
public string Title { get; set; }
|
||||
public string Description { get; set; }
|
||||
public AdminPagePermissions Permissions { get; set; }
|
||||
public IList<AdminPageProperty> Properties { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public Type RepositoryProvider { get; set; }
|
||||
|
||||
@@ -18,5 +21,3 @@ public class AdminPage<TModel> : IAdminPageEntry {
|
||||
public bool ShowDeleteButton { get; set; } = true;
|
||||
public bool ShowUpdateButton { get; set; } = true;
|
||||
}
|
||||
|
||||
public interface IAdminPageEntry;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace HopFrame.Web.Admin.Models;
|
||||
|
||||
public class AdminPagePermissions {
|
||||
public sealed class AdminPagePermissions {
|
||||
public string View { get; set; }
|
||||
public string Create { get; set; }
|
||||
public string Update { get; set; }
|
||||
|
||||
@@ -2,16 +2,18 @@ using System.Text.Json.Serialization;
|
||||
|
||||
namespace HopFrame.Web.Admin.Models;
|
||||
|
||||
public class AdminPageProperty {
|
||||
public sealed class AdminPageProperty {
|
||||
public string Name { get; set; }
|
||||
public string DisplayName { get; set; }
|
||||
public string Description { get; set; }
|
||||
public string Prefix { get; set; }
|
||||
|
||||
public bool DisplayInListing { get; set; } = true;
|
||||
public bool Sortable { get; set; } = true;
|
||||
public bool Editable { get; set; } = true;
|
||||
public bool EditDisplayValue { get; set; } = true;
|
||||
public bool Bold { get; set; }
|
||||
public bool Ignore { get; set; }
|
||||
[JsonIgnore]
|
||||
public Type Type { get; set; }
|
||||
}
|
||||
11
src/HopFrame.Web.Admin/Providers/IAdminPagesProvider.cs
Normal file
11
src/HopFrame.Web.Admin/Providers/IAdminPagesProvider.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using HopFrame.Web.Admin.Models;
|
||||
|
||||
namespace HopFrame.Web.Admin.Providers;
|
||||
|
||||
public interface IAdminPagesProvider {
|
||||
|
||||
internal void RegisterAdminPage(string url, AdminPage page);
|
||||
AdminPage LoadAdminPage(string url);
|
||||
IList<AdminPage> LoadRegisteredAdminPages();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using HopFrame.Web.Admin.Models;
|
||||
|
||||
namespace HopFrame.Web.Admin.Providers.Implementation;
|
||||
|
||||
public class AdminPagesProvider : IAdminPagesProvider {
|
||||
private readonly IDictionary<string, AdminPage> _pages = new Dictionary<string, AdminPage>();
|
||||
|
||||
public void RegisterAdminPage(string url, AdminPage page) {
|
||||
_pages.Add(url, page);
|
||||
}
|
||||
|
||||
public AdminPage LoadAdminPage(string url) {
|
||||
return _pages.TryGetValue(url, out var page) ? page : null;
|
||||
}
|
||||
|
||||
public IList<AdminPage> LoadRegisteredAdminPages() {
|
||||
return _pages.Values.ToList();
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,29 @@
|
||||
using HopFrame.Web.Admin.Generators.Implementation;
|
||||
using HopFrame.Web.Admin.Providers;
|
||||
using HopFrame.Web.Admin.Providers.Implementation;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
|
||||
namespace HopFrame.Web.Admin;
|
||||
|
||||
public static class ServiceCollectionExtensions {
|
||||
|
||||
private static IAdminPagesProvider _provider;
|
||||
|
||||
public static IServiceCollection AddAdminContext<TContext>(this IServiceCollection services) where TContext : AdminPagesContext {
|
||||
services.AddSingleton(_ => {
|
||||
var generator = new AdminContextGenerator();
|
||||
return generator.CompileContext<TContext>();
|
||||
});
|
||||
var provider = GetProvider();
|
||||
services.TryAddSingleton(provider);
|
||||
|
||||
var generator = new AdminContextGenerator();
|
||||
var context = generator.CompileContext<TContext>();
|
||||
AdminContextGenerator.RegisterPages(context, provider);
|
||||
services.AddSingleton(context);
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
private static IAdminPagesProvider GetProvider() {
|
||||
return _provider ??= new AdminPagesProvider();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user