Skip to content

Commit 2b08b60

Browse files
committed
controllers manage own dependencies
1 parent c9e3b86 commit 2b08b60

File tree

9 files changed

+249
-422
lines changed

9 files changed

+249
-422
lines changed
Lines changed: 68 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Algolia.Search.Models.Search;
2-
using Asp.Versioning;
1+
using Asp.Versioning;
32
using Microsoft.AspNetCore.Http;
43
using Microsoft.AspNetCore.Mvc;
54
using Microsoft.Extensions.Logging;
@@ -13,66 +12,86 @@
1312
using Umbraco.Cms.Integrations.Search.Algolia.Models.ContentTypeDtos;
1413
using Umbraco.Cms.Integrations.Search.Algolia.Services;
1514

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
1720
{
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)
2141
{
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)
4262
{
63+
// TODO => handle null result
64+
return BadRequest();
4365
}
4466

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)
4868
{
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);
5271

53-
var indexContentData = JsonSerializer.Deserialize<IEnumerable<ContentTypeDto>>(index.SerializedData);
54-
55-
foreach (var contentDataItem in indexContentData)
72+
if (contentType is null)
5673
{
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);
6080

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());
6282

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();
6888

69-
payload.Add(record);
70-
}
89+
payload.Add(record);
7190
}
91+
}
7292

73-
var result = await IndexService.PushData(index.Name, payload);
93+
var result = await _indexService.PushData(index.Name, payload);
7494

75-
return Ok(result);
76-
}
95+
return Ok(result);
7796
}
7897
}
Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,37 @@
1-
using Algolia.Search.Models.Search;
2-
using Asp.Versioning;
1+
using Asp.Versioning;
32
using Microsoft.AspNetCore.Http;
43
using Microsoft.AspNetCore.Mvc;
5-
using Microsoft.Extensions.Logging;
6-
using Umbraco.Cms.Core.Routing;
7-
using Umbraco.Cms.Core.Services;
8-
using Umbraco.Cms.Core.Web;
9-
using Umbraco.Cms.Integrations.Search.Algolia.Builders;
104
using Umbraco.Cms.Integrations.Search.Algolia.Migrations;
115
using Umbraco.Cms.Integrations.Search.Algolia.Models;
126
using Umbraco.Cms.Integrations.Search.Algolia.Services;
137

14-
namespace Umbraco.Cms.Integrations.Search.Algolia.Api.Management.Controllers
8+
namespace Umbraco.Cms.Integrations.Search.Algolia.Api.Management.Controllers;
9+
10+
[ApiVersion("1.0")]
11+
[ApiExplorerSettings(GroupName = Constants.ManagementApi.GroupName)]
12+
public class DeleteIndexController : SearchControllerBase
1513
{
16-
[ApiVersion("1.0")]
17-
[ApiExplorerSettings(GroupName = Constants.ManagementApi.GroupName)]
18-
public class DeleteIndexController : SearchControllerBase
19-
{
20-
public DeleteIndexController(
21-
IAlgoliaIndexService indexService,
22-
IAlgoliaSearchService<SearchResponse<Record>> searchService,
23-
IAlgoliaIndexDefinitionStorage<AlgoliaIndex> indexStorage,
24-
IUserService userService, IPublishedUrlProvider urlProvider,
25-
IContentService contentService,
26-
IAlgoliaSearchPropertyIndexValueFactory algoliaSearchPropertyIndexValueFactory,
27-
IUmbracoContextFactory umbracoContextFactory,
28-
ILogger<DeleteIndexController> logger,
29-
IRecordBuilderFactory recordBuilderFactory)
30-
: base(indexService,
31-
searchService,
32-
indexStorage,
33-
userService,
34-
urlProvider,
35-
contentService,
36-
algoliaSearchPropertyIndexValueFactory,
37-
umbracoContextFactory,
38-
logger,
39-
recordBuilderFactory)
40-
{
41-
}
14+
private readonly IAlgoliaIndexService _indexService;
15+
private readonly IAlgoliaIndexDefinitionStorage<AlgoliaIndex> _indexStorage;
4216

43-
[HttpDelete("index/{id:int}")]
44-
[ProducesResponseType(typeof(Result), StatusCodes.Status200OK)]
45-
public async Task<IActionResult> DeleteIndex(int id)
46-
{
47-
var indexName = IndexStorage.GetById(id).Name;
17+
public DeleteIndexController(
18+
IAlgoliaIndexService indexService,
19+
IAlgoliaIndexDefinitionStorage<AlgoliaIndex> indexStorage)
20+
{
21+
_indexService = indexService;
22+
_indexStorage = indexStorage;
23+
}
4824

49-
IndexStorage.Delete(id);
25+
[HttpDelete("index/{id:int}")]
26+
[ProducesResponseType(typeof(Result), StatusCodes.Status200OK)]
27+
public async Task<IActionResult> DeleteIndex(int id)
28+
{
29+
var indexName = _indexStorage.GetById(id).Name;
5030

51-
await IndexService.DeleteIndex(indexName);
31+
_indexStorage.Delete(id);
5232

53-
return Ok(Result.Ok());
54-
}
33+
await _indexService.DeleteIndex(indexName);
5534

35+
return Ok(Result.Ok());
5636
}
5737
}

0 commit comments

Comments
 (0)