Worked on admin page listing

This commit is contained in:
2024-10-07 17:39:05 +02:00
parent 6a781990e4
commit 075ca2286f
17 changed files with 195 additions and 21 deletions

View File

@@ -0,0 +1,6 @@
namespace HopFrame.Web.Admin.Attributes.Members;
[AttributeUsage(AttributeTargets.Property)]
public class AdminBoldAttribute(bool bold = true) : Attribute {
public bool Bold { get; set; } = bold;
}

View File

@@ -20,7 +20,7 @@ public interface IAdminPageGenerator<TModel> {
IAdminPageGenerator<TModel> DefaultSort<TProperty>(Expression<Func<TModel, TProperty>> propertyExpression, ListSortDirection direction);
IAdminPageGenerator<TModel> ConfigureRepository<TRepository>() where TRepository : IModelRepository<TModel>;
IAdminPageGenerator<TModel> ConfigureRepository<TRepository>() where TRepository : ModelRepository<TModel>;
IAdminPropertyGenerator Property<TProperty>(Expression<Func<TModel, TProperty>> propertyExpression);

View File

@@ -8,6 +8,7 @@ public interface IAdminPropertyGenerator {
IAdminPropertyGenerator DisplayInListing(bool display = true);
IAdminPropertyGenerator Ignore(bool ignore = true);
IAdminPropertyGenerator Generated(bool generated = true);
IAdminPropertyGenerator Bold(bool bold = true);
IAdminPropertyGenerator DisplayName(string displayName);
IAdminPropertyGenerator Description(string description);

View File

@@ -1,5 +1,6 @@
using HopFrame.Web.Admin.Models;
using HopFrame.Web.Admin.Providers;
using Microsoft.Extensions.DependencyInjection;
namespace HopFrame.Web.Admin.Generators.Implementation;
@@ -58,7 +59,7 @@ internal class AdminContextGenerator : IAdminContextGenerator {
public static void RegisterPages(AdminPagesContext context, IAdminPagesProvider provider) {
public static void RegisterPages(AdminPagesContext context, IAdminPagesProvider provider, IServiceCollection services) {
var properties = context.GetType().GetProperties();
foreach (var property in properties) {
@@ -66,6 +67,9 @@ internal class AdminContextGenerator : IAdminContextGenerator {
if (page is null) continue;
provider.RegisterAdminPage(page.Title.ToLower(), page);
if (page.RepositoryProvider is not null)
services.AddScoped(page.RepositoryProvider);
}
}

View File

@@ -94,7 +94,7 @@ internal sealed class AdminPageGenerator<TModel> : IAdminPageGenerator<TModel>,
return this;
}
public IAdminPageGenerator<TModel> ConfigureRepository<TRepository>() where TRepository : IModelRepository<TModel> {
public IAdminPageGenerator<TModel> ConfigureRepository<TRepository>() where TRepository : ModelRepository<TModel> {
Page.RepositoryProvider = typeof(TRepository);
return this;
}

View File

@@ -45,6 +45,11 @@ internal sealed class AdminPropertyGenerator(string name, Type type) : IAdminPro
return this;
}
public IAdminPropertyGenerator Bold(bool bold = true) {
_property.Bold = bold;
return this;
}
public IAdminPropertyGenerator DisplayName(string displayName) {
_property.DisplayName = displayName;
return this;
@@ -69,6 +74,7 @@ internal sealed class AdminPropertyGenerator(string name, Type type) : IAdminPro
if (attributes.Any(a => a is KeyAttribute)) {
pageGenerator.Page.DefaultSortPropertyName = property.Name;
Editable(false);
Bold();
}
if (attributes.Any(a => a is AdminUnsortableAttribute))
@@ -99,6 +105,11 @@ internal sealed class AdminPropertyGenerator(string name, Type type) : IAdminPro
var attribute = attributes.Single(a => a is AdminDescriptionAttribute) as AdminDescriptionAttribute;
Description(attribute?.Description);
}
if (attributes.Any(a => a is AdminBoldAttribute)) {
var attribute = attributes.Single(a => a is AdminBoldAttribute) as AdminBoldAttribute;
Bold(attribute?.Bold == true);
}
if (attributes.Any(a => a is AdminPrefixAttribute)) {
var attribute = attributes.Single(a => a is AdminPrefixAttribute) as AdminPrefixAttribute;

View File

@@ -1,8 +0,0 @@
namespace HopFrame.Web.Admin;
public interface IModelRepository<TModel> {
Task<IEnumerable<TModel>> ReadAll();
Task<TModel> Create(TModel model);
Task<TModel> Update(TModel model);
Task Delete(TModel model);
}

View File

@@ -0,0 +1,33 @@
namespace HopFrame.Web.Admin;
public abstract class ModelRepository<TModel> : IModelRepository {
public abstract Task<IEnumerable<TModel>> ReadAll();
public abstract Task<TModel> Create(TModel model);
public abstract Task<TModel> Update(TModel model);
public abstract Task Delete(TModel model);
public async Task<IEnumerable<object>> ReadAllO() {
var models = await ReadAll();
return models.Select(m => (object)m);
}
public async Task<object> CreateO(object model) {
return await Create((TModel)model);
}
public async Task<object> UpdateO(object model) {
return await Update((TModel)model);
}
public Task DeleteO(object model) {
return Delete((TModel)model);
}
}
public interface IModelRepository {
Task<IEnumerable<object>> ReadAllO();
Task<object> CreateO(object model);
Task<object> UpdateO(object model);
Task DeleteO(object model);
}

View File

@@ -13,6 +13,7 @@ public sealed class AdminPageProperty {
public bool Editable { get; set; } = true;
public bool EditDisplayValue { get; set; } = true;
public bool Generated { get; set; }
public bool Bold { get; set; } = false;
public bool Ignore { get; set; }
[JsonIgnore]
public Type Type { get; set; }

View File

@@ -16,7 +16,7 @@ public static class ServiceCollectionExtensions {
var generator = new AdminContextGenerator();
var context = generator.CompileContext<TContext>();
AdminContextGenerator.RegisterPages(context, provider);
AdminContextGenerator.RegisterPages(context, provider, services);
services.AddSingleton(context);
return services;