mirror of https://github.com/Squidex/squidex.git
6 changed files with 189 additions and 25 deletions
@ -0,0 +1,36 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using Squidex.Domain.Apps.Entities.Contents.Commands; |
||||
|
using Squidex.Domain.Apps.Entities.Schemas.Commands; |
||||
|
using Squidex.Infrastructure; |
||||
|
using Squidex.Infrastructure.Commands; |
||||
|
using Squidex.Infrastructure.Reflection; |
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Entities.Contents |
||||
|
{ |
||||
|
public sealed class SingletonCommandMiddleware : ICommandMiddleware |
||||
|
{ |
||||
|
public async Task HandleAsync(CommandContext context, Func<Task> next) |
||||
|
{ |
||||
|
await next(); |
||||
|
|
||||
|
if (context.IsCompleted && |
||||
|
context.Command is CreateSchema createSchema && |
||||
|
createSchema.Singleton) |
||||
|
{ |
||||
|
var schemaId = new NamedId<Guid>(createSchema.SchemaId, createSchema.Name); |
||||
|
|
||||
|
var command = SimpleMapper.Map(createSchema, new CreateContent { ContentId = Guid.Empty, SchemaId = schemaId, Publish = true }); |
||||
|
|
||||
|
await context.CommandBus.PublishAsync(command); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,60 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Threading.Tasks; |
||||
|
using FakeItEasy; |
||||
|
using Squidex.Domain.Apps.Entities.Contents.Commands; |
||||
|
using Squidex.Domain.Apps.Entities.Schemas.Commands; |
||||
|
using Squidex.Infrastructure.Commands; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Entities.Contents |
||||
|
{ |
||||
|
public sealed class SingletonCommandMiddlewareTests |
||||
|
{ |
||||
|
private readonly ICommandBus commandBus = A.Fake<ICommandBus>(); |
||||
|
private readonly SingletonCommandMiddleware sut = new SingletonCommandMiddleware(); |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_create_content_when_singleton_schema_is_created() |
||||
|
{ |
||||
|
var context = |
||||
|
new CommandContext(new CreateSchema { Singleton = true, Name = "my-schema" }, commandBus) |
||||
|
.Complete(); |
||||
|
|
||||
|
await sut.HandleAsync(context); |
||||
|
|
||||
|
A.CallTo(() => commandBus.PublishAsync(A<ICommand>.That.Matches(x => x is CreateContent))) |
||||
|
.MustHaveHappened(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_not_create_content_when_non_singleton_schema_is_created() |
||||
|
{ |
||||
|
var context = |
||||
|
new CommandContext(new CreateSchema { Singleton = false }, commandBus) |
||||
|
.Complete(); |
||||
|
|
||||
|
await sut.HandleAsync(context); |
||||
|
|
||||
|
A.CallTo(() => commandBus.PublishAsync(A<ICommand>.Ignored)) |
||||
|
.MustNotHaveHappened(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_not_create_content_when_singleton_schema_not_created() |
||||
|
{ |
||||
|
var context = |
||||
|
new CommandContext(new CreateSchema { Singleton = true }, commandBus); |
||||
|
|
||||
|
await sut.HandleAsync(context); |
||||
|
|
||||
|
A.CallTo(() => commandBus.PublishAsync(A<ICommand>.Ignored)) |
||||
|
.MustNotHaveHappened(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue