// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschränkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Squidex.Areas.Api.Controllers.Schemas.Models.Fields;
using Squidex.Web;
namespace Squidex.Areas.Api.Controllers.Schemas.Models
{
public sealed class FieldDto : Resource
{
///
/// The id of the field.
///
public long FieldId { get; set; }
///
/// The name of the field. Must be unique within the schema.
///
[Required]
[RegularExpression("^[a-z0-9]+(\\-[a-z0-9]+)*$")]
public string Name { get; set; }
///
/// Defines if the field is hidden.
///
public bool IsHidden { get; set; }
///
/// Defines if the field is locked.
///
public bool IsLocked { get; set; }
///
/// Defines if the field is disabled.
///
public bool IsDisabled { get; set; }
///
/// Defines the partitioning of the field.
///
[Required]
public string Partitioning { get; set; }
///
/// The field properties.
///
[Required]
public FieldPropertiesDto Properties { get; set; }
///
/// The nested fields.
///
public List Nested { get; set; }
public void CreateLinks(ApiController controller, string app, string schema, bool allowUpdate)
{
allowUpdate = allowUpdate && !IsLocked;
if (allowUpdate)
{
var values = new { app, name = schema, id = FieldId };
AddPutLink("update", controller.Url(x => nameof(x.PutField), values));
if (IsHidden)
{
AddPutLink("show", controller.Url(x => nameof(x.ShowField), values));
}
else
{
AddPutLink("hide", controller.Url(x => nameof(x.HideField), values));
}
if (IsDisabled)
{
AddPutLink("enable", controller.Url(x => nameof(x.EnableField), values));
}
else
{
AddPutLink("disable", controller.Url(x => nameof(x.DisableField), values));
}
if (Properties is ArrayFieldPropertiesDto)
{
var parentValues = new { app, name = schema, parentId = FieldId };
AddPostLink("fields/add", controller.Url(x => nameof(x.PostNestedField), parentValues));
AddPutLink("fields/order", controller.Url(x => nameof(x.PutNestedFieldOrdering), parentValues));
}
AddPutLink("lock", controller.Url(x => nameof(x.LockField), values));
AddDeleteLink("delete", controller.Url(x => nameof(x.DeleteField), values));
}
if (Nested != null)
{
foreach (var nested in Nested)
{
nested.CreateLinks(controller, app, schema, FieldId, allowUpdate);
}
}
}
}
}