// ========================================================================== // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex UG (haftungsbeschränkt) // All rights reserved. Licensed under the MIT license. // ========================================================================== using System; using System.Collections.Generic; using System.Threading.Tasks; using Squidex.Domain.Apps.Core.Contents; using Squidex.Domain.Apps.Core.EnrichContent; using Squidex.Domain.Apps.Core.Schemas; using Squidex.Domain.Apps.Core.Scripting; using Squidex.Domain.Apps.Core.ValidateContent; using Squidex.Domain.Apps.Entities.Apps; using Squidex.Domain.Apps.Entities.Assets.Repositories; using Squidex.Domain.Apps.Entities.Contents.Commands; using Squidex.Domain.Apps.Entities.Contents.Repositories; using Squidex.Domain.Apps.Entities.Schemas; using Squidex.Infrastructure.Queries; using Squidex.Infrastructure.Tasks; namespace Squidex.Domain.Apps.Entities.Contents { public sealed class ContentOperationContext { private IContentRepository contentRepository; private IAssetRepository assetRepository; private IScriptEngine scriptEngine; private ISchemaEntity schemaEntity; private IAppEntity appEntity; private ContentCommand command; private Guid schemaId; private Func message; public ISchemaEntity Schema { get { return schemaEntity; } } public static async Task CreateAsync( Guid appId, Guid schemaId, ContentCommand command, IAppProvider appProvider, IAssetRepository assetRepository, IContentRepository contentRepository, IScriptEngine scriptEngine, Func message) { var (appEntity, schemaEntity) = await appProvider.GetAppWithSchemaAsync(appId, schemaId); var context = new ContentOperationContext { appEntity = appEntity, assetRepository = assetRepository, command = command, contentRepository = contentRepository, message = message, schemaId = schemaId, schemaEntity = schemaEntity, scriptEngine = scriptEngine }; return context; } public Task EnrichAsync(NamedContentData data) { data.Enrich(schemaEntity.SchemaDef, appEntity.PartitionResolver()); return TaskHelper.Done; } public Task ValidateAsync(NamedContentData data) { var ctx = CreateValidationContext(); return data.ValidateAsync(ctx, schemaEntity.SchemaDef, appEntity.PartitionResolver(), message); } public Task ValidatePartialAsync(NamedContentData data) { var ctx = CreateValidationContext(); return data.ValidatePartialAsync(ctx, schemaEntity.SchemaDef, appEntity.PartitionResolver(), message); } public Task ExecuteScriptAndTransformAsync(Func script, ScriptContext context) { Enrich(context); var result = scriptEngine.ExecuteAndTransform(context, GetScript(script)); return Task.FromResult(result); } public Task ExecuteScriptAsync(Func script, ScriptContext context) { Enrich(context); scriptEngine.Execute(context, GetScript(script)); return TaskHelper.Done; } private void Enrich(ScriptContext context) { context.ContentId = command.ContentId; context.User = command.User; } private ValidationContext CreateValidationContext() { return new ValidationContext(command.ContentId, schemaId, QueryContentsAsync, QueryAssetsAsync); } private async Task> QueryAssetsAsync(IEnumerable assetIds) { return await assetRepository.QueryAsync(appEntity.Id, new HashSet(assetIds)); } private async Task> QueryContentsAsync(Guid filterSchemaId, FilterNode filterNode) { return await contentRepository.QueryIdsAsync(appEntity.Id, filterSchemaId, filterNode); } private string GetScript(Func script) { return script(schemaEntity.SchemaDef.Scripts); } } }