Skip to content

Commit

Permalink
feat: mark the IRelewisePropertyConverter as public, thus exposing it… (
Browse files Browse the repository at this point in the history
#32)

* feat: mark the IRelewisePropertyConverter as public, thus exposing it to the DI container

* update minor version according to SemVer
  • Loading branch information
brianhdk authored Jun 21, 2024
1 parent ec9114d commit b111943
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 27 deletions.
4 changes: 4 additions & 0 deletions samples/UmbracoV12/Relewise.UmbracoV12.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<PropertyGroup>
<NoWarn>NU1902</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Relewise.Client" Version="1.132.0" />
<PackageReference Include="Relewise.Client.Extensions" Version="1.4.1" />
Expand Down
2 changes: 1 addition & 1 deletion samples/UmbracoV13/Relewise.UmbracoV13.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<PropertyGroup>
<NoWarn>NU1608;NU1903</NoWarn>
<NoWarn>NU1608;NU1902;NU1903</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Integrations.Umbraco/Integrations.Umbraco.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<PackageProjectUrl>https://github.com/Relewise/relewise-integrations-umbraco</PackageProjectUrl>
<RepositoryUrl>https://github.com/Relewise/relewise-integrations-umbraco</RepositoryUrl>
<PackageId>Relewise.Integrations.Umbraco</PackageId>
<PackageVersion>1.7.2</PackageVersion>
<PackageVersion>1.8.0</PackageVersion>
<Authors>Relewise</Authors>
<Company>Relewise</Company>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,8 @@

namespace Relewise.Integrations.Umbraco.PropertyValueConverters;

internal class NestedContentPropertyValueConverter : IRelewisePropertyValueConverter
internal class NestedContentPropertyValueConverter(IServiceProvider serviceProvider) : IRelewisePropertyValueConverter
{
private readonly IServiceProvider _serviceProvider;

public NestedContentPropertyValueConverter(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}

public bool CanHandle(RelewisePropertyConverterContext context)
{
return context.Property.PropertyType.EditorAlias.Equals("Umbraco.NestedContent");
Expand All @@ -30,13 +23,13 @@ public void Convert(RelewisePropertyConverterContext context)
if (elementItems != null)
{
// NOTE: This needs to be resolved manually, since it would cause a circular dependency if injected through constructor
var propertyConverter = _serviceProvider.GetRequiredService<IRelewisePropertyConverter>();
var propertyConverter = serviceProvider.GetRequiredService<IRelewisePropertyConverter>();

var properties = new List<DataValue?>();

foreach (IPublishedElement prop in elementItems)
{
Dictionary<string, DataValue?> converted = propertyConverter.Convert(prop.Properties, context.Culture);
IReadOnlyDictionary<string, DataValue?> converted = propertyConverter.Convert(prop.Properties, context.Culture);

properties.AddRange(converted.Values);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Integrations.Umbraco/Services/ContentMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ private async Task AutoMapOrUseMapper(MapContent content, List<string> culturesT
}
else
{
contentUpdate.Content.Data = _propertyConverter.Convert(content.PublishedContent.Properties, culturesToPublish.ToArray());
contentUpdate.Content.Data = _propertyConverter
.Convert(content.PublishedContent.Properties, culturesToPublish.ToArray())
.ToDictionary(x => x.Key, x => x.Value);
}

contentUpdate.Content.Data ??= new Dictionary<string, DataValue?>();
Expand Down
26 changes: 22 additions & 4 deletions src/Integrations.Umbraco/Services/IRelewisePropertyConverter.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Relewise.Client.DataTypes;
using Umbraco.Cms.Core.Models.PublishedContent;

namespace Relewise.Integrations.Umbraco.Services;

internal interface IRelewisePropertyConverter
/// <summary>
/// Converts a collection of published properties to a dictionary of data values, supporting multiple cultures.
/// </summary>
public interface IRelewisePropertyConverter
{
Dictionary<string, DataValue?> Convert(IEnumerable<IPublishedProperty> properties, string[] cultures);
/// <summary>
/// Converts the given collection of properties to a read-only dictionary of data values.
/// The conversion supports multiple cultures.
/// </summary>
/// <param name="properties">A collection of published properties to be converted.</param>
/// <param name="cultures">An array of culture codes to consider during conversion.</param>
/// <returns>A read-only dictionary where keys are property names and values are data values.</returns>
IReadOnlyDictionary<string, DataValue?> Convert(IEnumerable<IPublishedProperty> properties, string[] cultures);

public Dictionary<string, DataValue?> Convert(IEnumerable<IPublishedProperty> properties, string culture) => Convert(properties, new[] { culture });
/// <summary>
/// Converts the given collection of properties to a read-only dictionary of data values.
/// The conversion supports multiple cultures.
/// </summary>
/// <param name="properties">A collection of published properties to be converted.</param>
/// <param name="culture">The culture code for which the property should be converted.</param>
/// <returns>A read-only dictionary where keys are property names and values are data values.</returns>
public IReadOnlyDictionary<string, DataValue?> Convert(IEnumerable<IPublishedProperty> properties, string culture) => Convert(properties, [culture]);
}
14 changes: 4 additions & 10 deletions src/Integrations.Umbraco/Services/RelewisePropertyConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,10 @@

namespace Relewise.Integrations.Umbraco.Services;

internal class RelewisePropertyConverter : IRelewisePropertyConverter
internal class RelewisePropertyConverter(IEnumerable<IRelewisePropertyValueConverter> converters)
: IRelewisePropertyConverter
{
private readonly IEnumerable<IRelewisePropertyValueConverter> _converters;

public RelewisePropertyConverter(IEnumerable<IRelewisePropertyValueConverter> converters)
{
_converters = converters;
}

public Dictionary<string, DataValue?> Convert(IEnumerable<IPublishedProperty> properties, string[] cultures)
public IReadOnlyDictionary<string, DataValue?> Convert(IEnumerable<IPublishedProperty> properties, string[] cultures)
{
if (properties == null) throw new ArgumentNullException(nameof(properties));
if (cultures == null) throw new ArgumentNullException(nameof(cultures));
Expand Down Expand Up @@ -75,7 +69,7 @@ private void Convert(IPublishedProperty property, string culture, Dictionary<str
{
var context = new RelewisePropertyConverterContext(property, culture, dataKeys);

foreach (IRelewisePropertyValueConverter converter in _converters)
foreach (IRelewisePropertyValueConverter converter in converters)
{
if (converter.CanHandle(context))
converter.Convert(context);
Expand Down

0 comments on commit b111943

Please sign in to comment.