using HopFrame.Core.Callbacks; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; namespace HopFrame.Core.Config; public class HopFrameConfig { public List Contexts { get; } = new(); public bool DisplayUserInfo { get; set; } = true; public string? BasePolicy { get; set; } public string? LoginPageRewrite { get; set; } public List Handlers { get; } = new(); } /// /// A helper class for editing the /// public sealed class HopFrameConfigurator(HopFrameConfig config, IServiceCollection collection = null!) { /// /// The Internal HopFrame configuration that's modified by the helper functions /// public HopFrameConfig InnerConfig { get; } = config; public IServiceCollection ServiceCollection { get; } = collection; /// /// Adds all tables defined in the DbContext to the HopFrame ui and configures it using the provided configurator /// /// Used for configuring the DbContext /// The DbContext from which all tables should be added /// public HopFrameConfigurator AddDbContext(Action> configurator) where TDbContext : DbContext { var context = AddDbContext(); configurator.Invoke(context); return this; } /// /// Adds all tables defined in the DbContext to the HopFrame ui and configures it using the provided configurator /// /// The DbContext from which all tables should be added /// The configurator used for the DbContext /// public DbContextConfigurator AddDbContext() where TDbContext : DbContext { var context = new DbContextConfig(typeof(TDbContext), InnerConfig); InnerConfig.Contexts.Add(context); return new DbContextConfigurator(context); } /// /// Check if a context is already registered in the HopFrame /// /// The context that should be checked /// true if the context is already registered, false if not public bool HasDbContext() where TDbContext : DbContext { return InnerConfig.Contexts.Any(context => context.ContextType == typeof(TDbContext)); } /// /// Returns a configurator for the context if it was already defined /// /// /// The configurator of the context if it already was defined, null if not public DbContextConfigurator? GetDbContext() where TDbContext : DbContext { var config = InnerConfig.Contexts .SingleOrDefault(context => context.ContextType == typeof(TDbContext)); if (config is null) return null; return new DbContextConfigurator(config); } /// /// Determines if the name of the currently logged-in user should be displayed in the top right corner of the admin ui /// public HopFrameConfigurator DisplayUserInfo(bool display) { InnerConfig.DisplayUserInfo = display; return this; } /// /// Sets a default policy that every user needs to have in order to access the admin ui /// public HopFrameConfigurator SetBasePolicy(string basePolicy) { InnerConfig.BasePolicy = basePolicy; return this; } /// /// Sets a custom login page to redirect to if the request to the admin ui was unauthorized /// public HopFrameConfigurator SetLoginPage(string url) { InnerConfig.LoginPageRewrite = url; return this; } }