Skip to content

Commit 05538e4

Browse files
authored
Merge pull request #192 from umbraco/bugfix/update-search-property-factory-across-versions
Updated convertes, index value retrieval and UI fix.
2 parents 11e3b5a + 020d5a5 commit 05538e4

File tree

11 files changed

+99
-61
lines changed

11 files changed

+99
-61
lines changed

src/Umbraco.Cms.Integrations.Search.Algolia/App_Plugins/UmbracoCms.Integrations/Search/Algolia/js/dashboard.controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,9 @@
382382
headline: 'Algolia',
383383
message: 'Index built successfully'
384384
});
385-
vm.loading = false;
386385
}
387386
});
387+
vm.loading = false;
388388

389389
closeBuildDialog();
390390
}

src/Umbraco.Cms.Integrations.Search.Algolia/App_Plugins/UmbracoCms.Integrations/Search/Algolia/views/dashboard.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,11 @@ <h5>Manage Algolia Indices</h5>
117117
<uui-label slot="label">{{ vm.manageIndex.selectedContentType.name }} Properties</uui-label>
118118
<uui-icon-registry-essential>
119119
<div class="alg-col-3">
120-
<uui-card-content-node selectable
120+
<uui-card-content-node selectable ng-attr-id="{{property.alias}}"
121121
ng-attr-selected="{{property.selected || undefined}}"
122122
ng-on-selected="vm.manageIndex.selectProperty(property)"
123123
ng-on-unselected="vm.manageIndex.removeProperty(property)"
124+
ng-on-deselected="vm.manageIndex.removeProperty(property)"
124125
ng-repeat="property in vm.manageIndex.propertiesList"
125126
name="{{ property.name }}">
126127
<uui-tag ng-if="property.selected" size="s" slot="tag" color="primary">Selected</uui-tag>

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: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,68 @@
11
using Umbraco.Cms.Core.Models;
2-
using Umbraco.Cms.Core.Models.PublishedContent;
32
using Umbraco.Cms.Core.PropertyEditors;
3+
using Umbraco.Cms.Core.Services;
44
using Umbraco.Cms.Integrations.Search.Algolia.Providers;
55

66
namespace Umbraco.Cms.Integrations.Search.Algolia.Services
77
{
88
public class AlgoliaSearchPropertyIndexValueFactory : IAlgoliaSearchPropertyIndexValueFactory
99
{
1010
private readonly PropertyEditorCollection _propertyEditorsCollection;
11-
1211
private readonly ConverterCollection _converterCollection;
1312

13+
private readonly ILocalizationService _localizationService;
14+
private readonly IContentTypeService _contentTypeService;
15+
1416
public AlgoliaSearchPropertyIndexValueFactory(
1517
PropertyEditorCollection propertyEditorCollection,
16-
ConverterCollection converterCollection)
18+
ConverterCollection converterCollection,
19+
ILocalizationService localizationService,
20+
IContentTypeService contentTypeService)
1721
{
1822
_propertyEditorsCollection = propertyEditorCollection;
19-
2023
_converterCollection = converterCollection;
24+
_localizationService = localizationService;
25+
_contentTypeService = contentTypeService;
2126
}
2227

2328
public virtual KeyValuePair<string, object> GetValue(IProperty property, string culture)
2429
{
30+
var availableCultures = _localizationService.GetAllLanguages().Select(p => p.IsoCode);
31+
IDictionary<Guid, IContentType> contentTypeDictionary = _contentTypeService.GetAll().ToDictionary(x => x.Key);
32+
2533
var propertyEditor = _propertyEditorsCollection.FirstOrDefault(p => p.Alias == property.PropertyType.PropertyEditorAlias);
2634
if (propertyEditor == null)
2735
{
2836
return default;
2937
}
3038

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-
3739
var converter = _converterCollection.FirstOrDefault(p => p.Name == property.PropertyType.PropertyEditorAlias);
3840
if (converter != null)
3941
{
40-
var result = converter.ParseIndexValues(property, indexValue.Value);
42+
var result = converter.ParseIndexValues(property);
4143
return new KeyValuePair<string, object>(property.Alias, result);
4244
}
4345

46+
IEnumerable<KeyValuePair<string, IEnumerable<object>>> indexValues =
47+
#if NET6_0 || NET7_0
48+
propertyEditor.PropertyIndexValueFactory.GetIndexValues(
49+
property,
50+
culture,
51+
null,
52+
true);
53+
#else
54+
propertyEditor.PropertyIndexValueFactory.GetIndexValues(
55+
property,
56+
culture,
57+
null,
58+
true,
59+
availableCultures,
60+
contentTypeDictionary);
61+
#endif
62+
if (indexValues == null || !indexValues.Any()) return new KeyValuePair<string, object>(property.Alias, string.Empty);
63+
64+
var indexValue = indexValues.First();
65+
4466
return new KeyValuePair<string, object>(property.Alias, indexValue.Value);
4567
}
4668
}

0 commit comments

Comments
 (0)