// ==========================================================================
// 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 System.Linq;
using Squidex.Domain.Apps.Core.Contents;
using Squidex.Domain.Apps.Entities;
using Squidex.Domain.Apps.Entities.Contents;
using Squidex.Infrastructure;
using Squidex.Shared;
using Squidex.Web;
namespace Squidex.Areas.Api.Controllers.Contents.Models
{
public sealed class ContentsDto : Resource
{
///
/// The total number of content items.
///
public long Total { get; set; }
///
/// The content items.
///
[Required]
public ContentDto[] Items { get; set; }
///
/// The possible statuses.
///
[Required]
public string[] Statuses { get; set; }
public string ToEtag()
{
return Items.ToManyEtag(Total);
}
public string ToSurrogateKeys()
{
return Items.ToSurrogateKeys();
}
public static ContentsDto FromContents(long total, IEnumerable contents, QueryContext context,
ApiController controller,
string app,
string schema)
{
var result = new ContentsDto
{
Total = total,
Items = contents.Select(x => ContentDto.FromContent(x, context, controller, app, schema)).ToArray(),
};
result.Statuses = new string[] { "Archived", "Draft", "Published" };
return result.CreateLinks(controller, app, schema);
}
private ContentsDto CreateLinks(ApiController controller, string app, string schema)
{
if (schema != null)
{
var values = new { app, name = schema };
AddSelfLink(controller.Url(x => nameof(x.GetContents), values));
if (controller.HasPermission(Permissions.AppContentsCreate, app, schema))
{
AddPostLink("create", controller.Url(x => nameof(x.PostContent), values));
if (controller.HasPermission(Helper.StatusPermission(app, schema, Status.Published)))
{
AddPostLink("create/publish", controller.Url(x => nameof(x.PostContent), values) + "?publish=true");
}
}
}
return this;
}
}
}