Add variancy information to reference response model (#18645)
* Made variant info available on DocumentReferenceResponseModel * Fix scope issue * PR Feedback + correct scoping
This commit is contained in:
@@ -1,7 +1,12 @@
|
|||||||
using Umbraco.Cms.Api.Management.ViewModels.TrackedReferences;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Umbraco.Cms.Api.Management.ViewModels.TrackedReferences;
|
||||||
using Umbraco.Cms.Core;
|
using Umbraco.Cms.Core;
|
||||||
|
using Umbraco.Cms.Core.DependencyInjection;
|
||||||
using Umbraco.Cms.Core.Mapping;
|
using Umbraco.Cms.Core.Mapping;
|
||||||
using Umbraco.Cms.Core.Models;
|
using Umbraco.Cms.Core.Models;
|
||||||
|
using Umbraco.Cms.Core.Models.Entities;
|
||||||
|
using Umbraco.Cms.Core.Persistence.Repositories;
|
||||||
|
using Umbraco.Cms.Infrastructure.Scoping;
|
||||||
using Umbraco.Extensions;
|
using Umbraco.Extensions;
|
||||||
|
|
||||||
namespace Umbraco.Cms.Api.Management.Factories;
|
namespace Umbraco.Cms.Api.Management.Factories;
|
||||||
@@ -9,21 +14,69 @@ namespace Umbraco.Cms.Api.Management.Factories;
|
|||||||
public class RelationTypePresentationFactory : IRelationTypePresentationFactory
|
public class RelationTypePresentationFactory : IRelationTypePresentationFactory
|
||||||
{
|
{
|
||||||
private readonly IUmbracoMapper _umbracoMapper;
|
private readonly IUmbracoMapper _umbracoMapper;
|
||||||
|
private readonly IEntityRepository _entityRepository;
|
||||||
|
private readonly IDocumentPresentationFactory _documentPresentationFactory;
|
||||||
|
private readonly IScopeProvider _scopeProvider;
|
||||||
|
|
||||||
|
[Obsolete("Please use the non obsoleted constructor. Scheduled for removal in v17")]
|
||||||
public RelationTypePresentationFactory(IUmbracoMapper umbracoMapper)
|
public RelationTypePresentationFactory(IUmbracoMapper umbracoMapper)
|
||||||
|
: this(
|
||||||
|
umbracoMapper,
|
||||||
|
StaticServiceProvider.Instance.GetRequiredService<IEntityRepository>(),
|
||||||
|
StaticServiceProvider.Instance.GetRequiredService<IDocumentPresentationFactory>(),
|
||||||
|
StaticServiceProvider.Instance.GetRequiredService<IScopeProvider>())
|
||||||
{
|
{
|
||||||
_umbracoMapper = umbracoMapper;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<IReferenceResponseModel>> CreateReferenceResponseModelsAsync(IEnumerable<RelationItemModel> relationItemModels)
|
public RelationTypePresentationFactory(
|
||||||
|
IUmbracoMapper umbracoMapper,
|
||||||
|
IEntityRepository entityRepository,
|
||||||
|
IDocumentPresentationFactory documentPresentationFactory,
|
||||||
|
IScopeProvider scopeProvider)
|
||||||
{
|
{
|
||||||
IReferenceResponseModel[] result = relationItemModels.Select(relationItemModel => relationItemModel.NodeType switch
|
_umbracoMapper = umbracoMapper;
|
||||||
|
_entityRepository = entityRepository;
|
||||||
|
_documentPresentationFactory = documentPresentationFactory;
|
||||||
|
_scopeProvider = scopeProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IEnumerable<IReferenceResponseModel>> CreateReferenceResponseModelsAsync(
|
||||||
|
IEnumerable<RelationItemModel> relationItemModels)
|
||||||
{
|
{
|
||||||
Constants.UdiEntityType.Document => _umbracoMapper.Map<DocumentReferenceResponseModel>(relationItemModel),
|
IReadOnlyCollection<RelationItemModel> relationItemModelsCollection = relationItemModels.ToArray();
|
||||||
|
|
||||||
|
Guid[] documentKeys = relationItemModelsCollection
|
||||||
|
.Where(item => item.NodeType is Constants.UdiEntityType.Document)
|
||||||
|
.Select(item => item.NodeKey).Distinct().ToArray();
|
||||||
|
|
||||||
|
using IScope scope = _scopeProvider.CreateScope(autoComplete: true);
|
||||||
|
|
||||||
|
var slimEntities = _entityRepository.GetAll(Constants.ObjectTypes.Document, documentKeys).ToList();
|
||||||
|
|
||||||
|
IReferenceResponseModel[] result = relationItemModelsCollection.Select(relationItemModel =>
|
||||||
|
relationItemModel.NodeType switch
|
||||||
|
{
|
||||||
|
Constants.UdiEntityType.Document => MapDocumentReference(relationItemModel, slimEntities),
|
||||||
Constants.UdiEntityType.Media => _umbracoMapper.Map<MediaReferenceResponseModel>(relationItemModel),
|
Constants.UdiEntityType.Media => _umbracoMapper.Map<MediaReferenceResponseModel>(relationItemModel),
|
||||||
_ => _umbracoMapper.Map<DefaultReferenceResponseModel>(relationItemModel) as IReferenceResponseModel,
|
_ => _umbracoMapper.Map<DefaultReferenceResponseModel>(relationItemModel),
|
||||||
}).WhereNotNull().ToArray();
|
}).WhereNotNull().ToArray();
|
||||||
|
|
||||||
return await Task.FromResult(result);
|
return await Task.FromResult(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private IReferenceResponseModel? MapDocumentReference(RelationItemModel relationItemModel,
|
||||||
|
List<IEntitySlim> slimEntities)
|
||||||
|
{
|
||||||
|
DocumentReferenceResponseModel? documentReferenceResponseModel =
|
||||||
|
_umbracoMapper.Map<DocumentReferenceResponseModel>(relationItemModel);
|
||||||
|
if (documentReferenceResponseModel is not null
|
||||||
|
&& slimEntities.FirstOrDefault(e => e.Key == relationItemModel.NodeKey) is DocumentEntitySlim
|
||||||
|
matchingSlimDocument)
|
||||||
|
{
|
||||||
|
documentReferenceResponseModel.Variants =
|
||||||
|
_documentPresentationFactory.CreateVariantsItemResponseModels(matchingSlimDocument);
|
||||||
|
}
|
||||||
|
|
||||||
|
return documentReferenceResponseModel;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ public class TrackedReferenceViewModelsMapDefinition : IMapDefinition
|
|||||||
mapper.Define<Guid, ReferenceByIdModel>((source, context) => new ReferenceByIdModel(), Map);
|
mapper.Define<Guid, ReferenceByIdModel>((source, context) => new ReferenceByIdModel(), Map);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Umbraco.Code.MapAll
|
// Umbraco.Code.MapAll -Variants
|
||||||
private void Map(RelationItemModel source, DocumentReferenceResponseModel target, MapperContext context)
|
private void Map(RelationItemModel source, DocumentReferenceResponseModel target, MapperContext context)
|
||||||
{
|
{
|
||||||
target.Id = source.NodeKey;
|
target.Id = source.NodeKey;
|
||||||
|
|||||||
@@ -37126,7 +37126,8 @@
|
|||||||
"required": [
|
"required": [
|
||||||
"$type",
|
"$type",
|
||||||
"documentType",
|
"documentType",
|
||||||
"id"
|
"id",
|
||||||
|
"variants"
|
||||||
],
|
],
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
@@ -37151,6 +37152,16 @@
|
|||||||
"$ref": "#/components/schemas/TrackedReferenceDocumentTypeModel"
|
"$ref": "#/components/schemas/TrackedReferenceDocumentTypeModel"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
"variants": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"oneOf": [
|
||||||
|
{
|
||||||
|
"$ref": "#/components/schemas/DocumentVariantItemResponseModel"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"additionalProperties": false,
|
"additionalProperties": false,
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
namespace Umbraco.Cms.Api.Management.ViewModels.TrackedReferences;
|
using Umbraco.Cms.Api.Management.ViewModels.Document;
|
||||||
|
|
||||||
|
namespace Umbraco.Cms.Api.Management.ViewModels.TrackedReferences;
|
||||||
|
|
||||||
public class DocumentReferenceResponseModel : IReferenceResponseModel
|
public class DocumentReferenceResponseModel : IReferenceResponseModel
|
||||||
{
|
{
|
||||||
@@ -9,4 +11,6 @@ public class DocumentReferenceResponseModel : IReferenceResponseModel
|
|||||||
public bool? Published { get; set; }
|
public bool? Published { get; set; }
|
||||||
|
|
||||||
public TrackedReferenceDocumentType DocumentType { get; set; } = new();
|
public TrackedReferenceDocumentType DocumentType { get; set; } = new();
|
||||||
|
|
||||||
|
public IEnumerable<DocumentVariantItemResponseModel> Variants { get; set; } = Enumerable.Empty<DocumentVariantItemResponseModel>();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user