// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschränkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System;
using System.ComponentModel.DataAnnotations;
using NodaTime;
using Squidex.Areas.Api.Controllers.Contents;
using Squidex.Domain.Apps.Entities.Schemas;
using Squidex.Infrastructure;
using Squidex.Infrastructure.Reflection;
using Squidex.Shared;
using Squidex.Web;
namespace Squidex.Areas.Api.Controllers.Schemas.Models
{
public class SchemaDto : Resource, IGenerateETag
{
///
/// The id of the schema.
///
public Guid Id { get; set; }
///
/// The name of the schema. Unique within the app.
///
[Required]
[RegularExpression("^[a-z0-9]+(\\-[a-z0-9]+)*$")]
public string Name { get; set; }
///
/// The name of the category.
///
public string Category { get; set; }
///
/// The schema properties.
///
[Required]
public SchemaPropertiesDto Properties { get; set; } = new SchemaPropertiesDto();
///
/// Indicates if the schema is a singleton.
///
public bool IsSingleton { get; set; }
///
/// Indicates if the schema is published.
///
public bool IsPublished { get; set; }
///
/// The user that has created the schema.
///
[Required]
public RefToken CreatedBy { get; set; }
///
/// The user that has updated the schema.
///
[Required]
public RefToken LastModifiedBy { get; set; }
///
/// The date and time when the schema has been created.
///
public Instant Created { get; set; }
///
/// The date and time when the schema has been modified last.
///
public Instant LastModified { get; set; }
///
/// The version of the schema.
///
public long Version { get; set; }
public static SchemaDto FromSchema(ISchemaEntity schema, ApiController controller, string app)
{
var result = new SchemaDto();
SimpleMapper.Map(schema, result);
SimpleMapper.Map(schema.SchemaDef, result);
SimpleMapper.Map(schema.SchemaDef.Properties, result.Properties);
return result.CreateLinks(controller, app);
}
protected virtual SchemaDto CreateLinks(ApiController controller, string app)
{
var values = new { app, name = Name };
AddSelfLink(controller.Url(x => nameof(x.GetSchema), values));
if (controller.HasPermission(Permissions.AppContentsRead, app, Name))
{
AddGetLink("contents", controller.Url(x => nameof(x.GetContents), values));
}
return this;
}
}
}