mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
2.3 KiB
73 lines
2.3 KiB
// ==========================================================================
|
|
// ContentEnricher.cs
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex Group
|
|
// All rights reserved.
|
|
// ==========================================================================
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
using Squidex.Domain.Apps.Core.Contents;
|
|
using Squidex.Domain.Apps.Core.Schemas;
|
|
using Squidex.Infrastructure;
|
|
using Squidex.Infrastructure.Json;
|
|
|
|
namespace Squidex.Domain.Apps.Core
|
|
{
|
|
public sealed class ContentEnricher<T>
|
|
{
|
|
private readonly Schema schema;
|
|
private readonly PartitionResolver partitionResolver;
|
|
|
|
public ContentEnricher(Schema schema, PartitionResolver partitionResolver)
|
|
{
|
|
Guard.NotNull(schema, nameof(schema));
|
|
Guard.NotNull(partitionResolver, nameof(partitionResolver));
|
|
|
|
this.schema = schema;
|
|
|
|
this.partitionResolver = partitionResolver;
|
|
}
|
|
|
|
public void Enrich(ContentData<T> data)
|
|
{
|
|
Guard.NotNull(data, nameof(data));
|
|
|
|
foreach (var field in schema.Fields)
|
|
{
|
|
var fieldKey = data.GetKey(field);
|
|
var fieldData = data.GetOrCreate(fieldKey, k => new ContentFieldData());
|
|
var fieldPartition = partitionResolver(field.Paritioning);
|
|
|
|
foreach (var partitionItem in fieldPartition)
|
|
{
|
|
Enrich(field, fieldData, partitionItem);
|
|
}
|
|
|
|
if (fieldData.Count > 0)
|
|
{
|
|
data[fieldKey] = fieldData;
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void Enrich(Field field, ContentFieldData fieldData, IFieldPartitionItem partitionItem)
|
|
{
|
|
Guard.NotNull(fieldData, nameof(fieldData));
|
|
|
|
var defaultValue = field.RawProperties.GetDefaultValue();
|
|
|
|
if (field.RawProperties.IsRequired || defaultValue.IsNull())
|
|
{
|
|
return;
|
|
}
|
|
|
|
var key = partitionItem.Key;
|
|
|
|
if (!fieldData.TryGetValue(key, out JToken value) || value == null || value.Type == JTokenType.Null)
|
|
{
|
|
fieldData.AddValue(key, defaultValue);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|