Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add endpoint for retrieving the published (if any) version of a document #17278

Merged
merged 3 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Asp.Versioning;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Api.Management.Factories;
using Umbraco.Cms.Api.Management.ViewModels.Document;
using Umbraco.Cms.Core.Actions;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Security.Authorization;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Web.Common.Authorization;
using Umbraco.Extensions;

namespace Umbraco.Cms.Api.Management.Controllers.Document;

[ApiVersion("1.0")]
public class ByKeyPublishedDocumentController : DocumentControllerBase
{
private readonly IAuthorizationService _authorizationService;
private readonly IContentEditingService _contentEditingService;
private readonly IDocumentPresentationFactory _documentPresentationFactory;

public ByKeyPublishedDocumentController(
IAuthorizationService authorizationService,
IContentEditingService contentEditingService,
IDocumentPresentationFactory documentPresentationFactory)
{
_authorizationService = authorizationService;
_contentEditingService = contentEditingService;
_documentPresentationFactory = documentPresentationFactory;
}

[HttpGet("{id:guid}/published")]
[MapToApiVersion("1.0")]
[ProducesResponseType(typeof(PublishedDocumentResponseModel), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
public async Task<IActionResult> ByKeyPublished(CancellationToken cancellationToken, Guid id)
{
AuthorizationResult authorizationResult = await _authorizationService.AuthorizeResourceAsync(
User,
ContentPermissionResource.WithKeys(ActionBrowse.ActionLetter, id),
AuthorizationPolicies.ContentPermissionByResource);

if (!authorizationResult.Succeeded)
{
return Forbidden();
}

IContent? content = await _contentEditingService.GetAsync(id);
if (content == null || content.Published is false)
{
return DocumentNotFound();
}

PublishedDocumentResponseModel model = await _documentPresentationFactory.CreatePublishedResponseModelAsync(content);
return Ok(model);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,23 @@
return responseModel;
}

public async Task<PublishedDocumentResponseModel> CreatePublishedResponseModelAsync(IContent content)
{
PublishedDocumentResponseModel responseModel = _umbracoMapper.Map<PublishedDocumentResponseModel>(content)!;

responseModel.Urls = await _documentUrlFactory.CreateUrlsAsync(content);

Guid? templateKey = content.PublishTemplateId.HasValue
? _templateService.GetAsync(content.PublishTemplateId.Value).Result?.Key
: null;

responseModel.Template = templateKey.HasValue
? new ReferenceByIdModel { Id = templateKey.Value }
: null;

return responseModel;
}

Check warning on line 72 in src/Umbraco.Cms.Api.Management/Factories/DocumentPresentationFactory.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (release/15.0)

❌ New issue: Code Duplication

The module contains 2 functions with similar structure: CreatePublishedResponseModelAsync,CreateResponseModelAsync. Avoid duplicated, aka copy-pasted, code inside the module. More duplication lowers the code health.

public DocumentItemResponseModel CreateItemResponseModel(IDocumentEntitySlim entity)
{
var responseModel = new DocumentItemResponseModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public interface IDocumentPresentationFactory
{
Task<DocumentResponseModel> CreateResponseModelAsync(IContent content);

Task<PublishedDocumentResponseModel> CreatePublishedResponseModelAsync(IContent content);

DocumentItemResponseModel CreateItemResponseModel(IDocumentEntitySlim entity);

DocumentBlueprintItemResponseModel CreateBlueprintItemResponseModel(IDocumentEntitySlim entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public abstract class ContentMapDefinition<TContent, TValueViewModel, TVariantVi

protected delegate void VariantViewModelMapping(string? culture, string? segment, TVariantViewModel variantViewModel);

protected IEnumerable<TValueViewModel> MapValueViewModels(IEnumerable<IProperty> properties, ValueViewModelMapping? additionalPropertyMapping = null) =>
protected IEnumerable<TValueViewModel> MapValueViewModels(IEnumerable<IProperty> properties, ValueViewModelMapping? additionalPropertyMapping = null, bool published = false) =>
properties
.SelectMany(property => property
.Values
Expand All @@ -31,12 +31,19 @@ protected IEnumerable<TValueViewModel> MapValueViewModels(IEnumerable<IProperty>
return null;
}

IProperty? publishedProperty = null;
if (published)
{
publishedProperty = new Property(property.PropertyType);
publishedProperty.SetValue(propertyValue.PublishedValue, propertyValue.Culture, propertyValue.Segment);
}

var variantViewModel = new TValueViewModel
{
Culture = propertyValue.Culture,
Segment = propertyValue.Segment,
Alias = property.Alias,
Value = propertyEditor.GetValueEditor().ToEditor(property, propertyValue.Culture, propertyValue.Segment),
Value = propertyEditor.GetValueEditor().ToEditor(publishedProperty ?? property, propertyValue.Culture, propertyValue.Segment),
EditorAlias = propertyEditor.Alias
};
additionalPropertyMapping?.Invoke(propertyEditor, variantViewModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public DocumentMapDefinition(PropertyEditorCollection propertyEditorCollection,
public void DefineMaps(IUmbracoMapper mapper)
{
mapper.Define<IContent, DocumentResponseModel>((_, _) => new DocumentResponseModel(), Map);
mapper.Define<IContent, PublishedDocumentResponseModel>((_, _) => new PublishedDocumentResponseModel(), Map);
mapper.Define<IContent, DocumentCollectionResponseModel>((_, _) => new DocumentCollectionResponseModel(), Map);
mapper.Define<IContent, DocumentBlueprintResponseModel>((_, _) => new DocumentBlueprintResponseModel(), Map);
}
Expand All @@ -44,6 +45,28 @@ private void Map(IContent source, DocumentResponseModel target, MapperContext co
target.IsTrashed = source.Trashed;
}

// Umbraco.Code.MapAll -Urls -Template
private void Map(IContent source, PublishedDocumentResponseModel target, MapperContext context)
{
target.Id = source.Key;
target.DocumentType = context.Map<DocumentTypeReferenceResponseModel>(source.ContentType)!;
target.Values = MapValueViewModels(source.Properties, published: true);
target.Variants = MapVariantViewModels(
source,
(culture, _, documentVariantViewModel) =>
{
documentVariantViewModel.Name = source.GetPublishName(culture) ?? documentVariantViewModel.Name;
DocumentVariantState variantState = DocumentVariantStateHelper.GetState(source, culture);
documentVariantViewModel.State = variantState == DocumentVariantState.PublishedPendingChanges
? DocumentVariantState.Published
: variantState;
documentVariantViewModel.PublishDate = culture == null
? source.PublishDate
: source.GetPublishDate(culture);
});
target.IsTrashed = source.Trashed;
}

// Umbraco.Code.MapAll
private void Map(IContent source, DocumentCollectionResponseModel target, MapperContext context)
{
Expand Down
126 changes: 126 additions & 0 deletions src/Umbraco.Cms.Api.Management/OpenApi.json
Original file line number Diff line number Diff line change
Expand Up @@ -8967,6 +8967,66 @@
]
}
},
"/umbraco/management/api/v1/document/{id}/published": {
"get": {
"tags": [
"Document"
],
"operationId": "GetDocumentByIdPublished",
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"schema": {
"type": "string",
"format": "uuid"
}
}
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"oneOf": [
{
"$ref": "#/components/schemas/PublishedDocumentResponseModel"
}
]
}
}
}
},
"404": {
"description": "Not Found",
"content": {
"application/json": {
"schema": {
"oneOf": [
{
"$ref": "#/components/schemas/ProblemDetails"
}
]
}
}
}
},
"401": {
"description": "The resource is protected and requires an authentication token"
},
"403": {
"description": "The authenticated user do not have access to this resource"
}
},
"security": [
{
"Backoffice User": [ ]
}
]
}
},
"/umbraco/management/api/v1/document/{id}/referenced-by": {
"get": {
"tags": [
Expand Down Expand Up @@ -42471,6 +42531,72 @@
},
"additionalProperties": false
},
"PublishedDocumentResponseModel": {
"required": [
"documentType",
"id",
"isTrashed",
"urls",
"values",
"variants"
],
"type": "object",
"properties": {
"values": {
"type": "array",
"items": {
"oneOf": [
{
"$ref": "#/components/schemas/DocumentValueResponseModel"
}
]
}
},
"variants": {
"type": "array",
"items": {
"oneOf": [
{
"$ref": "#/components/schemas/DocumentVariantResponseModel"
}
]
}
},
"id": {
"type": "string",
"format": "uuid"
},
"documentType": {
"oneOf": [
{
"$ref": "#/components/schemas/DocumentTypeReferenceResponseModel"
}
]
},
"urls": {
"type": "array",
"items": {
"oneOf": [
{
"$ref": "#/components/schemas/DocumentUrlInfoModel"
}
]
}
},
"template": {
"oneOf": [
{
"$ref": "#/components/schemas/ReferenceByIdModel"
}
],
"nullable": true
},
"isTrashed": {
"type": "boolean"
}
},
"additionalProperties": false
},
"RedirectStatusModel": {
"enum": [
"Enabled",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Umbraco.Cms.Api.Management.ViewModels.Document;

public class PublishedDocumentResponseModel : DocumentResponseModel
{
}
Loading