using Asp.Versioning; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Umbraco.Cms.Api.Common.ViewModels.Pagination; using Umbraco.Cms.Api.Management.ViewModels; using Umbraco.Cms.Core; using Umbraco.Cms.Core.Mapping; using Umbraco.Cms.Core.Models; using Umbraco.Cms.Core.Services; using Umbraco.Cms.Core.Services.OperationStatus; namespace Umbraco.Cms.Api.Management.Controllers.Document.References; [ApiVersion("1.0")] public class ReferencedDescendantsDocumentController : DocumentControllerBase { private readonly ITrackedReferencesService _trackedReferencesSkipTakeService; private readonly IUmbracoMapper _umbracoMapper; public ReferencedDescendantsDocumentController( ITrackedReferencesService trackedReferencesSkipTakeService, IUmbracoMapper umbracoMapper) { _trackedReferencesSkipTakeService = trackedReferencesSkipTakeService; _umbracoMapper = umbracoMapper; } [Obsolete("Use the ReferencedDescendants2 action method instead. Scheduled for removal in Umbraco 19, when ReferencedDescendants2 will be renamed back to ReferencedDescendants.")] [NonAction] public async Task>> ReferencedDescendants( CancellationToken cancellationToken, Guid id, int skip = 0, int take = 20) { PagedModel relationItems = await _trackedReferencesSkipTakeService.GetPagedDescendantsInReferencesAsync(id, skip, take, true); var pagedViewModel = new PagedViewModel { Total = relationItems.Total, Items = _umbracoMapper.MapEnumerable(relationItems.Items), }; return pagedViewModel; } /// /// Gets a paged list of the descendant nodes of the current item used in any kind of relation. /// /// /// Used when deleting and unpublishing a single item to check if this item has any descending items that are in any /// kind of relation. /// This is basically finding the descending items which are children in relations. /// [HttpGet("{id:guid}/referenced-descendants")] [MapToApiVersion("1.0")] [ProducesResponseType(typeof(PagedViewModel), StatusCodes.Status200OK)] [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)] public async Task ReferencedDescendants2( CancellationToken cancellationToken, Guid id, int skip = 0, int take = 20) { Attempt, GetReferencesOperationStatus> relationItemsAttempt = await _trackedReferencesSkipTakeService.GetPagedDescendantsInReferencesAsync(id, UmbracoObjectTypes.Document, skip, take, true); if (relationItemsAttempt.Success is false) { return GetReferencesOperationStatusResult(relationItemsAttempt.Status); } var pagedViewModel = new PagedViewModel { Total = relationItemsAttempt.Result.Total, Items = _umbracoMapper.MapEnumerable(relationItemsAttempt.Result.Items), }; return Ok(pagedViewModel); } }