@ -9,6 +9,7 @@ using System;
using System.Collections.Generic ;
using System.Linq ;
using System.Threading.Tasks ;
using Squidex.Domain.Apps.Core.Contents ;
using Squidex.Domain.Apps.Core.EnrichContent ;
using Squidex.Domain.Apps.Core.Scripting ;
using Squidex.Domain.Apps.Core.ValidateContent ;
@ -17,7 +18,6 @@ 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 ;
using Squidex.Infrastructure.Tasks ;
namespace Squidex.Domain.Apps.Entities.Contents
@ -31,6 +31,7 @@ namespace Squidex.Domain.Apps.Entities.Contents
private IScriptEngine scriptEngine ;
private ISchemaEntity schemaEntity ;
private IAppEntity appEntity ;
private Guid appId ;
private Func < string > message ;
public static async Task < ContentOperationContext > CreateAsync (
@ -56,6 +57,7 @@ namespace Squidex.Domain.Apps.Entities.Contents
var context = new ContentOperationContext
{
appEntity = appEntity ,
appId = a . Id ,
assetRepository = assetRepository ,
contentRepository = contentRepository ,
content = content ,
@ -78,54 +80,35 @@ namespace Squidex.Domain.Apps.Entities.Contents
return TaskHelper . Done ;
}
public async Task ValidateAsync ( bool partial )
public Task ValidateAsync ( )
{
if ( command is ContentDataCommand dataCommand )
{
var errors = new List < ValidationError > ( ) ;
var ctx =
new ValidationContext (
( contentIds , schemaId ) = >
{
return QueryContentsAsync ( content . AppId . Id , schemaId , contentIds ) ;
} ,
assetIds = >
{
return QueryAssetsAsync ( content . AppId . Id , assetIds ) ;
} ) ;
if ( partial )
{
await dataCommand . Data . ValidatePartialAsync ( ctx , schemaEntity . SchemaDef , appEntity . PartitionResolver ( ) , errors ) ;
}
else
{
await dataCommand . Data . ValidateAsync ( ctx , schemaEntity . SchemaDef , appEntity . PartitionResolver ( ) , errors ) ;
}
var ctx = CreateValidationContext ( ) ;
if ( errors . Count > 0 )
{
throw new ValidationException ( message ( ) , errors . ToArray ( ) ) ;
}
return dataCommand . Data . ValidateAsync ( ctx , schemaEntity . SchemaDef , appEntity . PartitionResolver ( ) , message ) ;
}
}
private async Task < IReadOnlyList < IAssetInfo > > QueryAssetsAsync ( Guid appId , IEnumerable < Guid > assetIds )
{
return await assetRepository . QueryAsync ( appId , new HashSet < Guid > ( assetIds ) ) ;
return TaskHelper . Done ;
}
private async Task < IReadOnlyList < Guid > > QueryContentsAsync ( Guid appId , Guid schemaId , IEnumerable < Guid > contentIds )
public Task ValidatePartialAsync ( )
{
return await contentRepository . QueryNotFoundAsync ( appId , schemaId , contentIds . ToList ( ) ) ;
if ( command is ContentDataCommand dataCommand )
{
var ctx = CreateValidationContext ( ) ;
return dataCommand . Data . ValidatePartialAsync ( ctx , schemaEntity . SchemaDef , appEntity . PartitionResolver ( ) , message ) ;
}
return TaskHelper . Done ;
}
public Task ExecuteScriptAndTransformAsync ( Func < ISchemaEntity , string > script , object operation )
{
if ( command is ContentDataCommand dataCommand )
{
var ctx = new ScriptContext { ContentId = content . Id , OldData = content . Data , User = command . User , Operation = operation . ToString ( ) , Data = dataCommand . Data } ;
var ctx = CreateScriptContext ( operation , dataCommand . Data ) ;
dataCommand . Data = scriptEngine . ExecuteAndTransform ( ctx , script ( schemaEntity ) ) ;
}
@ -135,11 +118,39 @@ namespace Squidex.Domain.Apps.Entities.Contents
public Task ExecuteScriptAsync ( Func < ISchemaEntity , string > script , object operation )
{
var ctx = new ScriptContext { ContentId = content . Id , OldData = content . Data , User = command . User , Operation = operation . ToString ( ) } ;
var ctx = CreateScriptContext ( operation , content . Data ) ;
scriptEngine . Execute ( ctx , script ( schemaEntity ) ) ;
return TaskHelper . Done ;
}
private ScriptContext CreateScriptContext ( object operation , NamedContentData data = null )
{
return new ScriptContext { ContentId = command . ContentId , OldData = content . Data , Data = data , User = command . User , Operation = operation . ToString ( ) } ;
}
private ValidationContext CreateValidationContext ( )
{
return new ValidationContext (
( contentIds , schemaId ) = >
{
return QueryContentsAsync ( schemaId , contentIds ) ;
} ,
assetIds = >
{
return QueryAssetsAsync ( assetIds ) ;
} ) ;
}
private async Task < IReadOnlyList < IAssetInfo > > QueryAssetsAsync ( IEnumerable < Guid > assetIds )
{
return await assetRepository . QueryAsync ( appId , new HashSet < Guid > ( assetIds ) ) ;
}
private async Task < IReadOnlyList < Guid > > QueryContentsAsync ( Guid schemaId , IEnumerable < Guid > contentIds )
{
return await contentRepository . QueryNotFoundAsync ( appId , schemaId , contentIds . ToList ( ) ) ;
}
}
}