diff --git a/src/Squidex/Pipeline/CommandMiddlewares/EnrichWithAppIdCommandMiddleware.cs b/src/Squidex/Pipeline/CommandMiddlewares/EnrichWithAppIdCommandMiddleware.cs index 1a063c723..8f724b1a9 100644 --- a/src/Squidex/Pipeline/CommandMiddlewares/EnrichWithAppIdCommandMiddleware.cs +++ b/src/Squidex/Pipeline/CommandMiddlewares/EnrichWithAppIdCommandMiddleware.cs @@ -9,6 +9,7 @@ using System; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Squidex.Domain.Apps.Entities; +using Squidex.Domain.Apps.Entities.Apps.Commands; using Squidex.Infrastructure; using Squidex.Infrastructure.Commands; @@ -32,17 +33,31 @@ namespace Squidex.Pipeline.CommandMiddlewares if (context.Command is IAppCommand appCommand && appCommand.AppId == null) { - var appFeature = httpContextAccessor.HttpContext.Features.Get(); + var appId = GetAppId(); - if (appFeature == null) - { - throw new InvalidOperationException("Cannot resolve app."); - } + appCommand.AppId = appId; + } + + if (context.Command is AppCommand appSelfCommand && appSelfCommand.AppId == Guid.Empty) + { + var appId = GetAppId(); - appCommand.AppId = new NamedId(appFeature.App.Id, appFeature.App.Name); + appSelfCommand.AppId = appId.Id; } return next(); } + + private NamedId GetAppId() + { + var appFeature = httpContextAccessor.HttpContext.Features.Get(); + + if (appFeature?.App == null) + { + throw new InvalidOperationException("Cannot resolve app."); + } + + return new NamedId(appFeature.App.Id, appFeature.App.Name); + } } -} +} \ No newline at end of file diff --git a/src/Squidex/Pipeline/CommandMiddlewares/EnrichWithSchemaIdCommandMiddleware.cs b/src/Squidex/Pipeline/CommandMiddlewares/EnrichWithSchemaIdCommandMiddleware.cs index 495131855..40b660c8a 100644 --- a/src/Squidex/Pipeline/CommandMiddlewares/EnrichWithSchemaIdCommandMiddleware.cs +++ b/src/Squidex/Pipeline/CommandMiddlewares/EnrichWithSchemaIdCommandMiddleware.cs @@ -36,28 +36,42 @@ namespace Squidex.Pipeline.CommandMiddlewares if (context.Command is ISchemaCommand schemaCommand && schemaCommand.SchemaId == null) { - NamedId appId = null; + var schemaId = await GetSchemaIdAsync(context); - if (context.Command is IAppCommand appCommand) - { - appId = appCommand.AppId; - } + schemaCommand.SchemaId = schemaId; + } - if (appId == null) - { - var appFeature = actionContextAccessor.ActionContext.HttpContext.Features.Get(); + if (context.Command is SchemaCommand schemaSelfCommand && schemaSelfCommand.SchemaId == Guid.Empty) + { + var schemaId = await GetSchemaIdAsync(context); - if (appFeature != null && appFeature.App != null) - { - appId = new NamedId(appFeature.App.Id, appFeature.App.Name); - } - } + schemaSelfCommand.SchemaId = schemaId?.Id ?? Guid.Empty; + } + + await next(); + } + + private async Task> GetSchemaIdAsync(CommandContext context) + { + NamedId appId = null; + + if (context.Command is IAppCommand appCommand) + { + appId = appCommand.AppId; + } - if (appId == null) + if (appId == null) + { + var appFeature = actionContextAccessor.ActionContext.HttpContext.Features.Get(); + + if (appFeature != null && appFeature.App != null) { - return; + appId = new NamedId(appFeature.App.Id, appFeature.App.Name); } + } + if (appId != null) + { var routeValues = actionContextAccessor.ActionContext.RouteData.Values; if (routeValues.ContainsKey("name")) @@ -77,14 +91,14 @@ namespace Squidex.Pipeline.CommandMiddlewares if (schema == null) { - throw new DomainObjectNotFoundException(schemaName, typeof(SchemaDomainObject)); + throw new DomainObjectNotFoundException(schemaName, typeof(ISchemaEntity)); } - schemaCommand.SchemaId = new NamedId(schema.Id, schema.Name); + return new NamedId(schema.Id, schema.Name); } } - await next(); + return null; } } -} +} \ No newline at end of file