Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract price properties and index as double #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Umbraco.Commerce.DemoStore/DemoStoreBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Extensions;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Commerce.DemoStore.Web.Index;
using Microsoft.Extensions.DependencyInjection;

namespace Umbraco.Commerce.DemoStore
{
Expand Down Expand Up @@ -43,6 +45,7 @@ public static IUmbracoBuilder AddDemoStore(this IUmbracoBuilder umbracoBuilder)
});

umbracoBuilder.AddNotificationHandler<UmbracoApplicationStartingNotification, TransformExamineValues>();
umbracoBuilder.Services.ConfigureOptions<ConfigureIndexOptions>();

return umbracoBuilder;
}
Expand Down
25 changes: 22 additions & 3 deletions src/Umbraco.Commerce.DemoStore/Events/TransformExamineValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,31 @@
using Umbraco.Extensions;
using Umbraco.Commerce.DemoStore.Models;
using System.Text;
using Newtonsoft.Json;
using Umbraco.Commerce.Core.Services;
using System;

namespace Umbraco.Commerce.DemoStore.Events
{
public class TransformExamineValues : INotificationHandler<UmbracoApplicationStartingNotification>
{
private readonly IExamineManager _examineManager;
private readonly IUmbracoContextFactory _umbracoContextFactory;
private readonly ICurrencyService _currencyService;

public TransformExamineValues(IExamineManager examineManager,
IUmbracoContextFactory umbracoContextFactory)
IUmbracoContextFactory umbracoContextFactory,
ICurrencyService currencyService)
{
_examineManager = examineManager;
_umbracoContextFactory = umbracoContextFactory;
_currencyService = currencyService;
}


public void Handle(UmbracoApplicationStartingNotification notification)
{
// Listen for nodes being reindexed in the external index set
if (_examineManager.TryGetIndex("ExternalIndex", out var index))
if (_examineManager.TryGetIndex(Constants.UmbracoIndexes.ExternalIndexName, out var index))
{
((BaseIndexProvider)index).TransformingIndexValues += (object sender, IndexingItemEventArgs e) =>
{
Expand Down Expand Up @@ -72,6 +77,20 @@ public void Handle(UmbracoApplicationStartingNotification notification)
values.Add("categoryAliases", new[] { string.Join(" ", categoryAliases) });
}
}

if (e.ValueSet.Values.ContainsKey("price"))
{
var prices = JsonConvert.DeserializeObject<Dictionary<Guid, string>>(e.ValueSet.GetValue("price").ToString());

foreach (var price in prices)
{
var currency = _currencyService.GetCurrency(price.Key);
if (currency == null)
continue;

values.Add($"price_{currency.Code}", new[] { price.Value });
}
}
}

// ================================================================
Expand Down
35 changes: 35 additions & 0 deletions src/Umbraco.Commerce.DemoStore/Web/Index/ConfigureIndexOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Examine.Lucene;
using Examine;
using Microsoft.Extensions.Options;
using Lucene.Net.Facet;
using System.Collections.Generic;
using Umbraco.Cms.Core;

namespace Umbraco.Commerce.DemoStore.Web.Index
{
public sealed class ConfigureIndexOptions : IConfigureNamedOptions<LuceneDirectoryIndexOptions>
{
public void Configure(string name, LuceneDirectoryIndexOptions options)
{
switch (name)
{
case Constants.UmbracoIndexes.ExternalIndexName:

var priceFields = new List<string>
{
"price_GBP"
};

foreach (var field in priceFields)
{
options.FieldDefinitions.TryAdd(new FieldDefinition(field, FieldDefinitionTypes.Double));
}

break;
}
}

public void Configure(LuceneDirectoryIndexOptions options)
=> Configure(string.Empty, options);
}
}