-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #158 from piotrkrajewskicn/feature/custom-record-v…
…alues Possibility to add custom record values in Algolia index
- Loading branch information
Showing
8 changed files
with
136 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/Umbraco.Cms.Integrations.Search.Algolia/Builders/IRecordBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Umbraco.Cms.Core.Models; | ||
using Umbraco.Cms.Core.Models.PublishedContent; | ||
using Umbraco.Cms.Integrations.Search.Algolia.Models; | ||
|
||
namespace Umbraco.Cms.Integrations.Search.Algolia.Builders | ||
{ | ||
public interface IRecordBuilder | ||
{ | ||
Record GetRecord(IContent content, Record record); | ||
} | ||
|
||
public interface IRecordBuilder<in TContentType> | ||
: IRecordBuilder where TContentType : PublishedContentModel | ||
{ | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/Umbraco.Cms.Integrations.Search.Algolia/Builders/IRecordBuilderFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Umbraco.Cms.Core.Models; | ||
|
||
namespace Umbraco.Cms.Integrations.Search.Algolia.Builders | ||
{ | ||
public interface IRecordBuilderFactory | ||
{ | ||
IRecordBuilder GetRecordBuilderService(IContent content); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/Umbraco.Cms.Integrations.Search.Algolia/Builders/RecordBuilderFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using Umbraco.Cms.Core.Models; | ||
using Umbraco.Cms.Core.Web; | ||
|
||
namespace Umbraco.Cms.Integrations.Search.Algolia.Builders | ||
{ | ||
public class RecordBuilderFactory : IRecordBuilderFactory | ||
{ | ||
private readonly IServiceProvider _serviceProvider; | ||
|
||
private readonly IUmbracoContextFactory _umbracoContextFactory; | ||
|
||
private readonly IDictionary<string, Type> _recordBuilderCache = new Dictionary<string, Type>(); | ||
|
||
public RecordBuilderFactory(IServiceProvider serviceProvider, IUmbracoContextFactory umbracoContextFactory) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
_umbracoContextFactory = umbracoContextFactory; | ||
} | ||
|
||
public IRecordBuilder GetRecordBuilderService(IContent content) | ||
{ | ||
using var context = _umbracoContextFactory.EnsureUmbracoContext(); | ||
|
||
var contentType = context.UmbracoContext.Content | ||
?.GetById(true, content.Id) | ||
?.GetType(); | ||
|
||
if (contentType == null) | ||
{ | ||
return null; | ||
} | ||
var serviceType = GetRecordBuilderType(contentType); | ||
|
||
return _serviceProvider.GetService(serviceType) as IRecordBuilder; | ||
} | ||
|
||
private Type GetRecordBuilderType(Type contentType) | ||
{ | ||
var dictionaryKey = contentType.FullName; | ||
if (_recordBuilderCache.ContainsKey(dictionaryKey)) | ||
{ | ||
return _recordBuilderCache[dictionaryKey]; | ||
} | ||
|
||
var type = typeof(IRecordBuilder<>).MakeGenericType(contentType); | ||
_recordBuilderCache[dictionaryKey] = type; | ||
return type; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters