|
1 |
| -using Algolia.Search.Models.Search; |
2 |
| -using Asp.Versioning; |
| 1 | +using Asp.Versioning; |
3 | 2 | using Microsoft.AspNetCore.Http;
|
4 | 3 | using Microsoft.AspNetCore.Mvc;
|
5 | 4 | using Microsoft.Extensions.Logging;
|
|
13 | 12 | using Umbraco.Cms.Integrations.Search.Algolia.Models.ContentTypeDtos;
|
14 | 13 | using Umbraco.Cms.Integrations.Search.Algolia.Services;
|
15 | 14 |
|
16 |
| -namespace Umbraco.Cms.Integrations.Search.Algolia.Api.Management.Controllers |
| 15 | +namespace Umbraco.Cms.Integrations.Search.Algolia.Api.Management.Controllers; |
| 16 | + |
| 17 | +[ApiVersion("1.0")] |
| 18 | +[ApiExplorerSettings(GroupName = Constants.ManagementApi.GroupName)] |
| 19 | +public class BuildIndexController : SearchControllerBase |
17 | 20 | {
|
18 |
| - [ApiVersion("1.0")] |
19 |
| - [ApiExplorerSettings(GroupName = Constants.ManagementApi.GroupName)] |
20 |
| - public class BuildIndexController : SearchControllerBase |
| 21 | + private readonly IAlgoliaIndexDefinitionStorage<AlgoliaIndex> _indexStorage; |
| 22 | + private readonly IAlgoliaIndexService _indexService; |
| 23 | + private readonly IUmbracoContextFactory _umbracoContextFactory; |
| 24 | + private readonly IContentService _contentService; |
| 25 | + private readonly ILogger<BuildIndexController> _logger; |
| 26 | + private readonly IUserService _userService; |
| 27 | + private readonly IPublishedUrlProvider _urlProvider; |
| 28 | + private readonly IAlgoliaSearchPropertyIndexValueFactory _algoliaSearchPropertyIndexValueFactory; |
| 29 | + private readonly IRecordBuilderFactory _recordBuilderFactory; |
| 30 | + |
| 31 | + public BuildIndexController( |
| 32 | + IAlgoliaIndexDefinitionStorage<AlgoliaIndex> indexStorage, |
| 33 | + IUmbracoContextFactory umbracoContextFactory, |
| 34 | + IContentService contentService, |
| 35 | + ILogger<BuildIndexController> logger, |
| 36 | + IAlgoliaIndexService indexService, |
| 37 | + IUserService userService, |
| 38 | + IPublishedUrlProvider urlProvider, |
| 39 | + IAlgoliaSearchPropertyIndexValueFactory algoliaSearchPropertyIndexValueFactory, |
| 40 | + IRecordBuilderFactory recordBuilderFactory) |
21 | 41 | {
|
22 |
| - public BuildIndexController( |
23 |
| - IAlgoliaIndexService indexService, |
24 |
| - IAlgoliaSearchService<SearchResponse<Record>> searchService, |
25 |
| - IAlgoliaIndexDefinitionStorage<AlgoliaIndex> indexStorage, |
26 |
| - IUserService userService, IPublishedUrlProvider urlProvider, |
27 |
| - IContentService contentService, |
28 |
| - IAlgoliaSearchPropertyIndexValueFactory algoliaSearchPropertyIndexValueFactory, |
29 |
| - IUmbracoContextFactory umbracoContextFactory, |
30 |
| - ILogger<BuildIndexController> logger, |
31 |
| - IRecordBuilderFactory recordBuilderFactory) |
32 |
| - : base(indexService, |
33 |
| - searchService, |
34 |
| - indexStorage, |
35 |
| - userService, |
36 |
| - urlProvider, |
37 |
| - contentService, |
38 |
| - algoliaSearchPropertyIndexValueFactory, |
39 |
| - umbracoContextFactory, |
40 |
| - logger, |
41 |
| - recordBuilderFactory) |
| 42 | + _indexStorage = indexStorage; |
| 43 | + _umbracoContextFactory = umbracoContextFactory; |
| 44 | + _contentService = contentService; |
| 45 | + _logger = logger; |
| 46 | + _indexService = indexService; |
| 47 | + _userService = userService; |
| 48 | + _urlProvider = urlProvider; |
| 49 | + _algoliaSearchPropertyIndexValueFactory = algoliaSearchPropertyIndexValueFactory; |
| 50 | + _recordBuilderFactory = recordBuilderFactory; |
| 51 | + } |
| 52 | + |
| 53 | + [HttpPost("index/build")] |
| 54 | + [ProducesResponseType(typeof(Result), StatusCodes.Status200OK)] |
| 55 | + public async Task<IActionResult> BuildIndex([FromBody] IndexConfiguration indexConfiguration) |
| 56 | + { |
| 57 | + AlgoliaIndex index = _indexStorage.GetById(indexConfiguration.Id); |
| 58 | + List<Record> payload = []; |
| 59 | + |
| 60 | + IEnumerable<ContentTypeDto>? indexContentData = JsonSerializer.Deserialize<IEnumerable<ContentTypeDto>>(index.SerializedData); |
| 61 | + if (indexContentData is null) |
42 | 62 | {
|
| 63 | + // TODO => handle null result |
| 64 | + return BadRequest(); |
43 | 65 | }
|
44 | 66 |
|
45 |
| - [HttpPost("index/build")] |
46 |
| - [ProducesResponseType(typeof(Result), StatusCodes.Status200OK)] |
47 |
| - public async Task<IActionResult> BuildIndex([FromBody] IndexConfiguration indexConfiguration) |
| 67 | + foreach (var contentDataItem in indexContentData) |
48 | 68 | {
|
49 |
| - var index = IndexStorage.GetById(indexConfiguration.Id); |
50 |
| - |
51 |
| - var payload = new List<Record>(); |
| 69 | + using var ctx = _umbracoContextFactory.EnsureUmbracoContext(); |
| 70 | + var contentType = ctx.UmbracoContext.Content?.GetContentType(contentDataItem.Alias); |
52 | 71 |
|
53 |
| - var indexContentData = JsonSerializer.Deserialize<IEnumerable<ContentTypeDto>>(index.SerializedData); |
54 |
| - |
55 |
| - foreach (var contentDataItem in indexContentData) |
| 72 | + if (contentType is null) |
56 | 73 | {
|
57 |
| - using var ctx = UmbracoContextFactory.EnsureUmbracoContext(); |
58 |
| - var contentType = ctx.UmbracoContext.Content.GetContentType(contentDataItem.Alias); |
59 |
| - var contentItems = ContentService.GetPagedOfType(contentType.Id, 0, int.MaxValue, out _, null); |
| 74 | + // TODO => handle null content type |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + // use GetPagedOfTypes as filter is nullable here, but not on GetPagedOfType |
| 79 | + var contentItems = _contentService.GetPagedOfTypes([contentType.Id], 0, int.MaxValue, out _, null); |
60 | 80 |
|
61 |
| - Logger.LogInformation("Building index for {ContentType} with {Count} items", contentDataItem.Alias, contentItems.Count()); |
| 81 | + _logger.LogInformation("Building index for {ContentType} with {Count} items", contentDataItem.Alias, contentItems.Count()); |
62 | 82 |
|
63 |
| - foreach (var contentItem in contentItems.Where(p => !p.Trashed)) |
64 |
| - { |
65 |
| - var record = new ContentRecordBuilder(UserService, UrlProvider, AlgoliaSearchPropertyIndexValueFactory, RecordBuilderFactory, UmbracoContextFactory) |
66 |
| - .BuildFromContent(contentItem, (p) => contentDataItem.Properties.Any(q => q.Alias == p.Alias)) |
67 |
| - .Build(); |
| 83 | + foreach (var contentItem in contentItems.Where(p => !p.Trashed)) |
| 84 | + { |
| 85 | + var record = new ContentRecordBuilder(_userService, _urlProvider, _algoliaSearchPropertyIndexValueFactory, _recordBuilderFactory, _umbracoContextFactory) |
| 86 | + .BuildFromContent(contentItem, (p) => contentDataItem.Properties.Any(q => q.Alias == p.Alias)) |
| 87 | + .Build(); |
68 | 88 |
|
69 |
| - payload.Add(record); |
70 |
| - } |
| 89 | + payload.Add(record); |
71 | 90 | }
|
| 91 | + } |
72 | 92 |
|
73 |
| - var result = await IndexService.PushData(index.Name, payload); |
| 93 | + var result = await _indexService.PushData(index.Name, payload); |
74 | 94 |
|
75 |
| - return Ok(result); |
76 |
| - } |
| 95 | + return Ok(result); |
77 | 96 | }
|
78 | 97 | }
|
0 commit comments