// ========================================================================== // ContentCommandHandler.cs // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex Group // All rights reserved. // ========================================================================== using System; using System.Collections.Generic; using System.Threading.Tasks; using Squidex.Core; using Squidex.Infrastructure; using Squidex.Infrastructure.CQRS.Commands; using Squidex.Infrastructure.Dispatching; using Squidex.Infrastructure.Tasks; using Squidex.Read.Apps.Services; using Squidex.Read.Schemas.Services; using Squidex.Write.Contents.Commands; namespace Squidex.Write.Contents { public class ContentCommandHandler : ICommandHandler { private readonly IAggregateHandler handler; private readonly IAppProvider appProvider; private readonly ISchemaProvider schemas; public ContentCommandHandler( IAggregateHandler handler, IAppProvider appProvider, ISchemaProvider schemas) { Guard.NotNull(handler, nameof(handler)); Guard.NotNull(schemas, nameof(schemas)); Guard.NotNull(appProvider, nameof(appProvider)); this.handler = handler; this.schemas = schemas; this.appProvider = appProvider; } protected async Task On(CreateContent command, CommandContext context) { await ValidateAsync(command, () => "Failed to create content", true); await handler.CreateAsync(context, c => { c.Create(command); context.Succeed(EntityCreatedResult.Create(command.Data, c.Version)); }); } protected async Task On(UpdateContent command, CommandContext context) { await ValidateAsync(command, () => "Failed to update content"); await handler.UpdateAsync(context, c => c.Update(command)); } protected async Task On(PatchContent command, CommandContext context) { await ValidateAsync(command, () => "Failed to patch content"); await handler.UpdateAsync(context, c => c.Patch(command)); } protected Task On(PublishContent command, CommandContext context) { return handler.UpdateAsync(context, c => c.Publish(command)); } protected Task On(UnpublishContent command, CommandContext context) { return handler.UpdateAsync(context, c => c.Unpublish(command)); } protected Task On(DeleteContent command, CommandContext context) { return handler.UpdateAsync(context, c => c.Delete(command)); } public Task HandleAsync(CommandContext context) { return context.IsHandled ? TaskHelper.False : this.DispatchActionAsync(context.Command, context); } private async Task ValidateAsync(ContentDataCommand command, Func message, bool enrich = false) { Guard.Valid(command, nameof(command), message); var taskForApp = appProvider.FindAppByIdAsync(command.AppId.Id); var taskForSchema = schemas.FindSchemaByIdAsync(command.SchemaId.Id); await Task.WhenAll(taskForApp, taskForSchema); var languages = new HashSet(taskForApp.Result.Languages); var schemaObject = taskForSchema.Result.Schema; var schemaErrors = new List(); await command.Data.ValidateAsync(schemaObject, languages, schemaErrors); if (schemaErrors.Count > 0) { throw new ValidationException(message(), schemaErrors); } if (enrich) { command.Data.Enrich(schemaObject, languages); } } } }