// ========================================================================== // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex UG (haftungsbeschraenkt) // All rights reserved. Licensed under the MIT license. // ========================================================================== using System.Linq; using NodaTime; using Squidex.Areas.Api.Controllers.Schemas.Models; using Squidex.Domain.Apps.Core.Contents; using Squidex.Domain.Apps.Core.ConvertContent; using Squidex.Domain.Apps.Entities.Contents; using Squidex.Infrastructure; using Squidex.Infrastructure.Reflection; using Squidex.Infrastructure.Validation; using Squidex.Web; namespace Squidex.Areas.Api.Controllers.Contents.Models { public sealed class ContentDto : Resource { /// /// The if of the content item. /// public DomainId Id { get; set; } /// /// The user that has created the content item. /// [LocalizedRequired] public RefToken CreatedBy { get; set; } /// /// The user that has updated the content item. /// [LocalizedRequired] public RefToken LastModifiedBy { get; set; } /// /// The data of the content item. /// [LocalizedRequired] public object Data { get; set; } /// /// The reference data for the frontend UI. /// public ContentData? ReferenceData { get; set; } /// /// The date and time when the content item has been created. /// public Instant Created { get; set; } /// /// The date and time when the content item has been modified last. /// public Instant LastModified { get; set; } /// /// The status of the content. /// public Status Status { get; set; } /// /// The new status of the content. /// public Status? NewStatus { get; set; } /// /// The color of the status. /// public string StatusColor { get; set; } /// /// The color of the new status. /// public string? NewStatusColor { get; set; } /// /// The scheduled status. /// public ScheduleJobDto? ScheduleJob { get; set; } /// /// The id of the schema. /// public DomainId SchemaId { get; set; } /// /// The name of the schema. /// public string? SchemaName { get; set; } /// /// The display name of the schema. /// public string? SchemaDisplayName { get; set; } /// /// The reference fields. /// public FieldDto[]? ReferenceFields { get; set; } /// /// The version of the content. /// public long Version { get; set; } public static ContentDto FromContent(IEnrichedContentEntity content, Resources resources) { var response = SimpleMapper.Map(content, new ContentDto { SchemaId = content.SchemaId.Id, SchemaName = content.SchemaId.Name }); if (resources.Context.ShouldFlatten()) { response.Data = content.Data.ToFlatten(); } else { response.Data = content.Data; } if (content.ReferenceFields != null) { response.ReferenceFields = content.ReferenceFields.Select(FieldDto.FromField).ToArray(); } if (content.ScheduleJob != null) { response.ScheduleJob = new ScheduleJobDto { Color = content.ScheduledStatusColor! }; SimpleMapper.Map(content.ScheduleJob, response.ScheduleJob); } return response.CreateLinksAsync(content, resources, content.SchemaId.Name); } private ContentDto CreateLinksAsync(IEnrichedContentEntity content, Resources resources, string schema) { var app = resources.App; var values = new { app, schema, id = Id }; AddSelfLink(resources.Url(x => nameof(x.GetContent), values)); if (Version > 0) { var versioned = new { app, schema, values.id, version = Version - 1 }; AddGetLink("previous", resources.Url(x => nameof(x.GetContentVersion), versioned)); } if (NewStatus != null) { if (resources.CanDeleteContentVersion(schema)) { AddDeleteLink("draft/delete", resources.Url(x => nameof(x.DeleteVersion), values)); } } else if (Status == Status.Published) { if (resources.CanCreateContentVersion(schema)) { AddPostLink("draft/create", resources.Url(x => nameof(x.CreateDraft), values)); } } if (content.NextStatuses != null && resources.CanChangeStatus(schema)) { foreach (var next in content.NextStatuses) { AddPutLink($"status/{next.Status}", resources.Url(x => nameof(x.PutContentStatus), values), next.Color); } } if (content.ScheduleJob != null && resources.CanCancelContentStatus(schema)) { AddDeleteLink($"cancel", resources.Url(x => nameof(x.DeleteContentStatus), values)); } if (content.IsSingleton == false && resources.CanDeleteContent(schema)) { AddDeleteLink("delete", resources.Url(x => nameof(x.DeleteContent), values)); } if (content.CanUpdate && resources.CanUpdateContent(schema)) { AddPatchLink("patch", resources.Url(x => nameof(x.PatchContent), values)); } if (content.CanUpdate && resources.CanUpdateContent(schema)) { AddPutLink("update", resources.Url(x => nameof(x.PutContent), values)); } return this; } } }