From d4ba1298d59673f4c93c1f615db47262c541a829 Mon Sep 17 00:00:00 2001 From: Stephan Date: Fri, 12 Apr 2019 16:05:43 +0200 Subject: [PATCH] Add GetById(Udi) to published caches --- .../PublishedContentCache.cs | 3 +++ .../PublishedMediaCache.cs | 3 +++ .../PublishedContent/SolidPublishedSnapshot.cs | 3 +++ .../PublishedCache/IPublishedCache.cs | 18 ++++++++++++++++++ .../PublishedCache/NuCache/ContentCache.cs | 12 ++++++++++++ .../PublishedCache/NuCache/MediaCache.cs | 15 +++++++++++++++ .../PublishedCache/PublishedCacheBase.cs | 18 +++++++++--------- 7 files changed, 63 insertions(+), 9 deletions(-) diff --git a/src/Umbraco.Tests/LegacyXmlPublishedCache/PublishedContentCache.cs b/src/Umbraco.Tests/LegacyXmlPublishedCache/PublishedContentCache.cs index 5fc0d628c9..2a144f3aaa 100644 --- a/src/Umbraco.Tests/LegacyXmlPublishedCache/PublishedContentCache.cs +++ b/src/Umbraco.Tests/LegacyXmlPublishedCache/PublishedContentCache.cs @@ -381,6 +381,9 @@ namespace Umbraco.Tests.LegacyXmlPublishedCache } } + public override IPublishedContent GetById(bool preview, Udi nodeId) + => throw new NotSupportedException(); + public override bool HasById(bool preview, int contentId) { return GetXml(preview).CreateNavigator().MoveToId(contentId.ToString(CultureInfo.InvariantCulture)); diff --git a/src/Umbraco.Tests/LegacyXmlPublishedCache/PublishedMediaCache.cs b/src/Umbraco.Tests/LegacyXmlPublishedCache/PublishedMediaCache.cs index 71490465d0..0c7ee98c6d 100644 --- a/src/Umbraco.Tests/LegacyXmlPublishedCache/PublishedMediaCache.cs +++ b/src/Umbraco.Tests/LegacyXmlPublishedCache/PublishedMediaCache.cs @@ -97,6 +97,9 @@ namespace Umbraco.Tests.LegacyXmlPublishedCache throw new NotImplementedException(); } + public override IPublishedContent GetById(bool preview, Udi nodeId) + => throw new NotSupportedException(); + public override bool HasById(bool preview, int contentId) { return GetUmbracoMedia(contentId) != null; diff --git a/src/Umbraco.Tests/PublishedContent/SolidPublishedSnapshot.cs b/src/Umbraco.Tests/PublishedContent/SolidPublishedSnapshot.cs index 86017be820..9828a14597 100644 --- a/src/Umbraco.Tests/PublishedContent/SolidPublishedSnapshot.cs +++ b/src/Umbraco.Tests/PublishedContent/SolidPublishedSnapshot.cs @@ -92,6 +92,9 @@ namespace Umbraco.Tests.PublishedContent throw new NotImplementedException(); } + public override IPublishedContent GetById(bool preview, Udi nodeId) + => throw new NotSupportedException(); + public override bool HasById(bool preview, int contentId) { return _content.ContainsKey(contentId); diff --git a/src/Umbraco.Web/PublishedCache/IPublishedCache.cs b/src/Umbraco.Web/PublishedCache/IPublishedCache.cs index ff459a2d9b..3cd7b924fb 100644 --- a/src/Umbraco.Web/PublishedCache/IPublishedCache.cs +++ b/src/Umbraco.Web/PublishedCache/IPublishedCache.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Xml.XPath; +using Umbraco.Core; using Umbraco.Core.Models.PublishedContent; using Umbraco.Core.Xml; @@ -29,6 +30,15 @@ namespace Umbraco.Web.PublishedCache /// The value of overrides defaults. IPublishedContent GetById(bool preview, Guid contentId); + /// + /// Gets a content identified by its Udi identifier. + /// + /// A value indicating whether to consider unpublished content. + /// The content Udi identifier. + /// The content, or null. + /// The value of overrides defaults. + IPublishedContent GetById(bool preview, Udi contentId); + /// /// Gets a content identified by its unique identifier. /// @@ -45,6 +55,14 @@ namespace Umbraco.Web.PublishedCache /// Considers published or unpublished content depending on defaults. IPublishedContent GetById(Guid contentId); + /// + /// Gets a content identified by its unique identifier. + /// + /// The content unique identifier. + /// The content, or null. + /// Considers published or unpublished content depending on defaults. + IPublishedContent GetById(Udi contentId); + /// /// Gets a value indicating whether the cache contains a specified content. /// diff --git a/src/Umbraco.Web/PublishedCache/NuCache/ContentCache.cs b/src/Umbraco.Web/PublishedCache/NuCache/ContentCache.cs index 0e74ea919f..4bd3fcf247 100644 --- a/src/Umbraco.Web/PublishedCache/NuCache/ContentCache.cs +++ b/src/Umbraco.Web/PublishedCache/NuCache/ContentCache.cs @@ -238,6 +238,18 @@ namespace Umbraco.Web.PublishedCache.NuCache return GetNodePublishedContent(node, preview); } + public override IPublishedContent GetById(bool preview, Udi contentId) + { + var guidUdi = contentId as GuidUdi; + if (guidUdi == null) + throw new ArgumentException($"Udi must be of type {typeof(GuidUdi).Name}.", nameof(contentId)); + + if (guidUdi.EntityType != Constants.UdiEntityType.Document) + throw new ArgumentException($"Udi entity type must be \"{Constants.UdiEntityType.Document}\".", nameof(contentId)); + + return GetById(preview, guidUdi.Guid); + } + public override bool HasById(bool preview, int contentId) { var n = _snapshot.Get(contentId); diff --git a/src/Umbraco.Web/PublishedCache/NuCache/MediaCache.cs b/src/Umbraco.Web/PublishedCache/NuCache/MediaCache.cs index f7bdb4400f..112ccd9931 100644 --- a/src/Umbraco.Web/PublishedCache/NuCache/MediaCache.cs +++ b/src/Umbraco.Web/PublishedCache/NuCache/MediaCache.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Xml.XPath; +using Umbraco.Core; using Umbraco.Core.Cache; using Umbraco.Core.Models.PublishedContent; using Umbraco.Core.Xml; @@ -44,6 +45,20 @@ namespace Umbraco.Web.PublishedCache.NuCache return n?.PublishedModel; } + public override IPublishedContent GetById(bool preview, Udi contentId) + { + var guidUdi = contentId as GuidUdi; + if (guidUdi == null) + throw new ArgumentException($"Udi must be of type {typeof(GuidUdi).Name}.", nameof(contentId)); + + if (guidUdi.EntityType != Constants.UdiEntityType.Media) + throw new ArgumentException($"Udi entity type must be \"{Constants.UdiEntityType.Media}\".", nameof(contentId)); + + // ignore preview, there's only draft for media + var n = _snapshot.Get(guidUdi.Guid); + return n?.PublishedModel; + } + public override bool HasById(bool preview, int contentId) { var n = _snapshot.Get(contentId); diff --git a/src/Umbraco.Web/PublishedCache/PublishedCacheBase.cs b/src/Umbraco.Web/PublishedCache/PublishedCacheBase.cs index b0fe1a4240..b88ae26704 100644 --- a/src/Umbraco.Web/PublishedCache/PublishedCacheBase.cs +++ b/src/Umbraco.Web/PublishedCache/PublishedCacheBase.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Xml.XPath; +using Umbraco.Core; using Umbraco.Core.Models.PublishedContent; using Umbraco.Core.Xml; @@ -19,23 +20,22 @@ namespace Umbraco.Web.PublishedCache public abstract IPublishedContent GetById(bool preview, int contentId); public IPublishedContent GetById(int contentId) - { - return GetById(PreviewDefault, contentId); - } + => GetById(PreviewDefault, contentId); public abstract IPublishedContent GetById(bool preview, Guid contentId); public IPublishedContent GetById(Guid contentId) - { - return GetById(PreviewDefault, contentId); - } + => GetById(PreviewDefault, contentId); + + public abstract IPublishedContent GetById(bool preview, Udi contentId); + + public IPublishedContent GetById(Udi contentId) + => GetById(PreviewDefault, contentId); public abstract bool HasById(bool preview, int contentId); public bool HasById(int contentId) - { - return HasById(PreviewDefault, contentId); - } + => HasById(PreviewDefault, contentId); public abstract IEnumerable GetAtRoot(bool preview);