Implemented an extension method to lessen duplication of code
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using Umbraco.Cms.Core.PublishedCache;
|
||||
|
||||
namespace Umbraco.Extensions
|
||||
{
|
||||
public static class PublishedSnapshotAccessorExtensions
|
||||
{
|
||||
public static IPublishedSnapshot GetRequiredPublishedSnapshot(this IPublishedSnapshotAccessor publishedSnapshotAccessor)
|
||||
{
|
||||
if (publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
|
||||
{
|
||||
return publishedSnapshot;
|
||||
}
|
||||
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,10 +17,7 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters
|
||||
Constants.Conventions.Content.Redirect.ToLower(CultureInfo.InvariantCulture)
|
||||
};
|
||||
|
||||
public ContentPickerValueConverter(IPublishedSnapshotAccessor publishedSnapshotAccessor)
|
||||
{
|
||||
_publishedSnapshotAccessor = publishedSnapshotAccessor;
|
||||
}
|
||||
public ContentPickerValueConverter(IPublishedSnapshotAccessor publishedSnapshotAccessor) => _publishedSnapshotAccessor = publishedSnapshotAccessor;
|
||||
|
||||
public override bool IsConverter(IPublishedPropertyType propertyType)
|
||||
=> propertyType.EditorAlias.Equals(Constants.PropertyEditors.Aliases.ContentPicker);
|
||||
@@ -57,10 +54,7 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters
|
||||
if ((propertyType.Alias != null && PropertiesToExclude.Contains(propertyType.Alias.ToLower(CultureInfo.InvariantCulture))) == false)
|
||||
{
|
||||
IPublishedContent content;
|
||||
if (!_publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
|
||||
{
|
||||
throw new InvalidOperationException("Wasn't possible to get a published snapshot");
|
||||
}
|
||||
var publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot();
|
||||
if (inter is int id)
|
||||
{
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Umbraco.Cms.Core.Models.PublishedContent;
|
||||
using Umbraco.Cms.Core.PublishedCache;
|
||||
using Umbraco.Extensions;
|
||||
|
||||
namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters
|
||||
{
|
||||
@@ -27,10 +28,7 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters
|
||||
_publishedModelFactory = publishedModelFactory;
|
||||
}
|
||||
|
||||
public override bool IsConverter(IPublishedPropertyType propertyType)
|
||||
{
|
||||
return propertyType.EditorAlias.Equals(Constants.PropertyEditors.Aliases.MediaPicker);
|
||||
}
|
||||
public override bool IsConverter(IPublishedPropertyType propertyType) => propertyType.EditorAlias.Equals(Constants.PropertyEditors.Aliases.MediaPicker);
|
||||
|
||||
public override Type GetPropertyValueType(IPublishedPropertyType propertyType)
|
||||
{
|
||||
@@ -73,10 +71,7 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters
|
||||
|
||||
if (udis.Any())
|
||||
{
|
||||
if (!_publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
|
||||
{
|
||||
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
|
||||
}
|
||||
var publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot();
|
||||
foreach (var udi in udis)
|
||||
{
|
||||
var guidUdi = udi as GuidUdi;
|
||||
@@ -92,9 +87,6 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters
|
||||
return source;
|
||||
}
|
||||
|
||||
private object FirstOrDefault(IList mediaItems)
|
||||
{
|
||||
return mediaItems.Count == 0 ? null : mediaItems[0];
|
||||
}
|
||||
private object FirstOrDefault(IList mediaItems) => mediaItems.Count == 0 ? null : mediaItems[0];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,11 +58,7 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters
|
||||
|
||||
|
||||
IPublishedContent member;
|
||||
|
||||
if (!_publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
|
||||
{
|
||||
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
|
||||
}
|
||||
var publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot();
|
||||
if (source is int id)
|
||||
{
|
||||
IMember m = _memberService.GetById(id);
|
||||
|
||||
@@ -86,10 +86,7 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters
|
||||
var multiNodeTreePicker = new List<IPublishedContent>();
|
||||
|
||||
var objectType = UmbracoObjectTypes.Unknown;
|
||||
if (!_publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
|
||||
{
|
||||
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
|
||||
}
|
||||
var publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot();
|
||||
foreach (var udi in udis)
|
||||
{
|
||||
var guidUdi = udi as GuidUdi;
|
||||
@@ -169,9 +166,6 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters
|
||||
return content;
|
||||
}
|
||||
|
||||
private static bool IsSingleNodePicker(IPublishedPropertyType propertyType)
|
||||
{
|
||||
return propertyType.DataType.ConfigurationAs<MultiNodePickerConfiguration>().MaxNumber == 1;
|
||||
}
|
||||
private static bool IsSingleNodePicker(IPublishedPropertyType propertyType) => propertyType.DataType.ConfigurationAs<MultiNodePickerConfiguration>().MaxNumber == 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ using System;
|
||||
using Umbraco.Cms.Core.Cache;
|
||||
using Umbraco.Cms.Core.Models.PublishedContent;
|
||||
using Umbraco.Cms.Core.PropertyEditors;
|
||||
using Umbraco.Extensions;
|
||||
|
||||
namespace Umbraco.Cms.Core.PublishedCache
|
||||
{
|
||||
@@ -74,10 +75,7 @@ namespace Umbraco.Cms.Core.PublishedCache
|
||||
public object XPathValue;
|
||||
}
|
||||
|
||||
public static string PropertyCacheValues(Guid contentUid, string typeAlias, bool previewing)
|
||||
{
|
||||
return "PublishedSnapshot.Property.CacheValues[" + (previewing ? "D:" : "P:") + contentUid + ":" + typeAlias + "]";
|
||||
}
|
||||
public static string PropertyCacheValues(Guid contentUid, string typeAlias, bool previewing) => "PublishedSnapshot.Property.CacheValues[" + (previewing ? "D:" : "P:") + contentUid + ":" + typeAlias + "]";
|
||||
|
||||
private void GetCacheLevels(out PropertyCacheLevel cacheLevel, out PropertyCacheLevel referenceCacheLevel)
|
||||
{
|
||||
@@ -140,10 +138,7 @@ namespace Umbraco.Cms.Core.PublishedCache
|
||||
cacheValues = (CacheValues) snapshotCache?.Get(ValuesCacheKey, () => new CacheValues()) ?? new CacheValues();
|
||||
break;
|
||||
case PropertyCacheLevel.Snapshot:
|
||||
if (!_publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
|
||||
{
|
||||
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
|
||||
}
|
||||
var publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot();
|
||||
// cache within the snapshot cache
|
||||
var facadeCache = publishedSnapshot.SnapshotCache;
|
||||
cacheValues = (CacheValues) facadeCache?.Get(ValuesCacheKey, () => new CacheValues()) ?? new CacheValues();
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using Umbraco.Cms.Core.Models.PublishedContent;
|
||||
using Umbraco.Cms.Core.PublishedCache;
|
||||
using Umbraco.Extensions;
|
||||
|
||||
namespace Umbraco.Cms.Infrastructure.ModelsBuilder
|
||||
{
|
||||
@@ -32,10 +33,7 @@ namespace Umbraco.Cms.Infrastructure.ModelsBuilder
|
||||
|
||||
public static IPublishedContentType GetModelContentType(IPublishedSnapshotAccessor publishedSnapshotAccessor, PublishedItemType itemType, string alias)
|
||||
{
|
||||
if (!publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
|
||||
{
|
||||
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
|
||||
}
|
||||
var publishedSnapshot = publishedSnapshotAccessor.GetRequiredPublishedSnapshot();
|
||||
switch (itemType)
|
||||
{
|
||||
case PublishedItemType.Content:
|
||||
|
||||
@@ -7,7 +7,6 @@ using System.Linq;
|
||||
using System.Runtime.Serialization;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Umbraco.Cms.Core.Hosting;
|
||||
using Umbraco.Cms.Core.IO;
|
||||
using Umbraco.Cms.Core.Models;
|
||||
using Umbraco.Cms.Core.Models.ContentEditing;
|
||||
@@ -18,7 +17,6 @@ using Umbraco.Cms.Core.Routing;
|
||||
using Umbraco.Cms.Core.Serialization;
|
||||
using Umbraco.Cms.Core.Services;
|
||||
using Umbraco.Cms.Core.Strings;
|
||||
using Umbraco.Cms.Core.Web;
|
||||
using Umbraco.Extensions;
|
||||
|
||||
namespace Umbraco.Cms.Core.PropertyEditors
|
||||
@@ -95,10 +93,7 @@ namespace Umbraco.Cms.Core.PropertyEditors
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (!_publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
|
||||
{
|
||||
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
|
||||
}
|
||||
var publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot();
|
||||
if (entity is IDocumentEntitySlim documentEntity)
|
||||
{
|
||||
icon = documentEntity.ContentTypeIcon;
|
||||
|
||||
@@ -5,6 +5,7 @@ using System;
|
||||
using Umbraco.Cms.Core.Models.Blocks;
|
||||
using Umbraco.Cms.Core.Models.PublishedContent;
|
||||
using Umbraco.Cms.Core.PublishedCache;
|
||||
using Umbraco.Extensions;
|
||||
|
||||
namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters
|
||||
{
|
||||
@@ -26,10 +27,7 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters
|
||||
BlockItemData data,
|
||||
PropertyCacheLevel referenceCacheLevel, bool preview)
|
||||
{
|
||||
if (!_publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
|
||||
{
|
||||
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
|
||||
}
|
||||
var publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot();
|
||||
// hack! we need to cast, we have no choice beacuse we cannot make breaking changes.
|
||||
var publishedContentCache = publishedSnapshot.Content;
|
||||
|
||||
|
||||
@@ -63,10 +63,7 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters
|
||||
var mediaItems = new List<MediaWithCrops>();
|
||||
var dtos = MediaPicker3PropertyEditor.MediaPicker3PropertyValueEditor.Deserialize(_jsonSerializer, inter);
|
||||
var configuration = propertyType.DataType.ConfigurationAs<MediaPicker3Configuration>();
|
||||
if (!_publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
|
||||
{
|
||||
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
|
||||
}
|
||||
var publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot();
|
||||
foreach (var dto in dtos)
|
||||
{
|
||||
var mediaItem = publishedSnapshot.Media.GetById(preview, dto.MediaKey);
|
||||
|
||||
@@ -58,10 +58,7 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters
|
||||
|
||||
var links = new List<Link>();
|
||||
var dtos = _jsonSerializer.Deserialize<IEnumerable<MultiUrlPickerValueEditor.LinkDto>>(inter.ToString());
|
||||
if (_publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
|
||||
{
|
||||
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
|
||||
}
|
||||
var publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot();
|
||||
foreach (var dto in dtos)
|
||||
{
|
||||
var type = LinkType.External;
|
||||
|
||||
@@ -46,10 +46,7 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters
|
||||
var elementTypeAlias = sourceObject[NestedContentPropertyEditor.ContentTypeAliasPropertyKey]?.ToObject<string>();
|
||||
if (string.IsNullOrEmpty(elementTypeAlias))
|
||||
return null;
|
||||
if (!_publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
|
||||
{
|
||||
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
|
||||
}
|
||||
var publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot();
|
||||
// only convert element types - content types will cause an exception when PublishedModelFactory creates the model
|
||||
var publishedContentType = publishedSnapshot.Content.GetContentType(elementTypeAlias);
|
||||
if (publishedContentType == null || publishedContentType.IsElement == false)
|
||||
|
||||
@@ -624,10 +624,7 @@ namespace Umbraco.Cms.Core.Security
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (_publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
|
||||
{
|
||||
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
|
||||
}
|
||||
var publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot();
|
||||
return publishedSnapshot.Members.Get(member);
|
||||
}
|
||||
|
||||
|
||||
@@ -142,10 +142,7 @@ namespace Umbraco.Cms.Infrastructure.PublishedCache
|
||||
// elements cache (if we don't want to pollute the elements cache with short-lived
|
||||
// data) depending on settings
|
||||
// for members, always cache in the snapshot cache - never pollute elements cache
|
||||
if (!_publishedSnapshotAccessor.TryGetPublishedSnapshot(out publishedSnapshot))
|
||||
{
|
||||
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
|
||||
}
|
||||
publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot();
|
||||
cache = publishedSnapshot == null
|
||||
? null
|
||||
: ((_isPreviewing == false || PublishedSnapshotService.FullCacheWhenPreviewing) && (_isMember == false)
|
||||
@@ -155,10 +152,7 @@ namespace Umbraco.Cms.Infrastructure.PublishedCache
|
||||
break;
|
||||
case PropertyCacheLevel.Snapshot:
|
||||
// cache within the snapshot cache
|
||||
if (!_publishedSnapshotAccessor.TryGetPublishedSnapshot(out publishedSnapshot))
|
||||
{
|
||||
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
|
||||
}
|
||||
publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot();
|
||||
cache = publishedSnapshot?.SnapshotCache;
|
||||
cacheValues = GetCacheValues(cache);
|
||||
break;
|
||||
|
||||
@@ -238,10 +238,7 @@ namespace Umbraco.Cms.Infrastructure.PublishedCache
|
||||
get
|
||||
{
|
||||
var getById = GetGetterById();
|
||||
if (!_publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
|
||||
{
|
||||
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
|
||||
}
|
||||
var publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot();
|
||||
return getById(publishedSnapshot, IsPreviewing, ParentId);
|
||||
}
|
||||
}
|
||||
@@ -252,10 +249,7 @@ namespace Umbraco.Cms.Infrastructure.PublishedCache
|
||||
get
|
||||
{
|
||||
var getById = GetGetterById();
|
||||
if (!_publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
|
||||
{
|
||||
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
|
||||
}
|
||||
var publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot();
|
||||
var id = _contentNode.FirstChildContentId;
|
||||
|
||||
while (id > 0)
|
||||
@@ -322,10 +316,7 @@ namespace Umbraco.Cms.Infrastructure.PublishedCache
|
||||
// beware what you use that one for - you don't want to cache its result
|
||||
private IAppCache GetAppropriateCache()
|
||||
{
|
||||
if (!_publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
|
||||
{
|
||||
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
|
||||
}
|
||||
var publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot();
|
||||
var cache = publishedSnapshot == null
|
||||
? null
|
||||
: ((IsPreviewing == false || PublishedSnapshotService.FullCacheWhenPreviewing) && (ContentType.ItemType != PublishedItemType.Member)
|
||||
@@ -336,10 +327,7 @@ namespace Umbraco.Cms.Infrastructure.PublishedCache
|
||||
|
||||
private IAppCache GetCurrentSnapshotCache()
|
||||
{
|
||||
if (!_publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
|
||||
{
|
||||
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
|
||||
}
|
||||
var publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot();
|
||||
return publishedSnapshot?.SnapshotCache;
|
||||
}
|
||||
|
||||
|
||||
@@ -205,10 +205,7 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.PropertyEditors
|
||||
PropertyCacheLevel referenceCacheLevel,
|
||||
object inter,
|
||||
bool preview) {
|
||||
if(!_publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
|
||||
{
|
||||
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
|
||||
}
|
||||
var publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot();
|
||||
return ((int[])inter).Select(x =>
|
||||
(PublishedSnapshotTestObjects.TestContentModel1)publishedSnapshot.Content
|
||||
.GetById(x)).ToArray();
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Cms.Core.Models;
|
||||
@@ -13,9 +12,7 @@ using Umbraco.Cms.Core.PropertyEditors;
|
||||
using Umbraco.Cms.Core.PublishedCache;
|
||||
using Umbraco.Cms.Core.PublishedCache.Internal;
|
||||
using Umbraco.Cms.Core.Services;
|
||||
using Umbraco.Cms.Core.Strings;
|
||||
using Umbraco.Cms.Infrastructure.Serialization;
|
||||
using Umbraco.Cms.Tests.Common.TestHelpers;
|
||||
using Umbraco.Extensions;
|
||||
|
||||
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Published
|
||||
@@ -172,10 +169,7 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Published
|
||||
|
||||
public object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview)
|
||||
{
|
||||
if(!_publishedSnapshotAccessor.TryGetPublishedSnapshot(out var publishedSnapshot))
|
||||
{
|
||||
throw new InvalidOperationException("Wasn't possible to a get a valid Snapshot");
|
||||
}
|
||||
var publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot();
|
||||
return publishedSnapshot.Content.GetById((int)inter);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user