Skip to content

Commit fd77074

Browse files
AndyButlandCopilot
andauthored
Added management API endpoint, service and repository for retrieval of references from the recycle bin (#18882)
* Added management API endpoint, service and repository for retrieval of references from the recycle bin. * Update src/Umbraco.Cms.Api.Management/Controllers/Document/RecycleBin/ReferencedByDocumentRecycleBinController.cs Co-authored-by: Copilot <[email protected]> * Removed unused code. --------- Co-authored-by: Copilot <[email protected]>
1 parent bf74636 commit fd77074

File tree

9 files changed

+350
-8
lines changed

9 files changed

+350
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using Asp.Versioning;
2+
using Microsoft.AspNetCore.Http;
3+
using Microsoft.AspNetCore.Mvc;
4+
using Umbraco.Cms.Api.Common.ViewModels.Pagination;
5+
using Umbraco.Cms.Api.Management.Factories;
6+
using Umbraco.Cms.Api.Management.ViewModels.TrackedReferences;
7+
using Umbraco.Cms.Core.Models;
8+
using Umbraco.Cms.Core.Services;
9+
10+
namespace Umbraco.Cms.Api.Management.Controllers.Document.RecycleBin;
11+
12+
[ApiVersion("1.0")]
13+
public class ReferencedByDocumentRecycleBinController : DocumentRecycleBinControllerBase
14+
{
15+
private readonly ITrackedReferencesService _trackedReferencesService;
16+
private readonly IRelationTypePresentationFactory _relationTypePresentationFactory;
17+
18+
public ReferencedByDocumentRecycleBinController(
19+
IEntityService entityService,
20+
IDocumentPresentationFactory documentPresentationFactory,
21+
ITrackedReferencesService trackedReferencesService,
22+
IRelationTypePresentationFactory relationTypePresentationFactory)
23+
: base(entityService, documentPresentationFactory)
24+
{
25+
_trackedReferencesService = trackedReferencesService;
26+
_relationTypePresentationFactory = relationTypePresentationFactory;
27+
}
28+
29+
/// <summary>
30+
/// Gets a paged list of tracked references for all items in the document recycle bin, so you can see where an item is being used.
31+
/// </summary>
32+
[HttpGet("referenced-by")]
33+
[MapToApiVersion("1.0")]
34+
[ProducesResponseType(typeof(PagedViewModel<IReferenceResponseModel>), StatusCodes.Status200OK)]
35+
public async Task<ActionResult<PagedViewModel<IReferenceResponseModel>>> ReferencedBy(
36+
CancellationToken cancellationToken,
37+
int skip = 0,
38+
int take = 20)
39+
{
40+
PagedModel<RelationItemModel> relationItems = await _trackedReferencesService.GetPagedRelationsForRecycleBinAsync(UmbracoObjectTypes.Document, skip, take, true);
41+
42+
var pagedViewModel = new PagedViewModel<IReferenceResponseModel>
43+
{
44+
Total = relationItems.Total,
45+
Items = await _relationTypePresentationFactory.CreateReferenceResponseModelsAsync(relationItems.Items),
46+
};
47+
48+
return pagedViewModel;
49+
}
50+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using Asp.Versioning;
2+
using Microsoft.AspNetCore.Http;
3+
using Microsoft.AspNetCore.Mvc;
4+
using Umbraco.Cms.Api.Common.ViewModels.Pagination;
5+
using Umbraco.Cms.Api.Management.Factories;
6+
using Umbraco.Cms.Api.Management.ViewModels.TrackedReferences;
7+
using Umbraco.Cms.Core.Models;
8+
using Umbraco.Cms.Core.Services;
9+
10+
namespace Umbraco.Cms.Api.Management.Controllers.Media.RecycleBin;
11+
12+
[ApiVersion("1.0")]
13+
public class ReferencedByMediaRecycleBinController : MediaRecycleBinControllerBase
14+
{
15+
private readonly ITrackedReferencesService _trackedReferencesService;
16+
private readonly IRelationTypePresentationFactory _relationTypePresentationFactory;
17+
18+
public ReferencedByMediaRecycleBinController(
19+
IEntityService entityService,
20+
IMediaPresentationFactory mediaPresentationFactory,
21+
ITrackedReferencesService trackedReferencesService,
22+
IRelationTypePresentationFactory relationTypePresentationFactory)
23+
: base(entityService, mediaPresentationFactory)
24+
{
25+
_trackedReferencesService = trackedReferencesService;
26+
_relationTypePresentationFactory = relationTypePresentationFactory;
27+
}
28+
29+
/// <summary>
30+
/// Gets a paged list of tracked references for all items in the media recycle bin, so you can see where an item is being used.
31+
/// </summary>
32+
[HttpGet("referenced-by")]
33+
[MapToApiVersion("1.0")]
34+
[ProducesResponseType(typeof(PagedViewModel<IReferenceResponseModel>), StatusCodes.Status200OK)]
35+
public async Task<ActionResult<PagedViewModel<IReferenceResponseModel>>> ReferencedBy(
36+
CancellationToken cancellationToken,
37+
int skip = 0,
38+
int take = 20)
39+
{
40+
PagedModel<RelationItemModel> relationItems = await _trackedReferencesService.GetPagedRelationsForRecycleBinAsync(UmbracoObjectTypes.Media, skip, take, true);
41+
42+
var pagedViewModel = new PagedViewModel<IReferenceResponseModel>
43+
{
44+
Total = relationItems.Total,
45+
Items = await _relationTypePresentationFactory.CreateReferenceResponseModelsAsync(relationItems.Items),
46+
};
47+
48+
return pagedViewModel;
49+
}
50+
}

src/Umbraco.Cms.Api.Management/OpenApi.json

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10705,6 +10705,61 @@
1070510705
]
1070610706
}
1070710707
},
10708+
"/umbraco/management/api/v1/recycle-bin/document/referenced-by": {
10709+
"get": {
10710+
"tags": [
10711+
"Document"
10712+
],
10713+
"operationId": "GetRecycleBinDocumentReferencedBy",
10714+
"parameters": [
10715+
{
10716+
"name": "skip",
10717+
"in": "query",
10718+
"schema": {
10719+
"type": "integer",
10720+
"format": "int32",
10721+
"default": 0
10722+
}
10723+
},
10724+
{
10725+
"name": "take",
10726+
"in": "query",
10727+
"schema": {
10728+
"type": "integer",
10729+
"format": "int32",
10730+
"default": 20
10731+
}
10732+
}
10733+
],
10734+
"responses": {
10735+
"200": {
10736+
"description": "OK",
10737+
"content": {
10738+
"application/json": {
10739+
"schema": {
10740+
"oneOf": [
10741+
{
10742+
"$ref": "#/components/schemas/PagedIReferenceResponseModel"
10743+
}
10744+
]
10745+
}
10746+
}
10747+
}
10748+
},
10749+
"401": {
10750+
"description": "The resource is protected and requires an authentication token"
10751+
},
10752+
"403": {
10753+
"description": "The authenticated user does not have access to this resource"
10754+
}
10755+
},
10756+
"security": [
10757+
{
10758+
"Backoffice User": [ ]
10759+
}
10760+
]
10761+
}
10762+
},
1070810763
"/umbraco/management/api/v1/recycle-bin/document/root": {
1070910764
"get": {
1071010765
"tags": [
@@ -17856,6 +17911,61 @@
1785617911
]
1785717912
}
1785817913
},
17914+
"/umbraco/management/api/v1/recycle-bin/media/referenced-by": {
17915+
"get": {
17916+
"tags": [
17917+
"Media"
17918+
],
17919+
"operationId": "GetRecycleBinMediaReferencedBy",
17920+
"parameters": [
17921+
{
17922+
"name": "skip",
17923+
"in": "query",
17924+
"schema": {
17925+
"type": "integer",
17926+
"format": "int32",
17927+
"default": 0
17928+
}
17929+
},
17930+
{
17931+
"name": "take",
17932+
"in": "query",
17933+
"schema": {
17934+
"type": "integer",
17935+
"format": "int32",
17936+
"default": 20
17937+
}
17938+
}
17939+
],
17940+
"responses": {
17941+
"200": {
17942+
"description": "OK",
17943+
"content": {
17944+
"application/json": {
17945+
"schema": {
17946+
"oneOf": [
17947+
{
17948+
"$ref": "#/components/schemas/PagedIReferenceResponseModel"
17949+
}
17950+
]
17951+
}
17952+
}
17953+
}
17954+
},
17955+
"401": {
17956+
"description": "The resource is protected and requires an authentication token"
17957+
},
17958+
"403": {
17959+
"description": "The authenticated user does not have access to this resource"
17960+
}
17961+
},
17962+
"security": [
17963+
{
17964+
"Backoffice User": [ ]
17965+
}
17966+
]
17967+
}
17968+
},
1785917969
"/umbraco/management/api/v1/recycle-bin/media/root": {
1786017970
"get": {
1786117971
"tags": [

src/Umbraco.Core/Persistence/Repositories/ITrackedReferencesRepository.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,29 @@ IEnumerable<RelationItemModel> GetPagedRelationsForItem(
7070
bool filterMustBeIsDependency,
7171
out long totalRecords);
7272

73+
/// <summary>
74+
/// Gets a paged result of items which are in relation with an item in the recycle bin.
75+
/// </summary>
76+
/// <param name="objectTypeKey">The Umbraco object type that has recycle bin support (currently Document or Media).</param>
77+
/// <param name="skip">The amount of items to skip.</param>
78+
/// <param name="take">The amount of items to take.</param>
79+
/// <param name="filterMustBeIsDependency">
80+
/// A boolean indicating whether to filter only the RelationTypes which are
81+
/// dependencies (isDependency field is set to true).
82+
/// </param>
83+
/// <param name="totalRecords">The total count of the items with reference to the current item.</param>
84+
/// <returns>An enumerable list of <see cref="RelationItem" /> objects.</returns>
85+
IEnumerable<RelationItemModel> GetPagedRelationsForRecycleBin(
86+
Guid objectTypeKey,
87+
long skip,
88+
long take,
89+
bool filterMustBeIsDependency,
90+
out long totalRecords)
91+
{
92+
totalRecords = 0;
93+
return [];
94+
}
95+
7396
[Obsolete("Use overload that takes key instead of id. This will be removed in Umbraco 15.")]
7497
IEnumerable<RelationItemModel> GetPagedRelationsForItem(
7598
int id,

src/Umbraco.Core/Services/ITrackedReferencesService.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ public interface ITrackedReferencesService
6464
/// <returns>A paged result of <see cref="RelationItemModel" /> objects.</returns>
6565
Task<PagedModel<RelationItemModel>> GetPagedRelationsForItemAsync(Guid key, long skip, long take, bool filterMustBeIsDependency);
6666

67+
/// <summary>
68+
/// Gets a paged result of items which are in relation with an item in the recycle bin.
69+
/// </summary>
70+
/// <param name="objectType">The Umbraco object type that has recycle bin support (currently Document or Media).</param>
71+
/// <param name="skip">The amount of items to skip</param>
72+
/// <param name="take">The amount of items to take.</param>
73+
/// <param name="filterMustBeIsDependency">
74+
/// A boolean indicating whether to filter only the RelationTypes which are
75+
/// dependencies (isDependency field is set to true).
76+
/// </param>
77+
/// <returns>A paged result of <see cref="RelationItemModel" /> objects.</returns>
78+
Task<PagedModel<RelationItemModel>> GetPagedRelationsForRecycleBinAsync(UmbracoObjectTypes objectType, long skip, long take, bool filterMustBeIsDependency)
79+
=> Task.FromResult(new PagedModel<RelationItemModel>(0, []));
80+
6781
[Obsolete("Use method that takes key (Guid) instead of id (int). This will be removed in Umbraco 15.")]
6882
PagedModel<RelationItemModel> GetPagedDescendantsInReferences(int parentId, long skip, long take, bool filterMustBeIsDependency);
6983

src/Umbraco.Core/Services/TrackedReferencesService.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,21 @@ public async Task<PagedModel<RelationItemModel>> GetPagedRelationsForItemAsync(G
9292
return await Task.FromResult(pagedModel);
9393
}
9494

95+
public async Task<PagedModel<RelationItemModel>> GetPagedRelationsForRecycleBinAsync(UmbracoObjectTypes objectType, long skip, long take, bool filterMustBeIsDependency)
96+
{
97+
Guid objectTypeKey = objectType switch
98+
{
99+
UmbracoObjectTypes.Document => Constants.ObjectTypes.Document,
100+
UmbracoObjectTypes.Media => Constants.ObjectTypes.Media,
101+
_ => throw new ArgumentOutOfRangeException(nameof(objectType), "Only documents and media have recycle bin support."),
102+
};
103+
104+
using ICoreScope scope = _scopeProvider.CreateCoreScope(autoComplete: true);
105+
IEnumerable<RelationItemModel> items = _trackedReferencesRepository.GetPagedRelationsForRecycleBin(objectTypeKey, skip, take, filterMustBeIsDependency, out var totalItems);
106+
var pagedModel = new PagedModel<RelationItemModel>(totalItems, items);
107+
return await Task.FromResult(pagedModel);
108+
}
109+
95110
[Obsolete("Use overload that takes key instead of id. This will be removed in Umbraco 15.")]
96111
public PagedModel<RelationItemModel> GetPagedDescendantsInReferences(int parentId, long skip, long take, bool filterMustBeIsDependency)
97112
{

0 commit comments

Comments
 (0)