Browse Source

Middleware fixed.

pull/250/head
Sebastian Stehle 8 years ago
parent
commit
5cd6ae9058
  1. 29
      src/Squidex/Pipeline/CommandMiddlewares/EnrichWithAppIdCommandMiddleware.cs
  2. 52
      src/Squidex/Pipeline/CommandMiddlewares/EnrichWithSchemaIdCommandMiddleware.cs

29
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<IAppFeature>();
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<Guid>(appFeature.App.Id, appFeature.App.Name);
appSelfCommand.AppId = appId.Id;
}
return next();
}
private NamedId<Guid> GetAppId()
{
var appFeature = httpContextAccessor.HttpContext.Features.Get<IAppFeature>();
if (appFeature?.App == null)
{
throw new InvalidOperationException("Cannot resolve app.");
}
return new NamedId<Guid>(appFeature.App.Id, appFeature.App.Name);
}
}
}
}

52
src/Squidex/Pipeline/CommandMiddlewares/EnrichWithSchemaIdCommandMiddleware.cs

@ -36,28 +36,42 @@ namespace Squidex.Pipeline.CommandMiddlewares
if (context.Command is ISchemaCommand schemaCommand && schemaCommand.SchemaId == null)
{
NamedId<Guid> 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<IAppFeature>();
if (context.Command is SchemaCommand schemaSelfCommand && schemaSelfCommand.SchemaId == Guid.Empty)
{
var schemaId = await GetSchemaIdAsync(context);
if (appFeature != null && appFeature.App != null)
{
appId = new NamedId<Guid>(appFeature.App.Id, appFeature.App.Name);
}
}
schemaSelfCommand.SchemaId = schemaId?.Id ?? Guid.Empty;
}
await next();
}
private async Task<NamedId<Guid>> GetSchemaIdAsync(CommandContext context)
{
NamedId<Guid> appId = null;
if (context.Command is IAppCommand appCommand)
{
appId = appCommand.AppId;
}
if (appId == null)
if (appId == null)
{
var appFeature = actionContextAccessor.ActionContext.HttpContext.Features.Get<IAppFeature>();
if (appFeature != null && appFeature.App != null)
{
return;
appId = new NamedId<Guid>(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<Guid>(schema.Id, schema.Name);
return new NamedId<Guid>(schema.Id, schema.Name);
}
}
await next();
return null;
}
}
}
}
Loading…
Cancel
Save