From f2886f40a1dfda2bda32ef064d9021b14daddc5f Mon Sep 17 00:00:00 2001 From: Erik-Jan Westendorp Date: Thu, 24 Oct 2024 13:35:34 +0200 Subject: [PATCH 1/7] Change Component to ContentPublishedNotificationHandler --- .../using-services/relationservice.md | 60 ++++++------------- 1 file changed, 19 insertions(+), 41 deletions(-) diff --git a/14/umbraco-cms/reference/management/using-services/relationservice.md b/14/umbraco-cms/reference/management/using-services/relationservice.md index 749ac5124cb..4a58f4de19a 100644 --- a/14/umbraco-cms/reference/management/using-services/relationservice.md +++ b/14/umbraco-cms/reference/management/using-services/relationservice.md @@ -6,78 +6,56 @@ Below you will find examples using `RelationService`. ## Automatically relate to root node -To perform the said task we need a component in which we can register to the `ContentService.Published` event: +To perform the said task we need a Notification Handler: [You can read more about composing Umbraco here](../../../implementation/composing.md) ```csharp -using System.Linq; -using Umbraco.Core.Composing; -using Umbraco.Core.Events; -using Umbraco.Core.Services; -using Umbraco.Core.Services.Implement; +using Umbraco.Cms.Core.Events; +using Umbraco.Cms.Core.Notifications; +using Umbraco.Cms.Core.Services; namespace Doccers.Core.Components; -public class RelationComponent : IComponent +public class ContentPublishedNotificationHandler(IContentService contentService, IRelationService relationService) : INotificationHandler { - private readonly IRelationService _relationService; - - public RelationComponent(IRelationService relationService) - { - _relationService = relationService; - } - - public void Initialize() - { - ContentService.Published += ContentService_Published; - } - - private void ContentService_Published(IContentService sender, - ContentPublishedEventArgs e) + public void Handle(ContentPublishedNotification notification) { - // Should never be null, to be honest. - var home = sender.GetRootContent()?.FirstOrDefault(); + var home = contentService.GetRootContent().FirstOrDefault(); if (home == null) return; // Get the relation type by alias - var relationType = _relationService.GetRelationTypeByAlias("homesick"); + var relationType = relationService.GetRelationTypeByAlias("homesick"); + if (relationType == null) return; - foreach (var entity in e.PublishedEntities - .Where(x => x.Id != home.Id)) + foreach (var entity in notification.PublishedEntities + .Where(x => x.Id != home.Id)) { // Check if they are already related - if (!_relationService.AreRelated(home.Id, entity.Id)) + if (!relationService.AreRelated(home.Id, entity.Id)) { // If not then let us relate the currenty entity to home - _relationService.Relate(home.Id, entity.Id, relationType); + relationService.Relate(home.Id, entity.Id, relationType); } } } - - public void Terminate() { - //unsubscribe during shutdown - ContentService.Published -= ContentService_Published; - } } ``` -To have Umbraco recognize our component we need to register it in a composer: +To have Umbraco recognize our Notification Handler we need to register it in a composer: ```csharp -using Doccers.Core.Components; -using Umbraco.Core; -using Umbraco.Core.Composing; +using Umbraco.Cms.Core.Composing; +using Umbraco.Cms.Core.Notifications; namespace Doccers.Core.Composers; -[RuntimeLevel(MinLevel = RuntimeLevel.Run)] -public class RelationComposer : IUserComposer +public class RelationComposer : IComposer { - public void Compose(Composition composition) + public void Compose(IUmbracoBuilder builder) { - composition.Components().Append(); + builder.AddNotificationHandler(); } } ``` From 4c009be6b9912ef301429ee986c28cb526d390fa Mon Sep 17 00:00:00 2001 From: Erik-Jan Westendorp Date: Thu, 24 Oct 2024 14:08:48 +0200 Subject: [PATCH 2/7] Update controller example --- .../using-services/relationservice.md | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/14/umbraco-cms/reference/management/using-services/relationservice.md b/14/umbraco-cms/reference/management/using-services/relationservice.md index 4a58f4de19a..6ba1a41b5ee 100644 --- a/14/umbraco-cms/reference/management/using-services/relationservice.md +++ b/14/umbraco-cms/reference/management/using-services/relationservice.md @@ -71,45 +71,45 @@ The example below uses UmbracoApiController which is obsolete in Umbraco 14 and {% endhint %} ```csharp -using System; -using System.Linq; -using System.Net; -using System.Net.Http; -using System.Web.Http; -using Umbraco.Core.Services; -using Umbraco.Web.WebApi; +using Microsoft.AspNetCore.Mvc; +using System.Runtime.Serialization; +using Umbraco.Cms.Core.Services; +using Umbraco.Cms.Web.Common; namespace Doccers.Core.Controllers.Http; -public class RelationsController : UmbracoApiController +[ApiController] +[Route("/umbraco/api/relations")] +public class RelationsController : Controller { private readonly IRelationService _relationService; + private readonly UmbracoHelper _umbracoHelper; - public RelationsController(IRelationService relationService) + public RelationsController(IRelationService relationService, UmbracoHelper umbracoHelper) { // Alternatively you could also access // the service via the service context: // _relationService = Services.RelationService; _relationService = relationService; + _umbracoHelper = umbracoHelper; } - [HttpGet] - public HttpResponseMessage GetByRelationTypeAlias(string alias) + [HttpGet("getbyrelationtypealias")] + public IActionResult GetByRelationTypeAlias(string alias) { var relationType = _relationService.GetRelationTypeByAlias(alias); if (relationType == null) - return Request.CreateResponse(HttpStatusCode.BadRequest, - "Invalid relation type alias"); + return BadRequest("Invalid relation type alias"); var relations = _relationService.GetAllRelationsByRelationType(relationType.Id); - var content = relations.Select(x => Umbraco.Content(x.ChildId)) + var content = relations.Select(x => _umbracoHelper.Content(x.ChildId)) .Select(x => new Relation() { Name = x.Name, UpdateDate = x.UpdateDate }); - return Request.CreateResponse(HttpStatusCode.OK, content); + return Ok(content); } } ``` From 636af752decb4b41dd172e8002c3a10518aaa48a Mon Sep 17 00:00:00 2001 From: Erik-Jan Westendorp Date: Thu, 24 Oct 2024 14:13:57 +0200 Subject: [PATCH 3/7] Update v15 --- .../using-services/relationservice.md | 90 +++++++------------ 1 file changed, 34 insertions(+), 56 deletions(-) diff --git a/15/umbraco-cms/reference/management/using-services/relationservice.md b/15/umbraco-cms/reference/management/using-services/relationservice.md index 749ac5124cb..6ba1a41b5ee 100644 --- a/15/umbraco-cms/reference/management/using-services/relationservice.md +++ b/15/umbraco-cms/reference/management/using-services/relationservice.md @@ -6,78 +6,56 @@ Below you will find examples using `RelationService`. ## Automatically relate to root node -To perform the said task we need a component in which we can register to the `ContentService.Published` event: +To perform the said task we need a Notification Handler: [You can read more about composing Umbraco here](../../../implementation/composing.md) ```csharp -using System.Linq; -using Umbraco.Core.Composing; -using Umbraco.Core.Events; -using Umbraco.Core.Services; -using Umbraco.Core.Services.Implement; +using Umbraco.Cms.Core.Events; +using Umbraco.Cms.Core.Notifications; +using Umbraco.Cms.Core.Services; namespace Doccers.Core.Components; -public class RelationComponent : IComponent +public class ContentPublishedNotificationHandler(IContentService contentService, IRelationService relationService) : INotificationHandler { - private readonly IRelationService _relationService; - - public RelationComponent(IRelationService relationService) - { - _relationService = relationService; - } - - public void Initialize() + public void Handle(ContentPublishedNotification notification) { - ContentService.Published += ContentService_Published; - } - - private void ContentService_Published(IContentService sender, - ContentPublishedEventArgs e) - { - // Should never be null, to be honest. - var home = sender.GetRootContent()?.FirstOrDefault(); + var home = contentService.GetRootContent().FirstOrDefault(); if (home == null) return; // Get the relation type by alias - var relationType = _relationService.GetRelationTypeByAlias("homesick"); + var relationType = relationService.GetRelationTypeByAlias("homesick"); + if (relationType == null) return; - foreach (var entity in e.PublishedEntities - .Where(x => x.Id != home.Id)) + foreach (var entity in notification.PublishedEntities + .Where(x => x.Id != home.Id)) { // Check if they are already related - if (!_relationService.AreRelated(home.Id, entity.Id)) + if (!relationService.AreRelated(home.Id, entity.Id)) { // If not then let us relate the currenty entity to home - _relationService.Relate(home.Id, entity.Id, relationType); + relationService.Relate(home.Id, entity.Id, relationType); } } } - - public void Terminate() { - //unsubscribe during shutdown - ContentService.Published -= ContentService_Published; - } } ``` -To have Umbraco recognize our component we need to register it in a composer: +To have Umbraco recognize our Notification Handler we need to register it in a composer: ```csharp -using Doccers.Core.Components; -using Umbraco.Core; -using Umbraco.Core.Composing; +using Umbraco.Cms.Core.Composing; +using Umbraco.Cms.Core.Notifications; namespace Doccers.Core.Composers; -[RuntimeLevel(MinLevel = RuntimeLevel.Run)] -public class RelationComposer : IUserComposer +public class RelationComposer : IComposer { - public void Compose(Composition composition) + public void Compose(IUmbracoBuilder builder) { - composition.Components().Append(); + builder.AddNotificationHandler(); } } ``` @@ -93,45 +71,45 @@ The example below uses UmbracoApiController which is obsolete in Umbraco 14 and {% endhint %} ```csharp -using System; -using System.Linq; -using System.Net; -using System.Net.Http; -using System.Web.Http; -using Umbraco.Core.Services; -using Umbraco.Web.WebApi; +using Microsoft.AspNetCore.Mvc; +using System.Runtime.Serialization; +using Umbraco.Cms.Core.Services; +using Umbraco.Cms.Web.Common; namespace Doccers.Core.Controllers.Http; -public class RelationsController : UmbracoApiController +[ApiController] +[Route("/umbraco/api/relations")] +public class RelationsController : Controller { private readonly IRelationService _relationService; + private readonly UmbracoHelper _umbracoHelper; - public RelationsController(IRelationService relationService) + public RelationsController(IRelationService relationService, UmbracoHelper umbracoHelper) { // Alternatively you could also access // the service via the service context: // _relationService = Services.RelationService; _relationService = relationService; + _umbracoHelper = umbracoHelper; } - [HttpGet] - public HttpResponseMessage GetByRelationTypeAlias(string alias) + [HttpGet("getbyrelationtypealias")] + public IActionResult GetByRelationTypeAlias(string alias) { var relationType = _relationService.GetRelationTypeByAlias(alias); if (relationType == null) - return Request.CreateResponse(HttpStatusCode.BadRequest, - "Invalid relation type alias"); + return BadRequest("Invalid relation type alias"); var relations = _relationService.GetAllRelationsByRelationType(relationType.Id); - var content = relations.Select(x => Umbraco.Content(x.ChildId)) + var content = relations.Select(x => _umbracoHelper.Content(x.ChildId)) .Select(x => new Relation() { Name = x.Name, UpdateDate = x.UpdateDate }); - return Request.CreateResponse(HttpStatusCode.OK, content); + return Ok(content); } } ``` From a1961c5472a49a85d496ec00bb08e3b82e4e206c Mon Sep 17 00:00:00 2001 From: Erik-Jan Westendorp Date: Wed, 30 Oct 2024 13:25:45 +0100 Subject: [PATCH 4/7] Delete obsolete message --- .../reference/management/using-services/relationservice.md | 4 ---- .../reference/management/using-services/relationservice.md | 4 ---- 2 files changed, 8 deletions(-) diff --git a/14/umbraco-cms/reference/management/using-services/relationservice.md b/14/umbraco-cms/reference/management/using-services/relationservice.md index 6ba1a41b5ee..f51f53203f4 100644 --- a/14/umbraco-cms/reference/management/using-services/relationservice.md +++ b/14/umbraco-cms/reference/management/using-services/relationservice.md @@ -66,10 +66,6 @@ If I know `Save and Publish` my `Products` node I get the following result: Now let us try and fetch the data from an API. -{% hint style="warning" %} -The example below uses UmbracoApiController which is obsolete in Umbraco 14 and will be removed in Umbraco 15. -{% endhint %} - ```csharp using Microsoft.AspNetCore.Mvc; using System.Runtime.Serialization; diff --git a/15/umbraco-cms/reference/management/using-services/relationservice.md b/15/umbraco-cms/reference/management/using-services/relationservice.md index 6ba1a41b5ee..f51f53203f4 100644 --- a/15/umbraco-cms/reference/management/using-services/relationservice.md +++ b/15/umbraco-cms/reference/management/using-services/relationservice.md @@ -66,10 +66,6 @@ If I know `Save and Publish` my `Products` node I get the following result: Now let us try and fetch the data from an API. -{% hint style="warning" %} -The example below uses UmbracoApiController which is obsolete in Umbraco 14 and will be removed in Umbraco 15. -{% endhint %} - ```csharp using Microsoft.AspNetCore.Mvc; using System.Runtime.Serialization; From fa793525eef3d4a3d8f4908eda0dc34e01bfc06e Mon Sep 17 00:00:00 2001 From: sofietoft Date: Thu, 31 Oct 2024 09:07:28 +0100 Subject: [PATCH 5/7] Trigger GitBook checks --- .../reference/management/using-services/relationservice.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/14/umbraco-cms/reference/management/using-services/relationservice.md b/14/umbraco-cms/reference/management/using-services/relationservice.md index f51f53203f4..02ec6266931 100644 --- a/14/umbraco-cms/reference/management/using-services/relationservice.md +++ b/14/umbraco-cms/reference/management/using-services/relationservice.md @@ -4,7 +4,7 @@ The `RelationService` allows you to create relations between objects that would Below you will find examples using `RelationService`. -## Automatically relate to root node +## Automatically relate to the root node To perform the said task we need a Notification Handler: From 0d41f3f6f8835d8f8b739af66804f23342374eed Mon Sep 17 00:00:00 2001 From: sofietoft Date: Thu, 31 Oct 2024 09:11:37 +0100 Subject: [PATCH 6/7] Trigger GitBook checks again --- .../reference/management/using-services/relationservice.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/15/umbraco-cms/reference/management/using-services/relationservice.md b/15/umbraco-cms/reference/management/using-services/relationservice.md index f51f53203f4..02ec6266931 100644 --- a/15/umbraco-cms/reference/management/using-services/relationservice.md +++ b/15/umbraco-cms/reference/management/using-services/relationservice.md @@ -4,7 +4,7 @@ The `RelationService` allows you to create relations between objects that would Below you will find examples using `RelationService`. -## Automatically relate to root node +## Automatically relate to the root node To perform the said task we need a Notification Handler: From df68770132fd183d5e419de28b910c1b0e6ab09f Mon Sep 17 00:00:00 2001 From: sofietoft Date: Thu, 31 Oct 2024 09:14:21 +0100 Subject: [PATCH 7/7] GitBook trigger - again --- .../reference/management/using-services/relationservice.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/15/umbraco-cms/reference/management/using-services/relationservice.md b/15/umbraco-cms/reference/management/using-services/relationservice.md index 02ec6266931..bed26552c64 100644 --- a/15/umbraco-cms/reference/management/using-services/relationservice.md +++ b/15/umbraco-cms/reference/management/using-services/relationservice.md @@ -60,7 +60,7 @@ public class RelationComposer : IComposer } ``` -If I know `Save and Publish` my `Products` node I get the following result: +If I now `Save and Publish` my `Products` node I get the following result: ![Relations](../../../../../10/umbraco-cms/reference/management/services/images/relations.PNG)