From f7f5f3a8d964987c90467f0b149a1aa88964dd04 Mon Sep 17 00:00:00 2001 From: Tobias Klika Date: Sun, 12 Dec 2021 15:41:51 +0100 Subject: [PATCH] hmm --- .../{ => Abstractions}/ZeroApiController.cs | 2 +- .../ZeroApiEntityStoreController.cs | 8 ++-- .../ZeroApiTreeEntityStoreController.cs | 2 +- zero.Api/ApiRequestHints.cs | 2 +- zero.Api/ApplicationBuilderExtensions.cs | 12 +++++ zero.Api/Endpoints/Media/MediaController.cs | 2 +- zero.Api/Plugin.cs | 46 ++++++++++++++----- ...Extensions.cs => ZeroBuilderExtensions.cs} | 2 +- zero.Api/zero.Api.csproj | 15 ++++++ .../ZeroBackofficeController.cs | 2 +- .../{ => Controllers}/ZeroIndexController.cs | 0 .../{ => Controllers}/ZeroSetupController.cs | 0 zero.Backoffice/Endpoints/DebugController.cs | 17 ------- .../ZeroBackofficeControllerExtensions.cs | 2 +- zero.Backoffice/Usings.cs | 1 + ...ons.cs => ApplicationBuilderExtensions.cs} | 2 +- .../Blueprints/BlueprintChildInterceptor.cs | 2 +- zero.Core/Architecture/Plugins/IZeroPlugin.cs | 8 ++-- .../Plugins/ZeroPluginInitializer.cs | 2 +- .../Architecture/ZeroModuleCollection.cs | 45 ++++++++++++------ .../Configuration/ConfigurationModule.cs | 2 + zero.Core/Configuration/Constants.cs | 24 +++++----- zero.Core/Persistence/IdGenerator.cs | 9 ++++ .../Routing/ApplicationBuilderExtensions.cs | 13 ++++++ .../ZeroEnpointRouteBuilderExtensions.cs | 18 +++++--- ...ions.cs => ServiceCollectionExtensions.cs} | 2 +- zero.Core/Stores/EntityStore.cs | 4 +- zero.Core/Stores/StoreOperations.Read.cs | 5 +- zero.Core/Stores/StoreOperations.cs | 2 +- zero.Core/ZeroApplicationBuilder.cs | 12 +++++ zero.Core/ZeroBuilder.cs | 45 +++++++++--------- zero.Demo/Program.cs | 19 ++++---- 32 files changed, 209 insertions(+), 118 deletions(-) rename zero.Api/{ => Abstractions}/ZeroApiController.cs (94%) rename zero.Api/{ => Abstractions}/ZeroApiEntityStoreController.cs (95%) rename zero.Api/{ => Abstractions}/ZeroApiTreeEntityStoreController.cs (98%) create mode 100644 zero.Api/ApplicationBuilderExtensions.cs rename zero.Api/{ServiceCollectionExtensions.cs => ZeroBuilderExtensions.cs} (93%) rename zero.Backoffice/{ => Abstractions}/ZeroBackofficeController.cs (92%) rename zero.Backoffice/{ => Controllers}/ZeroIndexController.cs (100%) rename zero.Backoffice/{ => Controllers}/ZeroSetupController.cs (100%) delete mode 100644 zero.Backoffice/Endpoints/DebugController.cs rename zero.Core/{ZeroApplicationBuilderExtensions.cs => ApplicationBuilderExtensions.cs} (76%) create mode 100644 zero.Core/Routing/ApplicationBuilderExtensions.cs rename zero.Core/{ZeroServiceCollectionExtensions.cs => ServiceCollectionExtensions.cs} (90%) diff --git a/zero.Api/ZeroApiController.cs b/zero.Api/Abstractions/ZeroApiController.cs similarity index 94% rename from zero.Api/ZeroApiController.cs rename to zero.Api/Abstractions/ZeroApiController.cs index c3a0e44f..47ca1424 100644 --- a/zero.Api/ZeroApiController.cs +++ b/zero.Api/Abstractions/ZeroApiController.cs @@ -2,7 +2,7 @@ using Microsoft.Extensions.DependencyInjection; using zero.Api.Filters; -namespace zero.Api.Controllers; +namespace zero.Api.Abstractions; [ApiController] [ZeroAuthorize] diff --git a/zero.Api/ZeroApiEntityStoreController.cs b/zero.Api/Abstractions/ZeroApiEntityStoreController.cs similarity index 95% rename from zero.Api/ZeroApiEntityStoreController.cs rename to zero.Api/Abstractions/ZeroApiEntityStoreController.cs index cf487e31..bf565af5 100644 --- a/zero.Api/ZeroApiEntityStoreController.cs +++ b/zero.Api/Abstractions/ZeroApiEntityStoreController.cs @@ -1,7 +1,7 @@ using Microsoft.AspNetCore.Mvc; using Raven.Client.Documents.Indexes; -namespace zero.Api.Controllers; +namespace zero.Api.Abstractions; public abstract class ZeroApiEntityStoreController : ZeroApiController where TModel : ZeroEntity, new() @@ -47,7 +47,7 @@ public abstract class ZeroApiEntityStoreController : ZeroApiCont return NotFound(); } - HttpContext.Items[ApiConstants.ChangeVector] = Store.GetChangeVector(model); + HttpContext.Items[ApiConstants.ChangeVector] = Store.GetChangeToken(model); return Mapper.Map(model); } @@ -116,7 +116,9 @@ public abstract class ZeroApiEntityStoreController : ZeroApiCont return BadRequest(ApiConstants.HttpErrors.IdNotFound); } - if (!changeToken.IsNullOrEmpty() && Store.GetChangeVector(model) != changeToken) + string storedChangeToken = Store.GetChangeToken(model); + + if (!changeToken.IsNullOrEmpty() && storedChangeToken != changeToken) { return BadRequest(ApiConstants.HttpErrors.ChangeTokenMismatch); } diff --git a/zero.Api/ZeroApiTreeEntityStoreController.cs b/zero.Api/Abstractions/ZeroApiTreeEntityStoreController.cs similarity index 98% rename from zero.Api/ZeroApiTreeEntityStoreController.cs rename to zero.Api/Abstractions/ZeroApiTreeEntityStoreController.cs index 196db9d0..3c74e8ee 100644 --- a/zero.Api/ZeroApiTreeEntityStoreController.cs +++ b/zero.Api/Abstractions/ZeroApiTreeEntityStoreController.cs @@ -1,7 +1,7 @@ using Microsoft.AspNetCore.Mvc; using Raven.Client.Documents.Indexes; -namespace zero.Api.Controllers; +namespace zero.Api.Abstractions; public abstract class ZeroApiTreeEntityStoreController : ZeroApiEntityStoreController where TModel : ZeroEntity, ISupportsTrees, new() diff --git a/zero.Api/ApiRequestHints.cs b/zero.Api/ApiRequestHints.cs index d1533f5b..78c81b3f 100644 --- a/zero.Api/ApiRequestHints.cs +++ b/zero.Api/ApiRequestHints.cs @@ -2,7 +2,7 @@ public struct ApiRequestHints { - public ApiResponsePreference ResponsePreference { get; set; } + public ApiResponsePreference ResponsePreference { get; set; } = ApiResponsePreference.Representation; } diff --git a/zero.Api/ApplicationBuilderExtensions.cs b/zero.Api/ApplicationBuilderExtensions.cs new file mode 100644 index 00000000..c82f1a8c --- /dev/null +++ b/zero.Api/ApplicationBuilderExtensions.cs @@ -0,0 +1,12 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; + +namespace zero.Api; + +public static class ApplicationBuilderExtensions +{ + public static IApplicationBuilder UseZeroApi(this IApplicationBuilder builder) + { + return builder; + } +} \ No newline at end of file diff --git a/zero.Api/Endpoints/Media/MediaController.cs b/zero.Api/Endpoints/Media/MediaController.cs index eb58a5df..6deb8aa5 100644 --- a/zero.Api/Endpoints/Media/MediaController.cs +++ b/zero.Api/Endpoints/Media/MediaController.cs @@ -26,7 +26,7 @@ public class MediaController : ZeroApiTreeEntityStoreController PostConfigureServices = null; - public ZeroApiPlugin() { Options.Name = "zero.Api"; @@ -23,22 +24,43 @@ public class ZeroApiPlugin : ZeroPlugin services.AddOptions().Bind(configuration.GetSection("Zero:Api")).Configure(ConfigureOptions); services.TryAddEnumerable(ServiceDescriptor.Transient, ZeroApiMvcOptions>()); - services.AddScoped(); + ZeroModuleCollection modules = new(); - ZeroModuleCollection.AddModule(services, configuration); - ZeroModuleCollection.AddModule(services, configuration); - ZeroModuleCollection.AddModule(services, configuration); - ZeroModuleCollection.AddModule(services, configuration); - ZeroModuleCollection.AddModule(services, configuration); - ZeroModuleCollection.AddModule(services, configuration); - ZeroModuleCollection.AddModule(services, configuration); - ZeroModuleCollection.AddModule(services, configuration); - ZeroModuleCollection.AddModule(services, configuration); + modules.Add(); + modules.Add(); + modules.Add(); + modules.Add(); + modules.Add(); + modules.Add(); + modules.Add(); + modules.Add(); + modules.Add(); + modules.ConfigureServices(services, configuration); + PostConfigureServices?.Invoke(services, configuration); } + public override void Configure(IApplicationBuilder app, IEndpointRouteBuilder routes, IServiceProvider serviceProvider) + { + string path = "/zero/api"; + + app.UseWhen(ctx => ctx.Request.Path.StartsWithSegments(path), appScoped => + { + appScoped.UseEndpoints(endpoints => + { + //IZeroOptions options = app.ApplicationServices.GetService(); // TODO oO + // see https://our.umbraco.com/documentation/reference/routing/custom-routes#where-to-put-your-routing-logic + //string path = options.BackofficePath.EnsureStartsWith('/').TrimEnd('/'); + //endpoints.MapFallbackToController(path + "/{**path}", "Index", "ZeroIndex"); + + //endpoints.MapControllers(); + }); + }); + } + + protected void ConfigureOptions(ApiOptions options, IWebHostEnvironment env) { options.Search.Enabled = true; diff --git a/zero.Api/ServiceCollectionExtensions.cs b/zero.Api/ZeroBuilderExtensions.cs similarity index 93% rename from zero.Api/ServiceCollectionExtensions.cs rename to zero.Api/ZeroBuilderExtensions.cs index 52bf68a1..fb8819f4 100644 --- a/zero.Api/ServiceCollectionExtensions.cs +++ b/zero.Api/ZeroBuilderExtensions.cs @@ -2,7 +2,7 @@ namespace zero.Api; -public static class ServiceCollectionExtensions +public static class ZeroBuilderExtensions { public static ZeroBuilder AddApi(this ZeroBuilder builder) where T : ZeroApiPlugin, IZeroPlugin, new() { diff --git a/zero.Api/zero.Api.csproj b/zero.Api/zero.Api.csproj index 69b3e915..43d57c0d 100644 --- a/zero.Api/zero.Api.csproj +++ b/zero.Api/zero.Api.csproj @@ -9,9 +9,18 @@ + + + + + + + + + @@ -26,4 +35,10 @@ + + + + + + \ No newline at end of file diff --git a/zero.Backoffice/ZeroBackofficeController.cs b/zero.Backoffice/Abstractions/ZeroBackofficeController.cs similarity index 92% rename from zero.Backoffice/ZeroBackofficeController.cs rename to zero.Backoffice/Abstractions/ZeroBackofficeController.cs index e63759e0..41355928 100644 --- a/zero.Backoffice/ZeroBackofficeController.cs +++ b/zero.Backoffice/Abstractions/ZeroBackofficeController.cs @@ -2,7 +2,7 @@ using Microsoft.Extensions.DependencyInjection; using zero.Api.Controllers; -namespace zero.Backoffice.Controllers; +namespace zero.Backoffice.Abstractions; [ApiController] [ZeroAuthorize] diff --git a/zero.Backoffice/ZeroIndexController.cs b/zero.Backoffice/Controllers/ZeroIndexController.cs similarity index 100% rename from zero.Backoffice/ZeroIndexController.cs rename to zero.Backoffice/Controllers/ZeroIndexController.cs diff --git a/zero.Backoffice/ZeroSetupController.cs b/zero.Backoffice/Controllers/ZeroSetupController.cs similarity index 100% rename from zero.Backoffice/ZeroSetupController.cs rename to zero.Backoffice/Controllers/ZeroSetupController.cs diff --git a/zero.Backoffice/Endpoints/DebugController.cs b/zero.Backoffice/Endpoints/DebugController.cs deleted file mode 100644 index e9b26444..00000000 --- a/zero.Backoffice/Endpoints/DebugController.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using zero.Api.Filters; -using zero.Backoffice.Services; -using zero.Identity.Models; - -namespace zero.Backoffice; - -[ZeroSystemApi] -[ZeroAuthorize(false)] -public class DebugController : ZeroBackofficeController -{ - [HttpGet("icons")] - public async Task GetIcons([FromServices] IIconService icons) - { - return Ok(await icons.GetSets()); - } -} \ No newline at end of file diff --git a/zero.Backoffice/Extensions/ZeroBackofficeControllerExtensions.cs b/zero.Backoffice/Extensions/ZeroBackofficeControllerExtensions.cs index f6bb6bf2..c6bdb686 100644 --- a/zero.Backoffice/Extensions/ZeroBackofficeControllerExtensions.cs +++ b/zero.Backoffice/Extensions/ZeroBackofficeControllerExtensions.cs @@ -1,7 +1,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.StaticFiles; using System.IO; -using zero.Api.Controllers; +using zero.Api.Abstractions; namespace zero.Backoffice.Extensions; diff --git a/zero.Backoffice/Usings.cs b/zero.Backoffice/Usings.cs index a47d0c77..244aece1 100644 --- a/zero.Backoffice/Usings.cs +++ b/zero.Backoffice/Usings.cs @@ -22,6 +22,7 @@ global using zero.Routing; global using zero.Utils; global using zero.Mapper; +global using zero.Backoffice.Abstractions; global using zero.Backoffice.Configuration; global using zero.Backoffice.Controllers; global using zero.Backoffice.UIComposition; diff --git a/zero.Core/ZeroApplicationBuilderExtensions.cs b/zero.Core/ApplicationBuilderExtensions.cs similarity index 76% rename from zero.Core/ZeroApplicationBuilderExtensions.cs rename to zero.Core/ApplicationBuilderExtensions.cs index 06cafaf0..748ecebd 100644 --- a/zero.Core/ZeroApplicationBuilderExtensions.cs +++ b/zero.Core/ApplicationBuilderExtensions.cs @@ -2,7 +2,7 @@ namespace zero; -public static class ZeroApplicationBuilderExtensions +public static class ApplicationBuilderExtensions { public static IZeroApplicationBuilder UseZero(this IApplicationBuilder app) => new ZeroApplicationBuilder(app); } diff --git a/zero.Core/Architecture/Blueprints/BlueprintChildInterceptor.cs b/zero.Core/Architecture/Blueprints/BlueprintChildInterceptor.cs index 9f4e2990..cb921118 100644 --- a/zero.Core/Architecture/Blueprints/BlueprintChildInterceptor.cs +++ b/zero.Core/Architecture/Blueprints/BlueprintChildInterceptor.cs @@ -22,7 +22,7 @@ public class BlueprintChildInterceptor : Interceptor, IBlueprintInte { if (model.Blueprint == null) { - return default; + return Task.FromResult>(default); } InterceptorResult result = new(); diff --git a/zero.Core/Architecture/Plugins/IZeroPlugin.cs b/zero.Core/Architecture/Plugins/IZeroPlugin.cs index cb84696b..d7a3e424 100644 --- a/zero.Core/Architecture/Plugins/IZeroPlugin.cs +++ b/zero.Core/Architecture/Plugins/IZeroPlugin.cs @@ -1,4 +1,6 @@ -using Microsoft.Extensions.Configuration; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Routing; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; namespace zero.Architecture; @@ -9,7 +11,7 @@ public abstract class ZeroPlugin : IZeroPlugin public virtual void ConfigureServices(IServiceCollection services, IConfiguration configuration) { } - public virtual void Configure(IZeroOptions zero) { } + public virtual void Configure(IApplicationBuilder app, IEndpointRouteBuilder routes, IServiceProvider serviceProvider) { } } @@ -19,5 +21,5 @@ public interface IZeroPlugin void ConfigureServices(IServiceCollection services, IConfiguration configuration); - void Configure(IZeroOptions zero); + void Configure(IApplicationBuilder app, IEndpointRouteBuilder routes, IServiceProvider serviceProvider); } \ No newline at end of file diff --git a/zero.Core/Architecture/Plugins/ZeroPluginInitializer.cs b/zero.Core/Architecture/Plugins/ZeroPluginInitializer.cs index 1daca37f..2ac4332e 100644 --- a/zero.Core/Architecture/Plugins/ZeroPluginInitializer.cs +++ b/zero.Core/Architecture/Plugins/ZeroPluginInitializer.cs @@ -38,7 +38,7 @@ internal class ZeroPluginInitializer T plugin = new(); plugin.ConfigureServices(services, configuration); - services.Configure(opts => plugin.Configure(opts)); + //services.Configure(opts => plugin.Configure(opts)); } catch { diff --git a/zero.Core/Architecture/ZeroModuleCollection.cs b/zero.Core/Architecture/ZeroModuleCollection.cs index 77169d6b..8b3f1272 100644 --- a/zero.Core/Architecture/ZeroModuleCollection.cs +++ b/zero.Core/Architecture/ZeroModuleCollection.cs @@ -1,42 +1,38 @@ -using Microsoft.Extensions.Configuration; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Routing; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using System.Collections.Concurrent; namespace zero.Architecture; -public class ZeroModuleCollection +public class ZeroModuleCollection : ZeroModule { - static ConcurrentDictionary _modules = new(); - - - /// - /// Get all registered modules - /// - public static IEnumerable GetAll() => _modules.Values; + ConcurrentDictionary _modules = new(); /// /// Adds a zero module /// - public static void AddModule(IServiceCollection services, IConfiguration configuration) where T : class, IZeroModule, new() + public void Add() where T : class, IZeroModule, new() { - AddModule(services, configuration, new T()); + Add(new T()); } /// /// Adds a zero module /// - public static void AddModule(IServiceCollection services, IConfiguration configuration, T moduleInstance) where T : IZeroModule + public void Add(T moduleInstance) where T : IZeroModule { - AddModule(typeof(T), services, configuration, moduleInstance); + Add(typeof(T), moduleInstance); } /// /// Adds a zero module /// - public static void AddModule(Type moduleType, IServiceCollection services, IConfiguration configuration, IZeroModule moduleInstance) + public void Add(Type moduleType, IZeroModule moduleInstance) { if (_modules.ContainsKey(moduleType)) { @@ -44,7 +40,26 @@ public class ZeroModuleCollection } _modules.TryAdd(moduleType, moduleInstance); + } - moduleInstance.ConfigureServices(services, configuration); + + /// + public override void ConfigureServices(IServiceCollection services, IConfiguration configuration) + { + foreach (var module in _modules.OrderBy(x => x.Value.Order)) + { + Console.WriteLine(module.Key.ToString()); + module.Value.ConfigureServices(services, configuration); + } + } + + + /// + public override void Configure(IApplicationBuilder app, IEndpointRouteBuilder routes, IServiceProvider serviceProvider) + { + foreach (var module in _modules.OrderBy(x => x.Value.ConfigureOrder)) + { + module.Value.Configure(app, routes, serviceProvider); + } } } \ No newline at end of file diff --git a/zero.Core/Configuration/ConfigurationModule.cs b/zero.Core/Configuration/ConfigurationModule.cs index fde5dd90..b8c80c48 100644 --- a/zero.Core/Configuration/ConfigurationModule.cs +++ b/zero.Core/Configuration/ConfigurationModule.cs @@ -6,6 +6,8 @@ namespace zero.Configuration; public class ConfigurationModule : ZeroModule { + public override int Order => -1000; + public override void ConfigureServices(IServiceCollection services, IConfiguration configuration) { services.AddOptions().Configure((opts, svc) => diff --git a/zero.Core/Configuration/Constants.cs b/zero.Core/Configuration/Constants.cs index 00e8bf1c..fa96aa14 100644 --- a/zero.Core/Configuration/Constants.cs +++ b/zero.Core/Configuration/Constants.cs @@ -1,15 +1,15 @@ namespace zero.Configuration; -public static class Constants +public static partial class Constants { public const string ErrorFieldNone = "__zero_no_field"; - public static class Tabs + public static partial class Tabs { public const string General = "general"; } - public static class Auth + public static partial class Auth { public const string SystemUser = "system"; public const string DefaultScheme = "zeroScheme"; @@ -18,7 +18,7 @@ public static class Constants public const string BackofficeCookieName = "zero.be.session"; public const string DefaultCookieName = "zero.session"; - public static class Claims + public static partial class Claims { public const string IsZero = "zero.claim.iszero"; public const string IsSuper = "zero.claim.issuper"; @@ -35,9 +35,9 @@ public static class Constants } - public static class Permissions + public static partial class Permissions { - public static class Groups + public static partial class Groups { public const string Sections = "zero.permissions.groups.sections"; public const string Spaces = "zero.permissions.groups.spaces"; @@ -48,13 +48,13 @@ public static class Constants } - public static class Database + public static partial class Database { public const string ReservationPrefix = "zero."; public const string Expires = Raven.Client.Constants.Documents.Metadata.Expires; } - public static class Sections + public static partial class Sections { public const string Dashboard = "dashboard"; public const string Pages = "pages"; @@ -63,7 +63,7 @@ public static class Constants public const string Settings = "settings"; } - public static class Settings + public static partial class Settings { public const string Updates = "updates"; public const string Applications = "applications"; @@ -78,7 +78,7 @@ public static class Constants public const string CreatePlugin = "createplugin"; } - public static class PermissionCollections + public static partial class PermissionCollections { public const string Sections = "permissionCollectionSections"; public const string Spaces = "permissionCollectionSpaces"; @@ -86,14 +86,14 @@ public static class Constants public const string Modules = "permissionCollectionModules"; } - public static class Pages + public static partial class Pages { public const string FolderAlias = "zero.folder"; public const string DefaultRootPageTypeAlias = "root"; public const string PageRouteProviderAlias = "zero.pages"; } - public static class Routing + public static partial class Routing { public const string InternalRoutePrefix = "/__zero/"; diff --git a/zero.Core/Persistence/IdGenerator.cs b/zero.Core/Persistence/IdGenerator.cs index ea1b7aa8..3addb588 100644 --- a/zero.Core/Persistence/IdGenerator.cs +++ b/zero.Core/Persistence/IdGenerator.cs @@ -30,4 +30,13 @@ public class IdGenerator //return Guid.NewGuid().ToString(); } + + + /// + /// Creates a simple hash from a string + /// + public static string HashString(string value) + { + return value.GetHashCode().ToString().Replace("-", String.Empty); + } } diff --git a/zero.Core/Routing/ApplicationBuilderExtensions.cs b/zero.Core/Routing/ApplicationBuilderExtensions.cs new file mode 100644 index 00000000..20be284d --- /dev/null +++ b/zero.Core/Routing/ApplicationBuilderExtensions.cs @@ -0,0 +1,13 @@ +using Microsoft.AspNetCore.Builder; + +namespace zero.Routing; + +public static class ApplicationBuilderExtensions +{ + public static IApplicationBuilder UseZeroRouting(this IApplicationBuilder app) + { + app.UseStaticFiles(); + app.UseRouting(); + return app; + } +} diff --git a/zero.Core/Routing/ZeroEnpointRouteBuilderExtensions.cs b/zero.Core/Routing/ZeroEnpointRouteBuilderExtensions.cs index a194d21c..76cc63a9 100644 --- a/zero.Core/Routing/ZeroEnpointRouteBuilderExtensions.cs +++ b/zero.Core/Routing/ZeroEnpointRouteBuilderExtensions.cs @@ -4,14 +4,20 @@ namespace zero.Routing; public static class ZeroEndpointRouteBuilderExtensions { - public static IZeroEndpointRouteBuilder MapZeroBackoffice(this IZeroEndpointRouteBuilder endpoints, string path = "/zero") + public static void UseZeroBackoffice(this IApplicationBuilder app, string path = "/zero") { - //IZeroOptions options = app.ApplicationServices.GetService(); // TODO oO - // see https://our.umbraco.com/documentation/reference/routing/custom-routes#where-to-put-your-routing-logic - //string path = options.BackofficePath.EnsureStartsWith('/').TrimEnd('/'); + //app.UseWhen(ctx => ctx.Request.Path.StartsWithSegments(path + "/api"), app1 => + //{ + // app1.UseEndpoints(endpoints => + // { + // //IZeroOptions options = app.ApplicationServices.GetService(); // TODO oO + // // see https://our.umbraco.com/documentation/reference/routing/custom-routes#where-to-put-your-routing-logic + // //string path = options.BackofficePath.EnsureStartsWith('/').TrimEnd('/'); + // //endpoints.MapFallbackToController(path + "/{**path}", "Index", "ZeroIndex"); - endpoints.MapFallbackToController(path + "/{**path}", "Index", "ZeroIndex"); - return endpoints; + // //endpoints.MapControllers(); + // }); + //}); } diff --git a/zero.Core/ZeroServiceCollectionExtensions.cs b/zero.Core/ServiceCollectionExtensions.cs similarity index 90% rename from zero.Core/ZeroServiceCollectionExtensions.cs rename to zero.Core/ServiceCollectionExtensions.cs index e7347ec7..f854e89b 100644 --- a/zero.Core/ZeroServiceCollectionExtensions.cs +++ b/zero.Core/ServiceCollectionExtensions.cs @@ -3,7 +3,7 @@ using Microsoft.Extensions.DependencyInjection; namespace zero; -public static class ZeroServiceCollectionExtensions +public static class ServiceCollectionExtensions { public static ZeroBuilder AddZero(this IServiceCollection services, IConfiguration configuration) { diff --git a/zero.Core/Stores/EntityStore.cs b/zero.Core/Stores/EntityStore.cs index 9c0efb51..49c0b9ba 100644 --- a/zero.Core/Stores/EntityStore.cs +++ b/zero.Core/Stores/EntityStore.cs @@ -57,7 +57,7 @@ public abstract class EntityStore : IEntityStore where T : ZeroIdEntity, I public virtual IAsyncEnumerable Stream(Func, IQueryable> expression) => Operations.Stream(expression); /// - public virtual string GetChangeVector(T model) => Operations.GetChangeVector(model); + public virtual string GetChangeToken(T model) => Operations.GetChangeToken(model); /// public virtual Task> Create(T model) => Operations.Create(model, async m => await Validate(m)); @@ -147,7 +147,7 @@ public interface IEntityStore where T : ZeroIdEntity, new() /// /// Get the change vector for a model (Proxy to IAsyncDocumentSession.GetChangeVectorFor<>) /// - string GetChangeVector(T model); + string GetChangeToken(T model); /// /// Validates an entity in this collection diff --git a/zero.Core/Stores/StoreOperations.Read.cs b/zero.Core/Stores/StoreOperations.Read.cs index 6598ef6f..7df3ac69 100644 --- a/zero.Core/Stores/StoreOperations.Read.cs +++ b/zero.Core/Stores/StoreOperations.Read.cs @@ -105,8 +105,9 @@ public partial class StoreOperations : IStoreOperations /// - public virtual string GetChangeVector(T model) where T : ZeroIdEntity, new() + public virtual string GetChangeToken(T model) where T : ZeroIdEntity, new() { - return Session.Advanced.GetChangeVectorFor(model); + string changeVector = Session.Advanced.GetChangeVectorFor(model); + return IdGenerator.HashString(changeVector); } } \ No newline at end of file diff --git a/zero.Core/Stores/StoreOperations.cs b/zero.Core/Stores/StoreOperations.cs index 7ae76b9b..5dc2cd1b 100644 --- a/zero.Core/Stores/StoreOperations.cs +++ b/zero.Core/Stores/StoreOperations.cs @@ -173,7 +173,7 @@ public interface IStoreOperations /// /// Get the change vector for a model /// - string GetChangeVector(T model) where T : ZeroIdEntity, new(); + string GetChangeToken(T model) where T : ZeroIdEntity, new(); /// /// Creates an entity with an optional validator diff --git a/zero.Core/ZeroApplicationBuilder.cs b/zero.Core/ZeroApplicationBuilder.cs index 087c2a0f..13cbc150 100644 --- a/zero.Core/ZeroApplicationBuilder.cs +++ b/zero.Core/ZeroApplicationBuilder.cs @@ -47,3 +47,15 @@ public interface IZeroApplicationBuilder IZeroApplicationBuilder WithMiddleware(Action configure); } + + +public class ZeroApplicationBuilderContext +{ + +} + + +public class ZeroEndpointBuilderContext +{ + +} \ No newline at end of file diff --git a/zero.Core/ZeroBuilder.cs b/zero.Core/ZeroBuilder.cs index 944312ab..a0df17ca 100644 --- a/zero.Core/ZeroBuilder.cs +++ b/zero.Core/ZeroBuilder.cs @@ -13,6 +13,8 @@ public class ZeroBuilder public virtual IMvcBuilder Mvc { get; } + public ZeroModuleCollection Modules { get; } = new(); + readonly IConfiguration Configuration; readonly IZeroStartupOptions StartupOptions; @@ -33,24 +35,26 @@ public class ZeroBuilder // adds and discovers additional and built-in assemblies new AssemblyDiscovery(Mvc).Execute(StartupOptions.AssemblyDiscoveryRules); - AddModule(); - AddModule(); - AddModule(); - AddModule(); - AddModule(); - AddModule(); - AddModule(); - AddModule(); - AddModule(); - AddModule(); - AddModule(); - AddModule(); + Modules.Add(); + Modules.Add(); + Modules.Add(); + Modules.Add(); + Modules.Add(); + Modules.Add(); + Modules.Add(); + Modules.Add(); + Modules.Add(); + Modules.Add(); + Modules.Add(); + Modules.Add(); - AddModule(); - AddModule(); - AddModule(); - AddModule(); - AddModule(); + Modules.Add(); + Modules.Add(); + Modules.Add(); + Modules.Add(); + Modules.Add(); + + Modules.ConfigureServices(services, configuration); //if (Environment.GetEnvironmentVariable("DOTNET_WATCH") == "1") //{ @@ -72,13 +76,6 @@ public class ZeroBuilder } - public ZeroBuilder AddModule() where T : class, IZeroModule, new() - { - ZeroModuleCollection.AddModule(Services, Configuration); - return this; - } - - /// /// Adds a zero plugin /// diff --git a/zero.Demo/Program.cs b/zero.Demo/Program.cs index 7c72678b..c403a00b 100644 --- a/zero.Demo/Program.cs +++ b/zero.Demo/Program.cs @@ -13,13 +13,10 @@ var builder = WebApplication.CreateBuilder(args); builder.Services.AddRazorPages(); builder.Services.AddTransient(); -ZeroBuilder zero = builder.Services - .AddZero(builder.Configuration, opts => - { - opts.Mvc.AddApplicationPart(typeof(Program).Assembly); - }) - .AddApi() - .AddBackoffice(); +builder.Services + .AddZero(builder.Configuration) + .AddApi() + .AddBackoffice(); builder.Services.Configure(opts => { @@ -41,11 +38,13 @@ if (!app.Environment.IsDevelopment()) app.UseZero().WithEndpoints(x => { - x.MapControllerRoute("default", "api/{controller}/{action=Index}/{id?}"); + //x.MapControllerRoute("default", "api/{controller}/{action=Index}/{id?}"); x.MapRazorPages(); x.MapZeroRoutes(); - x.MapZeroBackoffice(); - //x.MapFallbackToController("Index", "Error"); + //x.MapZeroBackoffice(); + x.MapFallbackToController("Index", "Error"); }); +//app.UseZeroBackoffice(); + app.Run();