@@ -6,78 +6,56 @@ Below you will find examples using `RelationService`.
6
6
7
7
## Automatically relate to root node
8
8
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 :
10
10
11
11
[ You can read more about composing Umbraco here] ( ../../../implementation/composing.md )
12
12
13
13
``` 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 ;
19
17
20
18
namespace Doccers .Core .Components ;
21
19
22
- public class RelationComponent : IComponent
20
+ public class ContentPublishedNotificationHandler ( IContentService contentService , IRelationService relationService ) : INotificationHandler < ContentPublishedNotification >
23
21
{
24
- private readonly IRelationService _relationService ;
25
-
26
- public RelationComponent (IRelationService relationService )
27
- {
28
- _relationService = relationService ;
29
- }
30
-
31
- public void Initialize ()
22
+ public void Handle (ContentPublishedNotification notification )
32
23
{
33
- ContentService .Published += ContentService_Published ;
34
- }
35
-
36
- private void ContentService_Published (IContentService sender ,
37
- ContentPublishedEventArgs e )
38
- {
39
- // Should never be null, to be honest.
40
- var home = sender .GetRootContent ()? .FirstOrDefault ();
24
+ var home = contentService .GetRootContent ().FirstOrDefault ();
41
25
if (home == null ) return ;
42
26
43
27
// Get the relation type by alias
44
- var relationType = _relationService .GetRelationTypeByAlias (" homesick" );
28
+ var relationType = relationService .GetRelationTypeByAlias (" homesick" );
29
+
45
30
if (relationType == null ) return ;
46
31
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 ))
49
34
{
50
35
// Check if they are already related
51
- if (! _relationService .AreRelated (home .Id , entity .Id ))
36
+ if (! relationService .AreRelated (home .Id , entity .Id ))
52
37
{
53
38
// 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 );
55
40
}
56
41
}
57
42
}
58
-
59
- public void Terminate () {
60
- // unsubscribe during shutdown
61
- ContentService .Published -= ContentService_Published ;
62
- }
63
43
}
64
44
```
65
45
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:
67
47
68
48
``` 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 ;
72
51
73
52
namespace Doccers .Core .Composers ;
74
53
75
- [RuntimeLevel (MinLevel = RuntimeLevel .Run )]
76
- public class RelationComposer : IUserComposer
54
+ public class RelationComposer : IComposer
77
55
{
78
- public void Compose (Composition composition )
56
+ public void Compose (IUmbracoBuilder builder )
79
57
{
80
- composition . Components (). Append < RelationComponent >();
58
+ builder . AddNotificationHandler < ContentPublishedNotification , ContentPublishedNotificationHandler >();
81
59
}
82
60
}
83
61
```
@@ -93,45 +71,45 @@ The example below uses UmbracoApiController which is obsolete in Umbraco 14 and
93
71
{% endhint %}
94
72
95
73
``` 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 ;
74
+ using Microsoft .AspNetCore .Mvc ;
75
+ using System .Runtime .Serialization ;
76
+ using Umbraco .Cms .Core .Services ;
77
+ using Umbraco .Cms .Web .Common ;
103
78
104
79
namespace Doccers .Core .Controllers .Http ;
105
80
106
- public class RelationsController : UmbracoApiController
81
+ [ApiController ]
82
+ [Route (" /umbraco/api/relations" )]
83
+ public class RelationsController : Controller
107
84
{
108
85
private readonly IRelationService _relationService ;
86
+ private readonly UmbracoHelper _umbracoHelper ;
109
87
110
- public RelationsController (IRelationService relationService )
88
+ public RelationsController (IRelationService relationService , UmbracoHelper umbracoHelper )
111
89
{
112
90
// Alternatively you could also access
113
91
// the service via the service context:
114
92
// _relationService = Services.RelationService;
115
93
_relationService = relationService ;
94
+ _umbracoHelper = umbracoHelper ;
116
95
}
117
96
118
- [HttpGet ]
119
- public HttpResponseMessage GetByRelationTypeAlias (string alias )
97
+ [HttpGet ( " getbyrelationtypealias " ) ]
98
+ public IActionResult GetByRelationTypeAlias (string alias )
120
99
{
121
100
var relationType = _relationService .GetRelationTypeByAlias (alias );
122
101
if (relationType == null )
123
- return Request .CreateResponse (HttpStatusCode .BadRequest ,
124
- " Invalid relation type alias" );
102
+ return BadRequest (" Invalid relation type alias" );
125
103
126
104
var relations = _relationService .GetAllRelationsByRelationType (relationType .Id );
127
- var content = relations .Select (x => Umbraco .Content (x .ChildId ))
105
+ var content = relations .Select (x => _umbracoHelper .Content (x .ChildId ))
128
106
.Select (x => new Relation ()
129
107
{
130
108
Name = x .Name ,
131
109
UpdateDate = x .UpdateDate
132
110
});
133
111
134
- return Request . CreateResponse ( HttpStatusCode . OK , content );
112
+ return Ok ( content );
135
113
}
136
114
}
137
115
```
0 commit comments