Skip to content

Commit 1f4c19d

Browse files
AndyButlandCopilotnikolajlauridsen
authored
Updated management API endpoint and model for data type references to align with that used for documents, media etc. (#18905)
* Updated management API endpoint and model for data type references to align with that used for documents, media etc. * Refactoring. * Update src/Umbraco.Core/Constants-ReferenceTypes.cs Co-authored-by: Copilot <[email protected]> * Fixed typos. * Added id to tracked reference content type response. * Updated OpenApi.json. * Added missing updates. * Renamed model and constants from code review feedback. * Fix typo * Fix multiple enumeration --------- Co-authored-by: Copilot <[email protected]> Co-authored-by: mole <[email protected]>
1 parent fd77074 commit 1f4c19d

File tree

21 files changed

+591
-8
lines changed

21 files changed

+591
-8
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.DataType.References;
11+
12+
[ApiVersion("1.0")]
13+
public class ReferencedByDataTypeController : DataTypeControllerBase
14+
{
15+
private readonly IDataTypeService _dataTypeService;
16+
private readonly IRelationTypePresentationFactory _relationTypePresentationFactory;
17+
18+
public ReferencedByDataTypeController(IDataTypeService dataTypeService, IRelationTypePresentationFactory relationTypePresentationFactory)
19+
{
20+
_dataTypeService = dataTypeService;
21+
_relationTypePresentationFactory = relationTypePresentationFactory;
22+
}
23+
24+
/// <summary>
25+
/// Gets a paged list of references for the current data type, so you can see where it is being used.
26+
/// </summary>
27+
[HttpGet("{id:guid}/referenced-by")]
28+
[MapToApiVersion("1.0")]
29+
[ProducesResponseType(typeof(PagedViewModel<IReferenceResponseModel>), StatusCodes.Status200OK)]
30+
public async Task<ActionResult<PagedViewModel<IReferenceResponseModel>>> ReferencedBy(
31+
CancellationToken cancellationToken,
32+
Guid id,
33+
int skip = 0,
34+
int take = 20)
35+
{
36+
PagedModel<RelationItemModel> relationItems = await _dataTypeService.GetPagedRelationsAsync(id, skip, take);
37+
38+
var pagedViewModel = new PagedViewModel<IReferenceResponseModel>
39+
{
40+
Total = relationItems.Total,
41+
Items = await _relationTypePresentationFactory.CreateReferenceResponseModelsAsync(relationItems.Items),
42+
};
43+
44+
return pagedViewModel;
45+
}
46+
}

src/Umbraco.Cms.Api.Management/Controllers/DataType/ReferencesDataTypeController.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Asp.Versioning;
1+
using Asp.Versioning;
22
using Microsoft.AspNetCore.Http;
33
using Microsoft.AspNetCore.Mvc;
44
using Umbraco.Cms.Api.Management.Factories;
@@ -10,6 +10,7 @@
1010
namespace Umbraco.Cms.Api.Management.Controllers.DataType;
1111

1212
[ApiVersion("1.0")]
13+
[Obsolete("Please use ReferencedByDataTypeController and the referenced-by endpoint. Scheduled for removal in Umbraco 17.")]
1314
public class ReferencesDataTypeController : DataTypeControllerBase
1415
{
1516
private readonly IDataTypeService _dataTypeService;

src/Umbraco.Cms.Api.Management/Factories/RelationTypePresentationFactory.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,12 @@ public async Task<IEnumerable<IReferenceResponseModel>> CreateReferenceResponseM
5656
IReferenceResponseModel[] result = relationItemModelsCollection.Select(relationItemModel =>
5757
relationItemModel.NodeType switch
5858
{
59-
Constants.UdiEntityType.Document => MapDocumentReference(relationItemModel, slimEntities),
60-
Constants.UdiEntityType.Media => _umbracoMapper.Map<MediaReferenceResponseModel>(relationItemModel),
61-
Constants.UdiEntityType.Member => _umbracoMapper.Map<MemberReferenceResponseModel>(relationItemModel),
59+
Constants.ReferenceType.Document => MapDocumentReference(relationItemModel, slimEntities),
60+
Constants.ReferenceType.Media => _umbracoMapper.Map<MediaReferenceResponseModel>(relationItemModel),
61+
Constants.ReferenceType.Member => _umbracoMapper.Map<MemberReferenceResponseModel>(relationItemModel),
62+
Constants.ReferenceType.DocumentTypePropertyType => _umbracoMapper.Map<DocumentTypePropertyTypeReferenceResponseModel>(relationItemModel),
63+
Constants.ReferenceType.MediaTypePropertyType => _umbracoMapper.Map<MediaTypePropertyTypeReferenceResponseModel>(relationItemModel),
64+
Constants.ReferenceType.MemberTypePropertyType => _umbracoMapper.Map<MemberTypePropertyTypeReferenceResponseModel>(relationItemModel),
6265
_ => _umbracoMapper.Map<DefaultReferenceResponseModel>(relationItemModel),
6366
}).WhereNotNull().ToArray();
6467

src/Umbraco.Cms.Api.Management/Mapping/TrackedReferences/TrackedReferenceViewModelsMapDefinition.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ public void DefineMaps(IUmbracoMapper mapper)
1212
mapper.Define<RelationItemModel, DocumentReferenceResponseModel>((source, context) => new DocumentReferenceResponseModel(), Map);
1313
mapper.Define<RelationItemModel, MediaReferenceResponseModel>((source, context) => new MediaReferenceResponseModel(), Map);
1414
mapper.Define<RelationItemModel, MemberReferenceResponseModel>((source, context) => new MemberReferenceResponseModel(), Map);
15+
mapper.Define<RelationItemModel, DocumentTypePropertyTypeReferenceResponseModel>((source, context) => new DocumentTypePropertyTypeReferenceResponseModel(), Map);
16+
mapper.Define<RelationItemModel, MediaTypePropertyTypeReferenceResponseModel>((source, context) => new MediaTypePropertyTypeReferenceResponseModel(), Map);
17+
mapper.Define<RelationItemModel, MemberTypePropertyTypeReferenceResponseModel>((source, context) => new MemberTypePropertyTypeReferenceResponseModel(), Map);
1518
mapper.Define<RelationItemModel, DefaultReferenceResponseModel>((source, context) => new DefaultReferenceResponseModel(), Map);
1619
mapper.Define<RelationItemModel, ReferenceByIdModel>((source, context) => new ReferenceByIdModel(), Map);
1720
mapper.Define<Guid, ReferenceByIdModel>((source, context) => new ReferenceByIdModel(), Map);
@@ -25,6 +28,7 @@ private void Map(RelationItemModel source, DocumentReferenceResponseModel target
2528
target.Published = source.NodePublished;
2629
target.DocumentType = new TrackedReferenceDocumentType
2730
{
31+
Id = source.ContentTypeKey,
2832
Alias = source.ContentTypeAlias,
2933
Icon = source.ContentTypeIcon,
3034
Name = source.ContentTypeName,
@@ -38,6 +42,7 @@ private void Map(RelationItemModel source, MediaReferenceResponseModel target, M
3842
target.Name = source.NodeName;
3943
target.MediaType = new TrackedReferenceMediaType
4044
{
45+
Id = source.ContentTypeKey,
4146
Alias = source.ContentTypeAlias,
4247
Icon = source.ContentTypeIcon,
4348
Name = source.ContentTypeName,
@@ -51,6 +56,52 @@ private void Map(RelationItemModel source, MemberReferenceResponseModel target,
5156
target.Name = source.NodeName;
5257
target.MemberType = new TrackedReferenceMemberType
5358
{
59+
Id = source.ContentTypeKey,
60+
Alias = source.ContentTypeAlias,
61+
Icon = source.ContentTypeIcon,
62+
Name = source.ContentTypeName,
63+
};
64+
}
65+
66+
// Umbraco.Code.MapAll
67+
private void Map(RelationItemModel source, DocumentTypePropertyTypeReferenceResponseModel target, MapperContext context)
68+
{
69+
target.Id = source.NodeKey;
70+
target.Name = source.NodeName;
71+
target.Alias = source.NodeAlias;
72+
target.DocumentType = new TrackedReferenceDocumentType
73+
{
74+
Id = source.ContentTypeKey,
75+
Alias = source.ContentTypeAlias,
76+
Icon = source.ContentTypeIcon,
77+
Name = source.ContentTypeName,
78+
};
79+
}
80+
81+
// Umbraco.Code.MapAll
82+
private void Map(RelationItemModel source, MediaTypePropertyTypeReferenceResponseModel target, MapperContext context)
83+
{
84+
target.Id = source.NodeKey;
85+
target.Name = source.NodeName;
86+
target.Alias = source.NodeAlias;
87+
target.MediaType = new TrackedReferenceMediaType
88+
{
89+
Id = source.ContentTypeKey,
90+
Alias = source.ContentTypeAlias,
91+
Icon = source.ContentTypeIcon,
92+
Name = source.ContentTypeName,
93+
};
94+
}
95+
96+
// Umbraco.Code.MapAll
97+
private void Map(RelationItemModel source, MemberTypePropertyTypeReferenceResponseModel target, MapperContext context)
98+
{
99+
target.Id = source.NodeKey;
100+
target.Name = source.NodeName;
101+
target.Alias = source.NodeAlias;
102+
target.MemberType = new TrackedReferenceMemberType
103+
{
104+
Id = source.ContentTypeKey,
54105
Alias = source.ContentTypeAlias,
55106
Icon = source.ContentTypeIcon,
56107
Name = source.ContentTypeName,

0 commit comments

Comments
 (0)