mirror of https://github.com/Squidex/squidex.git
38 changed files with 823 additions and 192 deletions
@ -0,0 +1,42 @@ |
|||
// ==========================================================================
|
|||
// 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.Core.Contents; |
|||
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 data = new NamedContentData(); |
|||
|
|||
var contentId = Guid.Empty; |
|||
var content = new CreateContent { Data = data, ContentId = contentId, SchemaId = schemaId, Publish = true }; |
|||
|
|||
SimpleMapper.Map(context.Command, content); |
|||
|
|||
await context.CommandBus.PublishAsync(content); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Binary file not shown.
|
Before Width: | Height: | Size: 75 KiB After Width: | Height: | Size: 78 KiB |
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 2.7 KiB |
|
After Width: | Height: | Size: 7.3 KiB |
@ -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