Skip to content

Instantly share code, notes, and snippets.

@torleifhalseth
Last active January 2, 2018 11:48
Show Gist options
  • Save torleifhalseth/569372362348b9bfe96d to your computer and use it in GitHub Desktop.
Save torleifhalseth/569372362348b9bfe96d to your computer and use it in GitHub Desktop.
Umbraco Relations
namespace MyProject.Models
{
/// <summary>
/// Enum RelationLookupType
/// </summary>
public enum RelationLookupType
{
Child,
Parent
}
}
using System.Collections.Generic;
using System.Linq;
using MyProject.Models;
using Our.Umbraco.Ditto;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using Umbraco.Web;
namespace MyProject.Repositories
{
/// <summary>
/// Class RelationRepository.
/// </summary>
public class RelationRepository
{
private readonly UmbracoHelper _umbracoHelper;
private readonly ServiceContext _serviceContext;
/// <summary>
/// Initializes a new instance of the <see cref="RelationRepository" /> class.
/// </summary>
/// <param name="umbracoContext">The umbraco context.</param>
/// <param name="serviceContext">The service context.</param>
public RelationRepository(UmbracoContext umbracoContext, ServiceContext serviceContext)
{
_umbracoHelper = new UmbracoHelper(umbracoContext);
_serviceContext = serviceContext;
}
/// <summary>
/// Gets the related models.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="content">The content.</param>
/// <param name="relationAlias">The relation alias.</param>
/// <param name="relationLookupType">Type of the relation lookup.</param>
/// <returns>IList&lt;T&gt;.</returns>
public IList<T> GetRelatedModels<T>(IPublishedContent content, string relationAlias, RelationLookupType relationLookupType = RelationLookupType.Child) where T : class
{
IEnumerable<IRelation> relations = null;
switch (relationLookupType)
{
case RelationLookupType.Parent:
relations = _serviceContext.RelationService.GetByChildId(content.Id).Where(x => x.RelationType.Alias == relationAlias);
break;
case RelationLookupType.Child:
relations = _serviceContext.RelationService.GetByParentId(content.Id).Where(x => x.RelationType.Alias == relationAlias);
break;
}
if (relations == null || !relations.Any()) return null;
var relatedModels = MapRelatedModels<T>(relationLookupType, relations);
return !relatedModels.Any() ? null : relatedModels;
}
/// <summary>
/// Maps the related models.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="relationLookupType">Type of the relation lookup.</param>
/// <param name="relations">The relations.</param>
/// <returns>List&lt;T&gt;.</returns>
private List<T> MapRelatedModels<T>(RelationLookupType relationLookupType, IEnumerable<IRelation> relations) where T : class
{
var relatedModels = new List<T>();
foreach (var relation in relations)
{
if (relation == null) continue;
IPublishedContent node = null;
switch (relationLookupType)
{
case RelationLookupType.Child:
node = _umbracoHelper.TypedContent(relation.ChildId);
break;
case RelationLookupType.Parent:
node = _umbracoHelper.TypedContent(relation.ParentId);
break;
}
if (node == null) continue;
relatedModels.Add(node.As<T>());
}
return relatedModels;
}
/// <summary>
/// Determines whether the specified node has relations.
/// </summary>
/// <param name="nodeId">The node identifier.</param>
/// <returns><c>true</c> if the specified node identifier has relations; otherwise, <c>false</c>.</returns>
public bool HasRelations(int nodeId)
{
return _serviceContext.RelationService.GetByParentId(nodeId).Any();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment