66using System . Transactions ;
77using System . Web . Http ;
88using System . Web . Http . Controllers ;
9+ using umbraco ;
910using Umbraco . Core . Models ;
1011using Umbraco . Core . Services ;
1112using Umbraco . Web ;
@@ -20,11 +21,39 @@ public class RelationsController : UmbracoApiController
2021 {
2122 private readonly IRelationService relationService ;
2223 private readonly IContentService contentService ;
24+ private readonly IMediaService mediaService ;
25+ private readonly IContentTypeService contentTypeService ;
26+
27+ private readonly Dictionary < UmbracoObjectTypes , UmbracoObjectTypes [ ] > allowedRelations = new Dictionary < UmbracoObjectTypes , UmbracoObjectTypes [ ] >
28+ {
29+ { UmbracoObjectTypes . DocumentType , new [ ] { UmbracoObjectTypes . DocumentType , UmbracoObjectTypes . MediaType } } ,
30+ { UmbracoObjectTypes . MediaType , new [ ] { UmbracoObjectTypes . DocumentType , UmbracoObjectTypes . MediaType } } ,
31+ { UmbracoObjectTypes . Document , new [ ] { UmbracoObjectTypes . Document , UmbracoObjectTypes . Media } } ,
32+ { UmbracoObjectTypes . Media , new [ ] { UmbracoObjectTypes . Document , UmbracoObjectTypes . Media } } ,
33+ } ;
34+
35+ private readonly Dictionary < TreeNodeType , UmbracoObjectTypes > treeNodeObjectTypes = new Dictionary < TreeNodeType , UmbracoObjectTypes >
36+ {
37+ { new TreeNodeType ( "content" , null ) , UmbracoObjectTypes . Document } ,
38+ { new TreeNodeType ( "media" , null ) , UmbracoObjectTypes . Media } ,
39+ { new TreeNodeType ( "settings" , "nodeTypes" ) , UmbracoObjectTypes . DocumentType } ,
40+ { new TreeNodeType ( "settings" , "mediaTypes" ) , UmbracoObjectTypes . MediaType }
41+ } ;
42+
43+ private readonly Dictionary < Guid , TreeNodeType > objectTypeTreeTypes = new Dictionary < Guid , TreeNodeType >
44+ {
45+ { UmbracoObjectTypes . Document . GetGuid ( ) , new TreeNodeType ( "content" , null ) } ,
46+ { UmbracoObjectTypes . Media . GetGuid ( ) , new TreeNodeType ( "media" , null ) } ,
47+ { UmbracoObjectTypes . DocumentType . GetGuid ( ) , new TreeNodeType ( "settings" , "nodeTypes" ) } ,
48+ { UmbracoObjectTypes . MediaType . GetGuid ( ) , new TreeNodeType ( "settings" , "mediaTypes" ) }
49+ } ;
2350
2451 public RelationsController ( )
2552 {
2653 relationService = ApplicationContext . Services . RelationService ;
2754 contentService = ApplicationContext . Services . ContentService ;
55+ mediaService = ApplicationContext . Services . MediaService ;
56+ contentTypeService = ApplicationContext . Services . ContentTypeService ;
2857 }
2958
3059 public string [ ] GetObjectTypes ( )
@@ -33,39 +62,35 @@ public string[] GetObjectTypes()
3362 }
3463
3564 public ContentRelationsDto GetRelations (
36- string from ,
37- string to ,
65+ string section ,
66+ string treeType ,
3867 int parentId
3968 )
4069 {
41- UmbracoObjectTypes fromType ;
42- UmbracoObjectTypes toType ;
70+ var treeNodeType = new TreeNodeType ( section , treeType ) ;
71+ var fromType = UmbracoObjectTypes . Unknown ;
4372
4473 if (
45- ! Enum . TryParse ( from , out fromType ) ||
46- ! Enum . TryParse ( to , out toType ) ||
47- fromType == UmbracoObjectTypes . Unknown ||
48- toType == UmbracoObjectTypes . Unknown
74+ ! treeNodeObjectTypes . TryGetValue ( treeNodeType , out fromType )
75+ || fromType == UmbracoObjectTypes . Unknown
4976 )
5077 throw new Exception ( "Cannot get relation types for unknown object type" ) ;
5178
52- if ( fromType != UmbracoObjectTypes . Document || toType != UmbracoObjectTypes . Document )
53- throw new Exception ( "Haven't implemented anything but document relations yet" ) ;
54-
5579 var allRelations = relationService . GetByParentOrChildId ( parentId ) ;
5680 var relationSets = relationService . GetAllRelationTypes ( )
57- . Where ( rt => rt . ParentObjectType == fromType . GetGuid ( ) && rt . ChildObjectType == toType . GetGuid ( ) )
81+ . Where ( rt => rt . ParentObjectType == fromType . GetGuid ( ) && allowedRelations [ fromType ] . Any ( ar => ar . GetGuid ( ) == rt . ChildObjectType ) )
5882 . Select ( rt => new RelationSetDto
5983 {
6084 RelationTypeId = rt . Id ,
85+ ChildType = objectTypeTreeTypes [ rt . ChildObjectType ] ,
6186 Alias = rt . Alias ,
6287 Name = rt . Name ,
6388 Relations = allRelations
6489 . Where ( r => r . RelationTypeId == rt . Id )
6590 . Select ( r => new RelationDto
6691 {
6792 ChildId = r . ChildId ,
68- ChildName = contentService . GetById ( r . ChildId ) . Name ,
93+ ChildName = GetChildName ( rt . ChildObjectType , r . ChildId ) ,
6994 State = RelationStateEnum . Unmodified
7095 } ) . ToList ( )
7196 } ) . ToList ( ) ;
@@ -100,6 +125,22 @@ public void SaveRelations(ContentRelationsDto contentRelations)
100125 }
101126 }
102127 }
128+
129+ private string GetChildName ( Guid childObjectType , int childId )
130+ {
131+ switch ( UmbracoObjectTypesExtensions . GetUmbracoObjectType ( childObjectType ) )
132+ {
133+ case UmbracoObjectTypes . Document :
134+ return contentService . GetById ( childId ) . Name ;
135+ case UmbracoObjectTypes . Media :
136+ return mediaService . GetById ( childId ) . Name ;
137+ case UmbracoObjectTypes . DocumentType :
138+ return contentTypeService . GetContentType ( childId ) . Name ;
139+ case UmbracoObjectTypes . MediaType :
140+ return contentTypeService . GetMediaType ( childId ) . Name ;
141+ }
142+ throw new Exception ( "Unknown child type" ) ;
143+ }
103144 }
104145
105146 public class ContentRelationsDto
@@ -111,6 +152,7 @@ public class ContentRelationsDto
111152 public class RelationSetDto
112153 {
113154 public int RelationTypeId { get ; set ; }
155+ public TreeNodeType ChildType { get ; set ; }
114156 public string Alias { get ; set ; }
115157 public string Name { get ; set ; }
116158 public IList < RelationDto > Relations { get ; set ; }
0 commit comments