Skip to content

Commit 99a0b68

Browse files
authored
Merge pull request #6595 from erikjanwestendorp/update-relation-service
Porting old Umbraco API Controller [Relation Service]
2 parents c1769b5 + df68770 commit 99a0b68

File tree

2 files changed

+71
-123
lines changed

2 files changed

+71
-123
lines changed

14/umbraco-cms/reference/management/using-services/relationservice.md

Lines changed: 35 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,80 +4,58 @@ The `RelationService` allows you to create relations between objects that would
44

55
Below you will find examples using `RelationService`.
66

7-
## Automatically relate to root node
7+
## Automatically relate to the root node
88

9-
To perform the said task we need a component in which we can register to the `ContentService.Published` event:
9+
To perform the said task we need a Notification Handler:
1010

1111
[You can read more about composing Umbraco here](../../../implementation/composing.md)
1212

1313
```csharp
14-
using System.Linq;
15-
using Umbraco.Core.Composing;
16-
using Umbraco.Core.Events;
17-
using Umbraco.Core.Services;
18-
using Umbraco.Core.Services.Implement;
14+
using Umbraco.Cms.Core.Events;
15+
using Umbraco.Cms.Core.Notifications;
16+
using Umbraco.Cms.Core.Services;
1917

2018
namespace Doccers.Core.Components;
2119

22-
public class RelationComponent : IComponent
20+
public class ContentPublishedNotificationHandler(IContentService contentService, IRelationService relationService) : INotificationHandler<ContentPublishedNotification>
2321
{
24-
private readonly IRelationService _relationService;
25-
26-
public RelationComponent(IRelationService relationService)
27-
{
28-
_relationService = relationService;
29-
}
30-
31-
public void Initialize()
32-
{
33-
ContentService.Published += ContentService_Published;
34-
}
35-
36-
private void ContentService_Published(IContentService sender,
37-
ContentPublishedEventArgs e)
22+
public void Handle(ContentPublishedNotification notification)
3823
{
39-
// Should never be null, to be honest.
40-
var home = sender.GetRootContent()?.FirstOrDefault();
24+
var home = contentService.GetRootContent().FirstOrDefault();
4125
if (home == null) return;
4226

4327
// Get the relation type by alias
44-
var relationType = _relationService.GetRelationTypeByAlias("homesick");
28+
var relationType = relationService.GetRelationTypeByAlias("homesick");
29+
4530
if (relationType == null) return;
4631

47-
foreach (var entity in e.PublishedEntities
48-
.Where(x => x.Id != home.Id))
32+
foreach (var entity in notification.PublishedEntities
33+
.Where(x => x.Id != home.Id))
4934
{
5035
// Check if they are already related
51-
if (!_relationService.AreRelated(home.Id, entity.Id))
36+
if (!relationService.AreRelated(home.Id, entity.Id))
5237
{
5338
// If not then let us relate the currenty entity to home
54-
_relationService.Relate(home.Id, entity.Id, relationType);
39+
relationService.Relate(home.Id, entity.Id, relationType);
5540
}
5641
}
5742
}
58-
59-
public void Terminate() {
60-
//unsubscribe during shutdown
61-
ContentService.Published -= ContentService_Published;
62-
}
6343
}
6444
```
6545

66-
To have Umbraco recognize our component we need to register it in a composer:
46+
To have Umbraco recognize our Notification Handler we need to register it in a composer:
6747

6848
```csharp
69-
using Doccers.Core.Components;
70-
using Umbraco.Core;
71-
using Umbraco.Core.Composing;
49+
using Umbraco.Cms.Core.Composing;
50+
using Umbraco.Cms.Core.Notifications;
7251

7352
namespace Doccers.Core.Composers;
7453

75-
[RuntimeLevel(MinLevel = RuntimeLevel.Run)]
76-
public class RelationComposer : IUserComposer
54+
public class RelationComposer : IComposer
7755
{
78-
public void Compose(Composition composition)
56+
public void Compose(IUmbracoBuilder builder)
7957
{
80-
composition.Components().Append<RelationComponent>();
58+
builder.AddNotificationHandler<ContentPublishedNotification, ContentPublishedNotificationHandler>();
8159
}
8260
}
8361
```
@@ -88,50 +66,46 @@ If I know `Save and Publish` my `Products` node I get the following result:
8866

8967
Now let us try and fetch the data from an API.
9068

91-
{% hint style="warning" %}
92-
The example below uses UmbracoApiController which is obsolete in Umbraco 14 and will be removed in Umbraco 15.
93-
{% endhint %}
94-
9569
```csharp
96-
using System;
97-
using System.Linq;
98-
using System.Net;
99-
using System.Net.Http;
100-
using System.Web.Http;
101-
using Umbraco.Core.Services;
102-
using Umbraco.Web.WebApi;
70+
using Microsoft.AspNetCore.Mvc;
71+
using System.Runtime.Serialization;
72+
using Umbraco.Cms.Core.Services;
73+
using Umbraco.Cms.Web.Common;
10374

10475
namespace Doccers.Core.Controllers.Http;
10576

106-
public class RelationsController : UmbracoApiController
77+
[ApiController]
78+
[Route("/umbraco/api/relations")]
79+
public class RelationsController : Controller
10780
{
10881
private readonly IRelationService _relationService;
82+
private readonly UmbracoHelper _umbracoHelper;
10983

110-
public RelationsController(IRelationService relationService)
84+
public RelationsController(IRelationService relationService, UmbracoHelper umbracoHelper)
11185
{
11286
// Alternatively you could also access
11387
// the service via the service context:
11488
// _relationService = Services.RelationService;
11589
_relationService = relationService;
90+
_umbracoHelper = umbracoHelper;
11691
}
11792

118-
[HttpGet]
119-
public HttpResponseMessage GetByRelationTypeAlias(string alias)
93+
[HttpGet("getbyrelationtypealias")]
94+
public IActionResult GetByRelationTypeAlias(string alias)
12095
{
12196
var relationType = _relationService.GetRelationTypeByAlias(alias);
12297
if (relationType == null)
123-
return Request.CreateResponse(HttpStatusCode.BadRequest,
124-
"Invalid relation type alias");
98+
return BadRequest("Invalid relation type alias");
12599

126100
var relations = _relationService.GetAllRelationsByRelationType(relationType.Id);
127-
var content = relations.Select(x => Umbraco.Content(x.ChildId))
101+
var content = relations.Select(x => _umbracoHelper.Content(x.ChildId))
128102
.Select(x => new Relation()
129103
{
130104
Name = x.Name,
131105
UpdateDate = x.UpdateDate
132106
});
133107

134-
return Request.CreateResponse(HttpStatusCode.OK, content);
108+
return Ok(content);
135109
}
136110
}
137111
```

15/umbraco-cms/reference/management/using-services/relationservice.md

Lines changed: 36 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,134 +4,108 @@ The `RelationService` allows you to create relations between objects that would
44

55
Below you will find examples using `RelationService`.
66

7-
## Automatically relate to root node
7+
## Automatically relate to the root node
88

9-
To perform the said task we need a component in which we can register to the `ContentService.Published` event:
9+
To perform the said task we need a Notification Handler:
1010

1111
[You can read more about composing Umbraco here](../../../implementation/composing.md)
1212

1313
```csharp
14-
using System.Linq;
15-
using Umbraco.Core.Composing;
16-
using Umbraco.Core.Events;
17-
using Umbraco.Core.Services;
18-
using Umbraco.Core.Services.Implement;
14+
using Umbraco.Cms.Core.Events;
15+
using Umbraco.Cms.Core.Notifications;
16+
using Umbraco.Cms.Core.Services;
1917

2018
namespace Doccers.Core.Components;
2119

22-
public class RelationComponent : IComponent
20+
public class ContentPublishedNotificationHandler(IContentService contentService, IRelationService relationService) : INotificationHandler<ContentPublishedNotification>
2321
{
24-
private readonly IRelationService _relationService;
25-
26-
public RelationComponent(IRelationService relationService)
27-
{
28-
_relationService = relationService;
29-
}
30-
31-
public void Initialize()
32-
{
33-
ContentService.Published += ContentService_Published;
34-
}
35-
36-
private void ContentService_Published(IContentService sender,
37-
ContentPublishedEventArgs e)
22+
public void Handle(ContentPublishedNotification notification)
3823
{
39-
// Should never be null, to be honest.
40-
var home = sender.GetRootContent()?.FirstOrDefault();
24+
var home = contentService.GetRootContent().FirstOrDefault();
4125
if (home == null) return;
4226

4327
// Get the relation type by alias
44-
var relationType = _relationService.GetRelationTypeByAlias("homesick");
28+
var relationType = relationService.GetRelationTypeByAlias("homesick");
29+
4530
if (relationType == null) return;
4631

47-
foreach (var entity in e.PublishedEntities
48-
.Where(x => x.Id != home.Id))
32+
foreach (var entity in notification.PublishedEntities
33+
.Where(x => x.Id != home.Id))
4934
{
5035
// Check if they are already related
51-
if (!_relationService.AreRelated(home.Id, entity.Id))
36+
if (!relationService.AreRelated(home.Id, entity.Id))
5237
{
5338
// If not then let us relate the currenty entity to home
54-
_relationService.Relate(home.Id, entity.Id, relationType);
39+
relationService.Relate(home.Id, entity.Id, relationType);
5540
}
5641
}
5742
}
58-
59-
public void Terminate() {
60-
//unsubscribe during shutdown
61-
ContentService.Published -= ContentService_Published;
62-
}
6343
}
6444
```
6545

66-
To have Umbraco recognize our component we need to register it in a composer:
46+
To have Umbraco recognize our Notification Handler we need to register it in a composer:
6747

6848
```csharp
69-
using Doccers.Core.Components;
70-
using Umbraco.Core;
71-
using Umbraco.Core.Composing;
49+
using Umbraco.Cms.Core.Composing;
50+
using Umbraco.Cms.Core.Notifications;
7251

7352
namespace Doccers.Core.Composers;
7453

75-
[RuntimeLevel(MinLevel = RuntimeLevel.Run)]
76-
public class RelationComposer : IUserComposer
54+
public class RelationComposer : IComposer
7755
{
78-
public void Compose(Composition composition)
56+
public void Compose(IUmbracoBuilder builder)
7957
{
80-
composition.Components().Append<RelationComponent>();
58+
builder.AddNotificationHandler<ContentPublishedNotification, ContentPublishedNotificationHandler>();
8159
}
8260
}
8361
```
8462

85-
If I know `Save and Publish` my `Products` node I get the following result:
63+
If I now `Save and Publish` my `Products` node I get the following result:
8664

8765
![Relations](../../../../../10/umbraco-cms/reference/management/services/images/relations.PNG)
8866

8967
Now let us try and fetch the data from an API.
9068

91-
{% hint style="warning" %}
92-
The example below uses UmbracoApiController which is obsolete in Umbraco 14 and will be removed in Umbraco 15.
93-
{% endhint %}
94-
9569
```csharp
96-
using System;
97-
using System.Linq;
98-
using System.Net;
99-
using System.Net.Http;
100-
using System.Web.Http;
101-
using Umbraco.Core.Services;
102-
using Umbraco.Web.WebApi;
70+
using Microsoft.AspNetCore.Mvc;
71+
using System.Runtime.Serialization;
72+
using Umbraco.Cms.Core.Services;
73+
using Umbraco.Cms.Web.Common;
10374

10475
namespace Doccers.Core.Controllers.Http;
10576

106-
public class RelationsController : UmbracoApiController
77+
[ApiController]
78+
[Route("/umbraco/api/relations")]
79+
public class RelationsController : Controller
10780
{
10881
private readonly IRelationService _relationService;
82+
private readonly UmbracoHelper _umbracoHelper;
10983

110-
public RelationsController(IRelationService relationService)
84+
public RelationsController(IRelationService relationService, UmbracoHelper umbracoHelper)
11185
{
11286
// Alternatively you could also access
11387
// the service via the service context:
11488
// _relationService = Services.RelationService;
11589
_relationService = relationService;
90+
_umbracoHelper = umbracoHelper;
11691
}
11792

118-
[HttpGet]
119-
public HttpResponseMessage GetByRelationTypeAlias(string alias)
93+
[HttpGet("getbyrelationtypealias")]
94+
public IActionResult GetByRelationTypeAlias(string alias)
12095
{
12196
var relationType = _relationService.GetRelationTypeByAlias(alias);
12297
if (relationType == null)
123-
return Request.CreateResponse(HttpStatusCode.BadRequest,
124-
"Invalid relation type alias");
98+
return BadRequest("Invalid relation type alias");
12599

126100
var relations = _relationService.GetAllRelationsByRelationType(relationType.Id);
127-
var content = relations.Select(x => Umbraco.Content(x.ChildId))
101+
var content = relations.Select(x => _umbracoHelper.Content(x.ChildId))
128102
.Select(x => new Relation()
129103
{
130104
Name = x.Name,
131105
UpdateDate = x.UpdateDate
132106
});
133107

134-
return Request.CreateResponse(HttpStatusCode.OK, content);
108+
return Ok(content);
135109
}
136110
}
137111
```

0 commit comments

Comments
 (0)