diff --git a/source/nuPickers/Shared/RelationDataSource/RelationDataSourceApiController.cs b/source/nuPickers/Shared/RelationDataSource/RelationDataSourceApiController.cs index 60f96ce..a837342 100644 --- a/source/nuPickers/Shared/RelationDataSource/RelationDataSourceApiController.cs +++ b/source/nuPickers/Shared/RelationDataSource/RelationDataSourceApiController.cs @@ -1,14 +1,12 @@ - -namespace nuPickers.Shared.RelationDataSource +namespace nuPickers.Shared.RelationDataSource { using System.Collections.Generic; using System.Linq; using System.Web.Http; - using umbraco.cms.businesslogic.relation; + using Umbraco.Core.Models; using Umbraco.Web.Editors; using Umbraco.Web.Mvc; using nuPickers.Shared.Editor; - using umbraco; using CustomLabel; [PluginController("nuPickers")] @@ -16,14 +14,15 @@ public class RelationDataSourceApiController : UmbracoAuthorizedJsonController { public IEnumerable GetRelationTypes() { - return RelationType.GetAll() - .OrderBy(x => x.Name) - .Select(x => new - { - key = x.Alias, - label = x.Name, - biDirectional = x.Dual - }); + var relationService = this.ApplicationContext.Services.RelationService; + + return relationService.GetAllRelationTypes() + .OrderBy(x => x.Name) + .Select(x => new { + key = x.Alias, + label = x.Name, + biDirectional = x.IsBidirectional + }); } [HttpPost] @@ -33,19 +32,39 @@ public IEnumerable GetEditorDataItems([FromUri] int currentId, [ var relationService = this.ApplicationContext.Services.RelationService; - IEnumerable editorDataItems = relationService.GetEntitiesFromRelations( - relationService.GetByRelationTypeAlias((string)data.config.dataSource.relationType) - .Where(r => r.ParentId == contextId)) - .Select(x => new EditorDataItem() - { - Key = x.Item2.Id.ToString(), - Label = x.Item2.Name.ToString() - }) - .ToList(); + var relation = relationService.GetByRelationTypeAlias((string) data.config.dataSource.relationType).FirstOrDefault(); + if (relation != null) + { + var realtionType = relation.RelationType; + + IEnumerable editorUmbracoEntities; + + if (realtionType.IsBidirectional) + { + editorUmbracoEntities = relationService.GetByRelationTypeAlias((string) data.config.dataSource.relationType) + .Where(r => r.ParentId == contextId || r.ChildId == contextId); + } + else + { + editorUmbracoEntities = relationService.GetByRelationTypeAlias((string)data.config.dataSource.relationType) + .Where(r => r.ParentId == contextId); + } + + IEnumerable editorDataItems = relationService.GetEntitiesFromRelations(editorUmbracoEntities) + .Select(x => new EditorDataItem() + { + Key = x.Item2.Id.ToString(), + Label = x.Item2.Name.ToString() + }) + .ToList(); + + CustomLabel customLabel = new CustomLabel((string)data.config.customLabel, contextId, propertyAlias); + + return customLabel.ProcessEditorDataItems(editorDataItems); + } - CustomLabel customLabel = new CustomLabel((string)data.config.customLabel, contextId, propertyAlias); + return null; - return customLabel.ProcessEditorDataItems(editorDataItems); } } }