-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathHeroService.cs
70 lines (59 loc) · 2.87 KB
/
HeroService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System.Linq;
using Helixbase.Feature.Hero.Models;
using Helixbase.Foundation.Content.Repositories;
using Helixbase.Foundation.Logging.Repositories;
using Helixbase.Foundation.Search.Models;
using Sitecore.ContentSearch.Linq.Utilities;
using Sitecore.Data.Items;
namespace Helixbase.Feature.Hero.Services
{
public class HeroService : IHeroService
{
private readonly IContextRepository _contextRepository;
private readonly ILogRepository _logRepository;
private readonly IRenderingRepository _renderingRepository;
public HeroService(IContextRepository contextRepository,
ILogRepository logRepository, IRenderingRepository renderingRepository)
{
_contextRepository = contextRepository;
_logRepository = logRepository;
_renderingRepository = renderingRepository;
}
/// <summary>
/// Get an item using the rendering repository
/// </summary>
/// <returns>The Hero datasource item from the Content API</returns>
public IHeroContentType GetHeroItems()
{
var dataSource = _renderingRepository.GetDataSourceItem<IHeroContentType>();
// Basic example of using the wrapped logger
if (dataSource == null)
_logRepository.Warn(Logging.Error.DataSourceError);
return dataSource;
}
/// <summary>
/// **** This method is not required/in use. It is here as an example of how to use the computed search field ****
/// Get an item from the index
/// </summary>
/// <returns>The first item based on the Hero template</returns>
public BaseSearchResultItem GetHeroImagesSearch()
{
// First setup your predicate
var predicate = PredicateBuilder.True<BaseSearchResultItem>();
predicate = predicate.And(item => item.Templates.Contains(Constants.Hero.TemplateId));
predicate = predicate.And(item => !item.Name.Equals("__Standard Values"));
// We could set the index manually using the line below (do not use magic strings, sample only)
// var index = ContentSearchManager.GetIndex($"Helixbase_{_contextRepository.GetDatabaseContext()}_index");
// OR we could automate retrieval of the context index:
var contextIndex = _contextRepository.GetSearchIndexContext(_contextRepository.GetCurrentItem<Item>());
using (var context = contextIndex.CreateSearchContext())
{
var result = context.GetQueryable<BaseSearchResultItem>().Where(predicate).First();
return result;
// OR we could have populated a Glass model using:
// injectedSitecoreService.Populate(result);
}
}
public bool IsExperienceEditor => _contextRepository.IsExperienceEditor;
}
}