// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System.Collections.Generic;
using Squidex.Domain.Apps.Core.Schemas;
using Squidex.Domain.Apps.Entities.Schemas.Commands;
using Squidex.Infrastructure.Reflection;
namespace Squidex.Areas.Api.Controllers.Schemas.Models
{
public abstract class UpsertSchemaDto
{
///
/// The optional properties.
///
public SchemaPropertiesDto? Properties { get; set; }
///
/// The optional scripts.
///
public SchemaScriptsDto? Scripts { get; set; }
///
/// The names of the fields that should be used in references.
///
public List? FieldsInReferences { get; set; }
///
/// The names of the fields that should be shown in lists, including meta fields.
///
public List? FieldsInLists { get; set; }
///
/// Optional fields.
///
public List? Fields { get; set; }
///
/// The optional preview urls.
///
public Dictionary? PreviewUrls { get; set; }
///
/// The category.
///
public string? Category { get; set; }
///
/// Set it to true to autopublish the schema.
///
public bool IsPublished { get; set; }
public static TCommand ToCommand(TDto dto, TCommand command) where TCommand : UpsertCommand where TDto : UpsertSchemaDto
{
SimpleMapper.Map(dto, command);
if (dto.Properties != null)
{
command.Properties = new SchemaProperties();
SimpleMapper.Map(dto.Properties, command.Properties);
}
if (dto.Scripts != null)
{
command.Scripts = new SchemaScripts();
SimpleMapper.Map(dto.Scripts, command.Scripts);
}
if (dto.FieldsInLists != null)
{
command.FieldsInLists = new FieldNames(dto.FieldsInLists);
}
if (dto.FieldsInReferences != null)
{
command.FieldsInReferences = new FieldNames(dto.FieldsInReferences);
}
if (dto.Fields != null)
{
command.Fields = new List();
foreach (var rootFieldDto in dto.Fields)
{
var rootProperties = rootFieldDto?.Properties?.ToProperties();
var rootField = new UpsertSchemaField { Properties = rootProperties! };
if (rootFieldDto != null)
{
SimpleMapper.Map(rootFieldDto, rootField);
if (rootFieldDto?.Nested?.Count > 0)
{
rootField.Nested = new List();
foreach (var nestedFieldDto in rootFieldDto.Nested)
{
var nestedProperties = nestedFieldDto?.Properties?.ToProperties();
var nestedField = new UpsertSchemaNestedField { Properties = nestedProperties! };
if (nestedFieldDto != null)
{
SimpleMapper.Map(nestedFieldDto, nestedField);
}
rootField.Nested.Add(nestedField);
}
}
}
command.Fields.Add(rootField);
}
}
return command;
}
}
}