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.
57 lines
1.8 KiB
57 lines
1.8 KiB
// ==========================================================================
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|
// All rights reserved. Licensed under the MIT license.
|
|
// ==========================================================================
|
|
|
|
using NodaTime;
|
|
using Squidex.Domain.Apps.Core.Contents;
|
|
using Squidex.Domain.Apps.Core.Schemas;
|
|
using Squidex.Infrastructure;
|
|
|
|
namespace Squidex.Domain.Apps.Core.DefaultValues;
|
|
|
|
public static class DefaultValueExtensions
|
|
{
|
|
public static void GenerateDefaultValues(this ContentData data, Schema schema, PartitionResolver partitionResolver)
|
|
{
|
|
Guard.NotNull(schema);
|
|
Guard.NotNull(partitionResolver);
|
|
|
|
foreach (var field in schema.Fields)
|
|
{
|
|
var fieldData = data.GetOrCreate(field.Name, _ => new ContentFieldData());
|
|
|
|
if (fieldData != null)
|
|
{
|
|
var partitioning = partitionResolver(field.Partitioning);
|
|
|
|
foreach (var partitionKey in partitioning.AllKeys)
|
|
{
|
|
Enrich(field, fieldData, partitionKey);
|
|
}
|
|
|
|
if (fieldData.Count > 0)
|
|
{
|
|
data[field.Name] = fieldData;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void Enrich(IField field, ContentFieldData fieldData, string partitionKey)
|
|
{
|
|
var defaultValue = DefaultValueFactory.CreateDefaultValue(field, SystemClock.Instance.GetCurrentInstant(), partitionKey);
|
|
|
|
if (field.RawProperties.IsRequired || defaultValue == default)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!fieldData.TryGetValue(partitionKey, out _))
|
|
{
|
|
fieldData.AddLocalized(partitionKey, defaultValue);
|
|
}
|
|
}
|
|
}
|
|
|