From 1c7bb623f19a7d093f46a7fdee6709c8a0a7ed8d Mon Sep 17 00:00:00 2001 From: Morten Christensen Date: Sat, 9 Feb 2013 11:12:51 -0100 Subject: [PATCH] Implements the retrieval of ancestors, children, descendats and parent IMedia in the MediaService and as extensions to IMedia. Corresponds to the IMedia implementation of U4-1638. --- src/Umbraco.Core/Models/ContentExtensions.cs | 44 ++++++++++++++++ src/Umbraco.Core/Services/ContentService.cs | 9 +++- src/Umbraco.Core/Services/IMediaService.cs | 35 +++++++++++++ src/Umbraco.Core/Services/MediaService.cs | 53 ++++++++++++++++++++ 4 files changed, 140 insertions(+), 1 deletion(-) diff --git a/src/Umbraco.Core/Models/ContentExtensions.cs b/src/Umbraco.Core/Models/ContentExtensions.cs index 2fd23dabe6..1cd26cfad6 100644 --- a/src/Umbraco.Core/Models/ContentExtensions.cs +++ b/src/Umbraco.Core/Models/ContentExtensions.cs @@ -19,6 +19,7 @@ namespace Umbraco.Core.Models { public static class ContentExtensions { + #region IContent /// /// Returns a list of the current contents ancestors, not including the content itself. /// @@ -58,6 +59,49 @@ namespace Umbraco.Core.Models { return ApplicationContext.Current.Services.ContentService.GetById(content.ParentId); } + #endregion + + #region IMedia + /// + /// Returns a list of the current medias ancestors, not including the media itself. + /// + /// Current media + /// An enumerable list of objects + public static IEnumerable Ancestors(this IMedia media) + { + return ApplicationContext.Current.Services.MediaService.GetAncestors(media); + } + + /// + /// Returns a list of the current medias children. + /// + /// Current media + /// An enumerable list of objects + public static IEnumerable Children(this IMedia media) + { + return ApplicationContext.Current.Services.MediaService.GetChildren(media.Id); + } + + /// + /// Returns a list of the current medias descendants, not including the media itself. + /// + /// Current media + /// An enumerable list of objects + public static IEnumerable Descendants(this IMedia media) + { + return ApplicationContext.Current.Services.MediaService.GetDescendants(media); + } + + /// + /// Returns the parent of the current media. + /// + /// Current media + /// An object + public static IMedia Parent(this IMedia media) + { + return ApplicationContext.Current.Services.MediaService.GetById(media.ParentId); + } + #endregion /// /// Set property values by alias with an annonymous object diff --git a/src/Umbraco.Core/Services/ContentService.cs b/src/Umbraco.Core/Services/ContentService.cs index 5b5845aa90..997f7e4c24 100644 --- a/src/Umbraco.Core/Services/ContentService.cs +++ b/src/Umbraco.Core/Services/ContentService.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Linq; using System.Web; using System.Xml.Linq; @@ -246,7 +247,10 @@ namespace Umbraco.Core.Services /// An Enumerable list of objects public IEnumerable GetAncestors(IContent content) { - var ids = content.Path.Split(',').Where(x => x != "-1" && x != content.Id.ToString()).Select(int.Parse).ToArray(); + var ids = content.Path.Split(',').Where(x => x != "-1" && x != content.Id.ToString(CultureInfo.InvariantCulture)).Select(int.Parse).ToArray(); + if (ids.Any() == false) + return new List(); + using (var repository = _repositoryFactory.CreateContentRepository(_uowProvider.GetUnitOfWork())) { return repository.GetAll(ids); @@ -331,6 +335,9 @@ namespace Umbraco.Core.Services /// Parent object public IContent GetParent(IContent content) { + if (content.ParentId == -1 || content.ParentId == -20) + return null; + return GetById(content.ParentId); } diff --git a/src/Umbraco.Core/Services/IMediaService.cs b/src/Umbraco.Core/Services/IMediaService.cs index 6f9d87d576..0698f08f49 100644 --- a/src/Umbraco.Core/Services/IMediaService.cs +++ b/src/Umbraco.Core/Services/IMediaService.cs @@ -177,5 +177,40 @@ namespace Umbraco.Core.Services /// Boolean indicating whether to delete versions prior to the versionId /// Optional Id of the User deleting versions of a Content object void DeleteVersion(int id, Guid versionId, bool deletePriorVersions, int userId = 0); + + /// + /// Gets a collection of objects, which are ancestors of the current media. + /// + /// Id of the to retrieve ancestors for + /// An Enumerable list of objects + IEnumerable GetAncestors(int id); + + /// + /// Gets a collection of objects, which are ancestors of the current media. + /// + /// to retrieve ancestors for + /// An Enumerable list of objects + IEnumerable GetAncestors(IMedia media); + + /// + /// Gets descendants of a object by its Id + /// + /// The Parent object to retrieve descendants from + /// An Enumerable flat list of objects + IEnumerable GetDescendants(IMedia media); + + /// + /// Gets the parent of the current media as an item. + /// + /// Id of the to retrieve the parent from + /// Parent object + IMedia GetParent(int id); + + /// + /// Gets the parent of the current media as an item. + /// + /// to retrieve the parent from + /// Parent object + IMedia GetParent(IMedia media); } } \ No newline at end of file diff --git a/src/Umbraco.Core/Services/MediaService.cs b/src/Umbraco.Core/Services/MediaService.cs index 844747bb8d..a32974102b 100644 --- a/src/Umbraco.Core/Services/MediaService.cs +++ b/src/Umbraco.Core/Services/MediaService.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Linq; using System.Xml.Linq; using Umbraco.Core.Auditing; @@ -189,6 +190,34 @@ namespace Umbraco.Core.Services } } + /// + /// Gets a collection of objects, which are ancestors of the current media. + /// + /// Id of the to retrieve ancestors for + /// An Enumerable list of objects + public IEnumerable GetAncestors(int id) + { + var media = GetById(id); + return GetAncestors(media); + } + + /// + /// Gets a collection of objects, which are ancestors of the current media. + /// + /// to retrieve ancestors for + /// An Enumerable list of objects + public IEnumerable GetAncestors(IMedia media) + { + var ids = media.Path.Split(',').Where(x => x != "-1" && x != media.Id.ToString(CultureInfo.InvariantCulture)).Select(int.Parse).ToArray(); + if(ids.Any() == false) + return new List(); + + using (var repository = _repositoryFactory.CreateMediaRepository(_uowProvider.GetUnitOfWork())) + { + return repository.GetAll(ids); + } + } + /// /// Gets a collection of objects by Parent Id /// @@ -234,6 +263,30 @@ namespace Umbraco.Core.Services } } + /// + /// Gets the parent of the current media as an item. + /// + /// Id of the to retrieve the parent from + /// Parent object + public IMedia GetParent(int id) + { + var media = GetById(id); + return GetParent(media); + } + + /// + /// Gets the parent of the current media as an item. + /// + /// to retrieve the parent from + /// Parent object + public IMedia GetParent(IMedia media) + { + if (media.ParentId == -1 || media.ParentId == -21) + return null; + + return GetById(media.ParentId); + } + /// /// Gets a collection of objects by the Id of the ///