Skip to content

Commit 8b0685f

Browse files
committed
Updated convertes, index value retrieval and UI fix.
1 parent 4cbbf97 commit 8b0685f

9 files changed

+70
-55
lines changed

src/Umbraco.Cms.Integrations.Search.Algolia/Converters/IAlgoliaIndexValueConverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public interface IAlgoliaIndexValueConverter
1313
string Name { get; }
1414

1515
/// <summary>
16-
/// Parses the index values.
16+
/// Parses the property's index values.
1717
/// </summary>
18-
object ParseIndexValues(IProperty property, IEnumerable<object> indexValues);
18+
object ParseIndexValues(IProperty property);
1919
}
2020
}
Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
11
using Umbraco.Cms.Core.Models;
2+
using Umbraco.Cms.Integrations.Search.Algolia.Extensions;
23

34
namespace Umbraco.Cms.Integrations.Search.Algolia.Converters
45
{
56
public class UmbracoBooleanConverter : IAlgoliaIndexValueConverter
67
{
78
public string Name => Core.Constants.PropertyEditors.Aliases.Boolean;
89

9-
public object ParseIndexValues(IProperty property, IEnumerable<object> indexValues)
10-
{
11-
if (indexValues != null && indexValues.Any())
12-
{
13-
var value = indexValues.FirstOrDefault();
10+
public object ParseIndexValues(IProperty property) =>
11+
property.TryGetPropertyIndexValue(out string value)
12+
? value.Equals("1")
13+
: default;
1414

15-
return value != null
16-
? value.Equals(1)
17-
: default;
18-
}
19-
20-
return default(bool);
21-
}
2215
}
2316
}
Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
using Umbraco.Cms.Core.Models;
2+
using Umbraco.Cms.Integrations.Search.Algolia.Extensions;
23

34
namespace Umbraco.Cms.Integrations.Search.Algolia.Converters
45
{
56
public class UmbracoDecimalConverter : IAlgoliaIndexValueConverter
67
{
78
public string Name => Core.Constants.PropertyEditors.Aliases.Decimal;
89

9-
public object ParseIndexValues(IProperty property, IEnumerable<object> indexValues)
10-
{
11-
if (indexValues != null && indexValues.Any())
12-
{
13-
var value = indexValues.FirstOrDefault();
14-
15-
return value != null
16-
? decimal.Parse(value.ToString())
17-
: default;
18-
}
19-
20-
return default(decimal);
21-
}
10+
public object ParseIndexValues(IProperty property) =>
11+
property.TryGetPropertyIndexValue(out string value)
12+
? (decimal.TryParse(value.ToString(), out var result)
13+
? result
14+
: default)
15+
: default;
2216
}
2317
}
Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
using Umbraco.Cms.Core.Models;
2+
using Umbraco.Cms.Integrations.Search.Algolia.Extensions;
23

34
namespace Umbraco.Cms.Integrations.Search.Algolia.Converters
45
{
56
public class UmbracoIntegerConverter : IAlgoliaIndexValueConverter
67
{
78
public string Name => Core.Constants.PropertyEditors.Aliases.Integer;
89

9-
public object ParseIndexValues(IProperty property, IEnumerable<object> indexValues)
10-
{
11-
if (indexValues != null && indexValues.Any())
12-
{
13-
var value = indexValues.FirstOrDefault();
14-
15-
return value ?? default(int);
16-
}
17-
18-
return default(int);
19-
}
10+
public object ParseIndexValues(IProperty property) =>
11+
property.TryGetPropertyIndexValue(out string value)
12+
? (int.TryParse(value.ToString(), out var result)
13+
? result
14+
: default)
15+
: default;
2016
}
2117
}

src/Umbraco.Cms.Integrations.Search.Algolia/Converters/UmbracoMediaPickerConverter.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Text.Json;
22
using Umbraco.Cms.Core.Models;
33
using Umbraco.Cms.Core.Services;
4+
using Umbraco.Cms.Integrations.Search.Algolia.Extensions;
45

56
namespace Umbraco.Cms.Integrations.Search.Algolia.Converters
67
{
@@ -12,15 +13,16 @@ public class UmbracoMediaPickerConverter : IAlgoliaIndexValueConverter
1213

1314
public string Name => Core.Constants.PropertyEditors.Aliases.MediaPicker3;
1415

15-
public object ParseIndexValues(IProperty property, IEnumerable<object> indexValues)
16+
public object ParseIndexValues(IProperty property)
1617
{
1718
var list = new List<string>();
1819

19-
var parsedIndexValue = ParseIndexValue(indexValues);
20-
21-
if (string.IsNullOrEmpty(parsedIndexValue)) return list;
20+
if (!property.TryGetPropertyIndexValue(out string value))
21+
{
22+
return list;
23+
}
2224

23-
var inputMedia = JsonSerializer.Deserialize<IEnumerable<MediaItem>>(parsedIndexValue);
25+
var inputMedia = JsonSerializer.Deserialize<IEnumerable<MediaItem>>(value);
2426

2527
if (inputMedia == null) return string.Empty;
2628

src/Umbraco.Cms.Integrations.Search.Algolia/Converters/UmbracoTagsConverter.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
1-
using Umbraco.Cms.Core.Models;
1+
using System.Text.Json;
2+
using Umbraco.Cms.Core.Models;
3+
using Umbraco.Cms.Integrations.Search.Algolia.Extensions;
24

35
namespace Umbraco.Cms.Integrations.Search.Algolia.Converters
46
{
57
public class UmbracoTagsConverter : IAlgoliaIndexValueConverter
68
{
79
public string Name => Core.Constants.PropertyEditors.Aliases.Tags;
810

9-
public object ParseIndexValues(IProperty property, IEnumerable<object> indexValues)
11+
public object ParseIndexValues(IProperty property)
1012
{
11-
if (indexValues != null && indexValues.Any())
13+
if (!property.TryGetPropertyIndexValue(out string value))
1214
{
13-
return indexValues;
15+
return Enumerable.Empty<string>();
16+
}
17+
18+
var valuesArr = JsonSerializer.Deserialize<List<string>>(value);
19+
if (valuesArr != null && valuesArr.Any())
20+
{
21+
return valuesArr.Select(p => p);
1422
}
1523

1624
return Enumerable.Empty<string>();
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Umbraco.Cms.Core.Models;
2+
3+
namespace Umbraco.Cms.Integrations.Search.Algolia.Extensions
4+
{
5+
public static class ConverterExtensions
6+
{
7+
public static bool TryGetPropertyIndexValue(this IProperty property, out string value)
8+
{
9+
bool success = true;
10+
value = string.Empty;
11+
12+
if (property.GetValue() is null || string.IsNullOrEmpty(property.GetValue().ToString()))
13+
{
14+
success = false;
15+
}
16+
17+
value = property.GetValue().ToString();
18+
19+
return success;
20+
}
21+
}
22+
}

src/Umbraco.Cms.Integrations.Search.Algolia/Services/AlgoliaSearchPropertyIndexValueFactory.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ public virtual KeyValuePair<string, object> GetValue(IProperty property, string
2828
return default;
2929
}
3030

31-
var indexValues = propertyEditor.PropertyIndexValueFactory.GetIndexValues(property, culture, null, true);
32-
33-
if (indexValues == null || !indexValues.Any()) return new KeyValuePair<string, object>(property.Alias, string.Empty);
34-
35-
var indexValue = indexValues.First();
36-
3731
var converter = _converterCollection.FirstOrDefault(p => p.Name == property.PropertyType.PropertyEditorAlias);
3832
if (converter != null)
3933
{
40-
var result = converter.ParseIndexValues(property, indexValue.Value);
34+
var result = converter.ParseIndexValues(property);
4135
return new KeyValuePair<string, object>(property.Alias, result);
4236
}
4337

38+
IEnumerable<KeyValuePair<string, IEnumerable<object>>> indexValues = propertyEditor.PropertyIndexValueFactory.GetIndexValues(property, culture, null, true);
39+
40+
if (indexValues == null || !indexValues.Any()) return new KeyValuePair<string, object>(property.Alias, string.Empty);
41+
42+
var indexValue = indexValues.First();
43+
4444
return new KeyValuePair<string, object>(property.Alias, indexValue.Value);
4545
}
4646
}

src/Umbraco.Cms.Integrations.Search.Algolia/Umbraco.Cms.Integrations.Search.Algolia.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<PackageIconUrl></PackageIconUrl>
1414
<PackageProjectUrl>https://github.com/umbraco/Umbraco.Cms.Integrations/tree/main/src/Umbraco.Cms.Integrations.Search.Algolia</PackageProjectUrl>
1515
<RepositoryUrl>https://github.com/umbraco/Umbraco.Cms.Integrations</RepositoryUrl>
16-
<Version>2.1.5</Version>
16+
<Version>2.2.0</Version>
1717
<Authors>Umbraco HQ</Authors>
1818
<Company>Umbraco</Company>
1919
<PackageTags>Umbraco;Umbraco-Marketplace</PackageTags>

0 commit comments

Comments
 (0)