// ========================================================================== // 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.Domain.Apps.Core.Contents; using Squidex.Domain.Apps.Core.ConvertContent; using Squidex.Domain.Apps.Entities; using Squidex.Domain.Apps.Entities.Contents; using Squidex.Infrastructure; using Squidex.Infrastructure.Reflection; using Squidex.Shared; using Squidex.Web; namespace Squidex.Areas.Api.Controllers.Contents.Models { public sealed class ContentDto : Resource, IGenerateETag { /// /// The if of the content item. /// public Guid Id { get; set; } /// /// The user that has created the content item. /// [Required] public RefToken CreatedBy { get; set; } /// /// The user that has updated the content item. /// [Required] public RefToken LastModifiedBy { get; set; } /// /// The data of the content item. /// [Required] public object Data { get; set; } /// /// The pending changes of the content item. /// public object DataDraft { get; set; } /// /// Indicates if the draft data is pending. /// public bool IsPending { get; set; } /// /// The scheduled status. /// public ScheduleJobDto ScheduleJob { 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 the status of the content. /// public Status Status { get; set; } /// /// The version of the content. /// public long Version { get; set; } public static ContentDto FromContent(IContentEntity content, QueryContext context, ApiController controller, string app, string schema) { var response = SimpleMapper.Map(content, new ContentDto()); if (context?.Flatten == true) { response.Data = content.Data?.ToFlatten(); response.DataDraft = content.DataDraft?.ToFlatten(); } else { response.Data = content.Data; response.DataDraft = content.DataDraft; } if (content.ScheduleJob != null) { response.ScheduleJob = SimpleMapper.Map(content.ScheduleJob, new ScheduleJobDto()); } return response.CreateLinks(controller, app, schema); } private ContentDto CreateLinks(ApiController controller, string app, string schema) { var values = new { app, name = schema, id = Id }; AddSelfLink(controller.Url(x => nameof(x.GetContent), values)); if (Version > 0) { var versioned = new { app, name = schema, id = Id, version = Version - 1 }; AddGetLink("prev", controller.Url(x => nameof(x.GetContentVersion), versioned)); } if (IsPending) { if (controller.HasPermission(Permissions.AppContentsDiscard, app, schema)) { AddPutLink("draft/discard", controller.Url(x => nameof(x.DiscardDraft), values)); } if (controller.HasPermission(Helper.StatusPermission(app, schema, Status.Published))) { AddPutLink("draft/publish", controller.Url(x => nameof(x.PutContentStatus), values)); } } if (controller.HasPermission(Permissions.AppContentsUpdate, app, schema)) { AddPutLink("update", controller.Url(x => nameof(x.PutContent), values)); if (Status == Status.Published) { AddPutLink("draft/propose", controller.Url(x => nameof(x.PutContent), values) + "?asDraft=true"); } AddPatchLink("patch", controller.Url(x => nameof(x.PatchContent), values)); } if (controller.HasPermission(Permissions.AppContentsDelete, app, schema)) { AddPutLink("delete", controller.Url(x => nameof(x.DeleteContent), values)); } foreach (var next in StatusFlow.Next(Status)) { if (controller.HasPermission(Helper.StatusPermission(app, schema, next))) { AddPutLink($"status/{next}", controller.Url(x => nameof(x.PutContentStatus), values)); } } return this; } } }