Browse Source

Middleware fixed.

pull/250/head
Sebastian Stehle 9 years ago
parent
commit
5cd6ae9058
  1. 27
      src/Squidex/Pipeline/CommandMiddlewares/EnrichWithAppIdCommandMiddleware.cs
  2. 50
      src/Squidex/Pipeline/CommandMiddlewares/EnrichWithSchemaIdCommandMiddleware.cs

27
src/Squidex/Pipeline/CommandMiddlewares/EnrichWithAppIdCommandMiddleware.cs

@ -9,6 +9,7 @@ using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Squidex.Domain.Apps.Entities; using Squidex.Domain.Apps.Entities;
using Squidex.Domain.Apps.Entities.Apps.Commands;
using Squidex.Infrastructure; using Squidex.Infrastructure;
using Squidex.Infrastructure.Commands; using Squidex.Infrastructure.Commands;
@ -32,17 +33,31 @@ namespace Squidex.Pipeline.CommandMiddlewares
if (context.Command is IAppCommand appCommand && appCommand.AppId == null) if (context.Command is IAppCommand appCommand && appCommand.AppId == null)
{ {
var appFeature = httpContextAccessor.HttpContext.Features.Get<IAppFeature>(); var appId = GetAppId();
if (appFeature == null) appCommand.AppId = appId;
{ }
throw new InvalidOperationException("Cannot resolve app.");
} 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(); 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);
}
} }
} }

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

@ -36,28 +36,42 @@ namespace Squidex.Pipeline.CommandMiddlewares
if (context.Command is ISchemaCommand schemaCommand && schemaCommand.SchemaId == null) if (context.Command is ISchemaCommand schemaCommand && schemaCommand.SchemaId == null)
{ {
NamedId<Guid> appId = null; var schemaId = await GetSchemaIdAsync(context);
if (context.Command is IAppCommand appCommand) schemaCommand.SchemaId = schemaId;
{ }
appId = appCommand.AppId;
}
if (appId == null) if (context.Command is SchemaCommand schemaSelfCommand && schemaSelfCommand.SchemaId == Guid.Empty)
{ {
var appFeature = actionContextAccessor.ActionContext.HttpContext.Features.Get<IAppFeature>(); var schemaId = await GetSchemaIdAsync(context);
if (appFeature != null && appFeature.App != null) schemaSelfCommand.SchemaId = schemaId?.Id ?? Guid.Empty;
{ }
appId = new NamedId<Guid>(appFeature.App.Id, appFeature.App.Name);
} 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; var routeValues = actionContextAccessor.ActionContext.RouteData.Values;
if (routeValues.ContainsKey("name")) if (routeValues.ContainsKey("name"))
@ -77,14 +91,14 @@ namespace Squidex.Pipeline.CommandMiddlewares
if (schema == null) 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