Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
@@ -1,10 +1,13 @@
using Asp.Versioning;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Cms.Api.Common.ViewModels.Pagination;
using Umbraco.Cms.Api.Management.Factories;
using Umbraco.Cms.Api.Management.ViewModels.TrackedReferences;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Entities;
using Umbraco.Cms.Core.Services;

namespace Umbraco.Cms.Api.Management.Controllers.Document.References;
Expand All @@ -14,11 +17,28 @@ public class ReferencedByDocumentController : DocumentControllerBase
{
private readonly ITrackedReferencesService _trackedReferencesService;
private readonly IRelationTypePresentationFactory _relationTypePresentationFactory;
private readonly IEntityService _entityService;

public ReferencedByDocumentController(ITrackedReferencesService trackedReferencesService, IRelationTypePresentationFactory relationTypePresentationFactory)
[Obsolete("Please use the constructor with all parameters. Scheduled for removal in Umbraco 19.")]
public ReferencedByDocumentController(
ITrackedReferencesService trackedReferencesService,
IRelationTypePresentationFactory relationTypePresentationFactory)
: this(
trackedReferencesService,
relationTypePresentationFactory,
StaticServiceProvider.Instance.GetRequiredService<IEntityService>())
{
}

[ActivatorUtilitiesConstructor]
public ReferencedByDocumentController(
ITrackedReferencesService trackedReferencesService,
IRelationTypePresentationFactory relationTypePresentationFactory,
IEntityService entityService)
{
_trackedReferencesService = trackedReferencesService;
_relationTypePresentationFactory = relationTypePresentationFactory;
_entityService = entityService;
}

/// <summary>
Expand All @@ -31,12 +51,19 @@ public ReferencedByDocumentController(ITrackedReferencesService trackedReference
[HttpGet("{id:guid}/referenced-by")]
[MapToApiVersion("1.0")]
[ProducesResponseType(typeof(PagedViewModel<IReferenceResponseModel>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<PagedViewModel<IReferenceResponseModel>>> ReferencedBy(
CancellationToken cancellationToken,
Guid id,
int skip = 0,
int take = 20)
{
IEntitySlim? entity = _entityService.Get(id, UmbracoObjectTypes.Document);
if (entity is null)
{
return NotFound();
}
Copy link
Member

@Zeegaan Zeegaan Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I disagree on this approach, sorry 😢
I think this logic doesn't belong in the controller itself, but in the service.

I think we should update the ITrackedReferencesService
to instead adopt the Attempt<T> pattern, which most of our services have adopted at this point, and then it will return a failed attempt, if the IEntitySlim? entity = _entityService.Get(id, UmbracoObjectTypes.Document); returns null. I guess you also need to pass the ObjectType, but I don't see the problem in that.
We actually already inject the IEntityService in the service, but it's not used for now 🙈

Image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to apologies, you are right in my view. I've refactored to implement this in the way you have suggested. Would you mind having another look please? Thanks.


PagedModel<RelationItemModel> relationItems = await _trackedReferencesService.GetPagedRelationsForItemAsync(id, skip, take, true);

var pagedViewModel = new PagedViewModel<IReferenceResponseModel>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using Asp.Versioning;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Cms.Api.Common.ViewModels.Pagination;
using Umbraco.Cms.Api.Management.ViewModels;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Mapping;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Entities;
using Umbraco.Cms.Core.Services;

namespace Umbraco.Cms.Api.Management.Controllers.Document.References;
Expand All @@ -14,11 +17,28 @@ public class ReferencedDescendantsDocumentController : DocumentControllerBase
{
private readonly ITrackedReferencesService _trackedReferencesSkipTakeService;
private readonly IUmbracoMapper _umbracoMapper;
private readonly IEntityService _entityService;

public ReferencedDescendantsDocumentController(ITrackedReferencesService trackedReferencesSkipTakeService, IUmbracoMapper umbracoMapper)
[Obsolete("Please use the constructor with all parameters. Scheduled for removal in Umbraco 19.")]
public ReferencedDescendantsDocumentController(
ITrackedReferencesService trackedReferencesSkipTakeService,
IUmbracoMapper umbracoMapper)
: this(
trackedReferencesSkipTakeService,
umbracoMapper,
StaticServiceProvider.Instance.GetRequiredService<IEntityService>())
{
}

[ActivatorUtilitiesConstructor]
public ReferencedDescendantsDocumentController(
ITrackedReferencesService trackedReferencesSkipTakeService,
IUmbracoMapper umbracoMapper,
IEntityService entityService)
{
_trackedReferencesSkipTakeService = trackedReferencesSkipTakeService;
_umbracoMapper = umbracoMapper;
_entityService = entityService;
}

/// <summary>
Expand All @@ -32,12 +52,19 @@ public ReferencedDescendantsDocumentController(ITrackedReferencesService tracked
[HttpGet("{id:guid}/referenced-descendants")]
[MapToApiVersion("1.0")]
[ProducesResponseType(typeof(PagedViewModel<ReferenceByIdModel>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<PagedViewModel<ReferenceByIdModel>>> ReferencedDescendants(
CancellationToken cancellationToken,
Guid id,
int skip = 0,
int take = 20)
{
IEntitySlim? entity = _entityService.Get(id, UmbracoObjectTypes.Document);
if (entity is null)
{
return NotFound();
}

PagedModel<RelationItemModel> relationItems = await _trackedReferencesSkipTakeService.GetPagedDescendantsInReferencesAsync(id, skip, take, true);
var pagedViewModel = new PagedViewModel<ReferenceByIdModel>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using Asp.Versioning;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Cms.Api.Common.ViewModels.Pagination;
using Umbraco.Cms.Api.Management.Factories;
using Umbraco.Cms.Api.Management.ViewModels.TrackedReferences;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Entities;
using Umbraco.Cms.Core.Services;

namespace Umbraco.Cms.Api.Management.Controllers.Media.References;
Expand All @@ -14,11 +17,28 @@ public class ReferencedByMediaController : MediaControllerBase
{
private readonly ITrackedReferencesService _trackedReferencesService;
private readonly IRelationTypePresentationFactory _relationTypePresentationFactory;
private readonly IEntityService _entityService;

public ReferencedByMediaController(ITrackedReferencesService trackedReferencesService, IRelationTypePresentationFactory relationTypePresentationFactory)
[Obsolete("Please use the constructor with all parameters. Scheduled for removal in Umbraco 19.")]
public ReferencedByMediaController(
ITrackedReferencesService trackedReferencesService,
IRelationTypePresentationFactory relationTypePresentationFactory)
: this(
trackedReferencesService,
relationTypePresentationFactory,
StaticServiceProvider.Instance.GetRequiredService<IEntityService>())
{
}

[ActivatorUtilitiesConstructor]
public ReferencedByMediaController(
ITrackedReferencesService trackedReferencesService,
IRelationTypePresentationFactory relationTypePresentationFactory,
IEntityService entityService)
{
_trackedReferencesService = trackedReferencesService;
_relationTypePresentationFactory = relationTypePresentationFactory;
_entityService = entityService;
}

/// <summary>
Expand All @@ -37,6 +57,12 @@ public async Task<ActionResult<PagedViewModel<IReferenceResponseModel>>> Referen
int skip = 0,
int take = 20)
{
IEntitySlim? entity = _entityService.Get(id, UmbracoObjectTypes.Media);
if (entity is null)
{
return NotFound();
}

PagedModel<RelationItemModel> relationItems = await _trackedReferencesService.GetPagedRelationsForItemAsync(id, skip, take, true);

var pagedViewModel = new PagedViewModel<IReferenceResponseModel>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using Asp.Versioning;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Cms.Api.Common.ViewModels.Pagination;
using Umbraco.Cms.Api.Management.Factories;
using Umbraco.Cms.Api.Management.ViewModels;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Mapping;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Entities;
using Umbraco.Cms.Core.Services;

namespace Umbraco.Cms.Api.Management.Controllers.Media.References;
Expand All @@ -14,11 +18,28 @@ public class ReferencedDescendantsMediaController : MediaControllerBase
{
private readonly ITrackedReferencesService _trackedReferencesSkipTakeService;
private readonly IUmbracoMapper _umbracoMapper;
private readonly IEntityService _entityService;

public ReferencedDescendantsMediaController(ITrackedReferencesService trackedReferencesSkipTakeService, IUmbracoMapper umbracoMapper)
[Obsolete("Please use the constructor with all parameters. Scheduled for removal in Umbraco 19.")]
public ReferencedDescendantsMediaController(
ITrackedReferencesService trackedReferencesSkipTakeService,
IUmbracoMapper umbracoMapper)
: this(
trackedReferencesSkipTakeService,
umbracoMapper,
StaticServiceProvider.Instance.GetRequiredService<IEntityService>())
{
}

[ActivatorUtilitiesConstructor]
public ReferencedDescendantsMediaController(
ITrackedReferencesService trackedReferencesSkipTakeService,
IUmbracoMapper umbracoMapper,
IEntityService entityService)
{
_trackedReferencesSkipTakeService = trackedReferencesSkipTakeService;
_umbracoMapper = umbracoMapper;
_entityService = entityService;
}

/// <summary>
Expand All @@ -32,12 +53,19 @@ public ReferencedDescendantsMediaController(ITrackedReferencesService trackedRef
[HttpGet("{id:guid}/referenced-descendants")]
[MapToApiVersion("1.0")]
[ProducesResponseType(typeof(PagedViewModel<ReferenceByIdModel>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<PagedViewModel<ReferenceByIdModel>>> ReferencedDescendants(
CancellationToken cancellationToken,
Guid id,
int skip = 0,
int take = 20)
{
IEntitySlim? entity = _entityService.Get(id, UmbracoObjectTypes.Media);
if (entity is null)
{
return NotFound();
}

PagedModel<RelationItemModel> relationItems = await _trackedReferencesSkipTakeService.GetPagedDescendantsInReferencesAsync(id, skip, take, true);
var pagedViewModel = new PagedViewModel<ReferenceByIdModel>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using Asp.Versioning;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Cms.Api.Common.ViewModels.Pagination;
using Umbraco.Cms.Api.Management.Factories;
using Umbraco.Cms.Api.Management.ViewModels.TrackedReferences;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Entities;
using Umbraco.Cms.Core.Services;

namespace Umbraco.Cms.Api.Management.Controllers.Member.References;
Expand All @@ -14,11 +17,28 @@ public class ReferencedByMemberController : MemberControllerBase
{
private readonly ITrackedReferencesService _trackedReferencesService;
private readonly IRelationTypePresentationFactory _relationTypePresentationFactory;
private readonly IEntityService _entityService;

public ReferencedByMemberController(ITrackedReferencesService trackedReferencesService, IRelationTypePresentationFactory relationTypePresentationFactory)
[Obsolete("Please use the constructor with all parameters. Scheduled for removal in Umbraco 19.")]
public ReferencedByMemberController(
ITrackedReferencesService trackedReferencesService,
IRelationTypePresentationFactory relationTypePresentationFactory)
: this(
trackedReferencesService,
relationTypePresentationFactory,
StaticServiceProvider.Instance.GetRequiredService<IEntityService>())
{
}

[ActivatorUtilitiesConstructor]
public ReferencedByMemberController(
ITrackedReferencesService trackedReferencesService,
IRelationTypePresentationFactory relationTypePresentationFactory,
IEntityService entityService)
{
_trackedReferencesService = trackedReferencesService;
_relationTypePresentationFactory = relationTypePresentationFactory;
_entityService = entityService;
}

/// <summary>
Expand All @@ -31,12 +51,19 @@ public ReferencedByMemberController(ITrackedReferencesService trackedReferencesS
[HttpGet("{id:guid}/referenced-by")]
[MapToApiVersion("1.0")]
[ProducesResponseType(typeof(PagedViewModel<IReferenceResponseModel>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<PagedViewModel<IReferenceResponseModel>>> ReferencedBy(
CancellationToken cancellationToken,
Guid id,
int skip = 0,
int take = 20)
{
IEntitySlim? entity = _entityService.Get(id, UmbracoObjectTypes.Member);
if (entity is null)
{
return NotFound();
}

PagedModel<RelationItemModel> relationItems = await _trackedReferencesService.GetPagedRelationsForItemAsync(id, skip, take, true);

var pagedViewModel = new PagedViewModel<IReferenceResponseModel>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using Asp.Versioning;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Umbraco.Cms.Api.Common.ViewModels.Pagination;
using Umbraco.Cms.Api.Management.ViewModels;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Mapping;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Entities;
using Umbraco.Cms.Core.Services;

namespace Umbraco.Cms.Api.Management.Controllers.Member.References;
Expand All @@ -14,11 +17,28 @@ public class ReferencedDescendantsMemberController : MemberControllerBase
{
private readonly ITrackedReferencesService _trackedReferencesSkipTakeService;
private readonly IUmbracoMapper _umbracoMapper;
private readonly IEntityService _entityService;

public ReferencedDescendantsMemberController(ITrackedReferencesService trackedReferencesSkipTakeService, IUmbracoMapper umbracoMapper)
[Obsolete("Please use the constructor with all parameters. Scheduled for removal in Umbraco 19.")]
public ReferencedDescendantsMemberController(
ITrackedReferencesService trackedReferencesSkipTakeService,
IUmbracoMapper umbracoMapper)
: this(
trackedReferencesSkipTakeService,
umbracoMapper,
StaticServiceProvider.Instance.GetRequiredService<IEntityService>())
{
}

[ActivatorUtilitiesConstructor]
public ReferencedDescendantsMemberController(
ITrackedReferencesService trackedReferencesSkipTakeService,
IUmbracoMapper umbracoMapper,
IEntityService entityService)
{
_trackedReferencesSkipTakeService = trackedReferencesSkipTakeService;
_umbracoMapper = umbracoMapper;
_entityService = entityService;
}

/// <summary>
Expand All @@ -32,12 +52,19 @@ public ReferencedDescendantsMemberController(ITrackedReferencesService trackedRe
[HttpGet("{id:guid}/referenced-descendants")]
[MapToApiVersion("1.0")]
[ProducesResponseType(typeof(PagedViewModel<ReferenceByIdModel>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<PagedViewModel<ReferenceByIdModel>>> ReferencedDescendants(
CancellationToken cancellationToken,
Guid id,
int skip = 0,
int take = 20)
{
IEntitySlim? entity = _entityService.Get(id, UmbracoObjectTypes.Member);
if (entity is null)
{
return NotFound();
}

PagedModel<RelationItemModel> relationItems = await _trackedReferencesSkipTakeService.GetPagedDescendantsInReferencesAsync(id, skip, take, true);
var pagedViewModel = new PagedViewModel<ReferenceByIdModel>
{
Expand Down
Loading