Added plugin events

This commit is contained in:
2025-02-02 19:06:41 +01:00
parent 4cfeaab652
commit 13e9af892c
28 changed files with 415 additions and 87 deletions

View File

@@ -1,5 +1,4 @@
using HopFrame.Core.Events;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace HopFrame.Core.Config;

View File

@@ -1,5 +1,6 @@
using HopFrame.Core.Events;
using HopFrame.Core.Callbacks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace HopFrame.Core.Config;
@@ -8,18 +9,20 @@ public class HopFrameConfig {
public bool DisplayUserInfo { get; set; } = true;
public string? BasePolicy { get; set; }
public string? LoginPageRewrite { get; set; }
public List<HopEventHandler> Handlers { get; } = new();
public List<HopCallbackHandler> Handlers { get; } = new();
}
/// <summary>
/// A helper class for editing the <see cref="HopFrameConfig"/>
/// </summary>
public class HopFrameConfigurator(HopFrameConfig config) {
public class HopFrameConfigurator(HopFrameConfig config, IServiceCollection collection = null!) {
/// <summary>
/// The Internal HopFrame configuration that's modified by the helper functions
/// </summary>
public HopFrameConfig InnerConfig { get; } = config;
public IServiceCollection ServiceCollection { get; } = collection;
/// <summary>
/// Adds all tables defined in the DbContext to the HopFrame ui and configures it using the provided configurator
@@ -45,6 +48,18 @@ public class HopFrameConfigurator(HopFrameConfig config) {
return new DbContextConfigurator<TDbContext>(context);
}
public bool HasDbContext<TDbContext>() where TDbContext : DbContext {
return InnerConfig.Contexts.Any(context => context.ContextType == typeof(TDbContext));
}
public DbContextConfigurator<TDbContext>? GetDbContext<TDbContext>() where TDbContext : DbContext {
var config = InnerConfig.Contexts
.SingleOrDefault(context => context.ContextType == typeof(TDbContext));
if (config is null) return null;
return new DbContextConfigurator<TDbContext>(config);
}
/// <summary>
/// Determines if the name of the currently logged-in user should be displayed in the top right corner of the admin ui
/// </summary>

View File

@@ -2,7 +2,7 @@
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq.Expressions;
using System.Reflection;
using HopFrame.Core.Events;
using HopFrame.Core.Callbacks;
namespace HopFrame.Core.Config;
@@ -191,26 +191,26 @@ public class TableConfigurator<TModel>(TableConfig config) {
}
/// <summary>
/// Adds an event handler of the provided type
/// Adds a callback handler of the provided type
/// </summary>
/// <param name="type">The type of event that triggers the handler</param>
/// <param name="type">The type of callback that triggers the handler</param>
/// <param name="handler">The handler delegate</param>
public TableConfigurator<TModel> AddEventHandler(EventType type, Func<TModel, IServiceProvider, Task> handler) {
var eventName = EventTypes.ConstructEventName(type, InnerConfig);
var handlerStore = new HopEventHandler(eventName, (o, provider) => handler.Invoke((TModel)o, provider));
public TableConfigurator<TModel> AddCallbackHandler(CallbackType type, Func<TModel, IServiceProvider, Task> handler) {
var eventName = CallbackTypes.ConstructCallbackName(type, InnerConfig);
var handlerStore = new HopCallbackHandler(eventName, (o, provider) => handler.Invoke((TModel)o, provider));
InnerConfig.ContextConfig.ParentConfig.Handlers.Add(handlerStore);
return this;
}
/// <summary>
/// Adds an event handler of the provided type
/// Adds a callback handler of the provided type
/// </summary>
/// <param name="type">The type of event that triggers the handler</param>
/// <param name="type">The type of callback that triggers the handler</param>
/// <param name="handler">The handler delegate</param>
public TableConfigurator<TModel> AddEventHandler(EventType type, Action<TModel, IServiceProvider> handler) {
var eventName = EventTypes.ConstructEventName(type, InnerConfig);
var handlerStore = new HopEventHandler(eventName, (o, provider) => {
public TableConfigurator<TModel> AddCallbackHandler(CallbackType type, Action<TModel, IServiceProvider> handler) {
var eventName = CallbackTypes.ConstructCallbackName(type, InnerConfig);
var handlerStore = new HopCallbackHandler(eventName, (o, provider) => {
handler.Invoke((TModel)o, provider);
return Task.CompletedTask;
});