Skip to content

Commit 7e9829b

Browse files
committed
Base content handler and Result object
1 parent 9372249 commit 7e9829b

File tree

10 files changed

+156
-164
lines changed

10 files changed

+156
-164
lines changed

src/Umbraco.Cms.Integrations.Search.Algolia/App_Plugins/UmbracoCms.Integrations/Search/Algolia/js/dashboard.controller.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132
umbracoCmsIntegrationsSearchAlgoliaResource
133133
.saveIndex(vm.manageIndex.name, vm.manageIndex.contentData)
134134
.then(function (response) {
135-
if (response.length == 0) {
135+
if (response.success) {
136136
vm.manageIndex.reset();
137137
algoliaService.getContentTypes((response) => vm.manageIndex.contentTypes = response);
138138
} else {
@@ -173,8 +173,8 @@
173173
vm.loading = true;
174174

175175
umbracoCmsIntegrationsSearchAlgoliaResource.buildIndex(model.index.id).then(function (response) {
176-
if (response.length > 0)
177-
notificationsService.warning("An error has occurred while building the index: " + response);
176+
if (response.failure)
177+
notificationsService.warning("An error has occurred while building the index: " + response.error);
178178
else {
179179
vm.loading = false;
180180
overlayService.close();

src/Umbraco.Cms.Integrations.Search.Algolia/Controllers/SearchController.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public IActionResult GetIndices()
5252
}
5353

5454
[HttpPost]
55-
public string SaveIndex([FromBody] IndexConfiguration index)
55+
public async Task<IActionResult> SaveIndex([FromBody] IndexConfiguration index)
5656
{
5757
try
5858
{
@@ -64,16 +64,18 @@ public string SaveIndex([FromBody] IndexConfiguration index)
6464
Date = DateTime.Now
6565
});
6666

67-
return _indexService.PushData(index.Name);
67+
var result = await _indexService.PushData(index.Name);
68+
69+
return new JsonResult(result);
6870
}
6971
catch(Exception ex)
7072
{
71-
return ex.Message;
73+
return new JsonResult(Result.Fail(ex.Message));
7274
}
7375
}
7476

7577
[HttpPost]
76-
public string BuildIndex([FromBody] IndexConfiguration indexConfiguration)
78+
public async Task<IActionResult> BuildIndex([FromBody] IndexConfiguration indexConfiguration)
7779
{
7880
try
7981
{
@@ -97,26 +99,28 @@ public string BuildIndex([FromBody] IndexConfiguration indexConfiguration)
9799
}
98100
}
99101

100-
return _indexService.PushData(index.Name, payload);
102+
var result = await _indexService.PushData(index.Name, payload);
103+
104+
return new JsonResult(result);
101105
}
102106
catch (Exception ex)
103107
{
104-
return ex.Message;
108+
return new JsonResult(Result.Fail(ex.Message));
105109
}
106110
}
107111

108112
[HttpDelete]
109-
public string DeleteIndex(int id)
113+
public IActionResult DeleteIndex(int id)
110114
{
111115
try
112116
{
113117
_indexStorage.Delete(id);
114118

115-
return string.Empty;
119+
return new JsonResult(Result.Ok());
116120
}
117121
catch(Exception ex)
118122
{
119-
return ex.Message;
123+
return new JsonResult(Result.Fail(ex.Message));
120124
}
121125
}
122126

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using Microsoft.Extensions.Logging;
2+
using System.Text.Json;
3+
using Umbraco.Cms.Core.Models;
4+
using Umbraco.Cms.Core.Notifications;
5+
using Umbraco.Cms.Integrations.Search.Algolia.Builders;
6+
using Umbraco.Cms.Integrations.Search.Algolia.Migrations;
7+
using Umbraco.Cms.Integrations.Search.Algolia.Models;
8+
using Umbraco.Cms.Integrations.Search.Algolia.Services;
9+
10+
namespace Umbraco.Cms.Integrations.Search.Algolia.Handlers
11+
{
12+
public abstract class BaseContentHandler
13+
{
14+
protected readonly ILogger Logger;
15+
16+
protected readonly IAlgoliaIndexDefinitionStorage<AlgoliaIndex> IndexStorage;
17+
18+
protected readonly IAlgoliaIndexService IndexService;
19+
20+
public BaseContentHandler(ILogger<BaseContentHandler> logger,
21+
IAlgoliaIndexDefinitionStorage<AlgoliaIndex> indexStorage,
22+
IAlgoliaIndexService indexService)
23+
{
24+
Logger = logger;
25+
26+
IndexStorage = indexStorage;
27+
28+
IndexService = indexService;
29+
}
30+
31+
public async Task RebuildIndex(IEnumerable<IContent> entities, bool deleteIndexData = false)
32+
{
33+
try
34+
{
35+
var indices = IndexStorage.Get();
36+
37+
foreach (var entity in entities)
38+
{
39+
foreach (var index in indices)
40+
{
41+
var indexConfiguration = JsonSerializer.Deserialize<List<ContentData>>(index.SerializedData)
42+
.FirstOrDefault(p => p.ContentType == entity.ContentType.Alias);
43+
if (indexConfiguration == null || indexConfiguration.ContentType != entity.ContentType.Alias) continue;
44+
45+
var record = new RecordBuilder()
46+
.BuildFromContent(entity, (p) => indexConfiguration.Properties.Any(q => q == p.Alias))
47+
.Build();
48+
49+
var result = deleteIndexData
50+
? await IndexService.DeleteData(index.Name, entity.Key.ToString())
51+
: await IndexService.UpdateData(index.Name, record);
52+
53+
if (result.Failure)
54+
Logger.LogError($"Failed to delete data for Algolia index: {result}");
55+
}
56+
}
57+
}
58+
catch (Exception ex)
59+
{
60+
Logger.LogError($"Failed to delete data for Algolia index: {ex.Message}");
61+
}
62+
}
63+
}
64+
}
Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,19 @@
11
using Microsoft.Extensions.Logging;
22

3-
using System.Text.Json;
4-
53
using Umbraco.Cms.Core.Events;
64
using Umbraco.Cms.Core.Notifications;
75
using Umbraco.Cms.Integrations.Search.Algolia.Migrations;
8-
using Umbraco.Cms.Integrations.Search.Algolia.Models;
96
using Umbraco.Cms.Integrations.Search.Algolia.Services;
107

118
namespace Umbraco.Cms.Integrations.Search.Algolia.Handlers
129
{
13-
public class ContentDeletedHandler : INotificationAsyncHandler<ContentDeletedNotification>
10+
public class ContentDeletedHandler : BaseContentHandler, INotificationAsyncHandler<ContentDeletedNotification>
1411
{
15-
private readonly ILogger<ContentDeletedNotification> _logger;
16-
17-
private readonly IAlgoliaIndexDefinitionStorage<AlgoliaIndex> _scopeService;
18-
19-
private readonly IAlgoliaIndexService _indexService;
20-
21-
public ContentDeletedHandler(ILogger<ContentDeletedNotification> logger, IAlgoliaIndexDefinitionStorage<AlgoliaIndex> scopeService, IAlgoliaIndexService indexService)
22-
{
23-
_logger = logger;
24-
25-
_scopeService = scopeService;
26-
27-
_indexService = indexService;
28-
}
29-
30-
public async Task HandleAsync(ContentDeletedNotification notification, CancellationToken cancellationToken)
31-
{
32-
try
33-
{
34-
var indices = _scopeService.Get();
35-
36-
foreach (var publishedItem in notification.DeletedEntities)
37-
{
38-
foreach (var index in indices)
39-
{
40-
var indexConfiguration = JsonSerializer.Deserialize<List<ContentData>>(index.SerializedData)
41-
.FirstOrDefault(p => p.ContentType == publishedItem.ContentType.Alias);
42-
if (indexConfiguration == null || indexConfiguration.ContentType != publishedItem.ContentType.Alias) continue;
43-
44-
var result = await _indexService.DeleteData(index.Name, publishedItem.Key.ToString());
12+
public ContentDeletedHandler(ILogger<ContentDeletedHandler> logger, IAlgoliaIndexDefinitionStorage<AlgoliaIndex> indexStorage, IAlgoliaIndexService indexService)
13+
: base(logger, indexStorage, indexService)
14+
{ }
4515

46-
if (!string.IsNullOrEmpty(result))
47-
_logger.LogError($"Failed to delete data for Algolia index: {result}");
48-
}
49-
}
50-
}
51-
catch (Exception ex)
52-
{
53-
_logger.LogError($"Failed to delete data for Algolia index: {ex.Message}");
54-
}
55-
}
16+
public async Task HandleAsync(ContentDeletedNotification notification, CancellationToken cancellationToken) =>
17+
await RebuildIndex(notification.DeletedEntities, true);
5618
}
5719
}
Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,18 @@
11
using Microsoft.Extensions.Logging;
22

3-
using System.Text.Json;
4-
53
using Umbraco.Cms.Core.Events;
64
using Umbraco.Cms.Core.Notifications;
7-
using Umbraco.Cms.Integrations.Search.Algolia.Builders;
85
using Umbraco.Cms.Integrations.Search.Algolia.Migrations;
9-
using Umbraco.Cms.Integrations.Search.Algolia.Models;
106
using Umbraco.Cms.Integrations.Search.Algolia.Services;
117

128
namespace Umbraco.Cms.Integrations.Search.Algolia.Handlers
139
{
14-
public class ContentPublishedHandler : INotificationAsyncHandler<ContentPublishedNotification>
10+
public class ContentPublishedHandler : BaseContentHandler, INotificationAsyncHandler<ContentPublishedNotification>
1511
{
16-
private readonly ILogger<ContentPublishedHandler> _logger;
17-
18-
private readonly IAlgoliaIndexDefinitionStorage<AlgoliaIndex> _indexStorage;
19-
20-
private readonly IAlgoliaIndexService _indexService;
21-
22-
public ContentPublishedHandler(ILogger<ContentPublishedHandler> logger,
23-
IAlgoliaIndexDefinitionStorage<AlgoliaIndex> indexStorage, IAlgoliaIndexService algoliaIndexService)
24-
{
25-
_logger = logger;
26-
27-
_indexStorage = indexStorage;
28-
29-
_indexService = algoliaIndexService;
30-
}
31-
32-
public async Task HandleAsync(ContentPublishedNotification notification, CancellationToken cancellationToken)
33-
{
34-
try
35-
{
36-
var indices = _indexStorage.Get();
37-
foreach (var publishedItem in notification.PublishedEntities)
38-
{
39-
foreach (var index in indices)
40-
{
41-
var indexConfiguration = JsonSerializer.Deserialize<List<ContentData>>(index.SerializedData)
42-
.FirstOrDefault(p => p.ContentType == publishedItem.ContentType.Alias);
43-
if (indexConfiguration == null || indexConfiguration.ContentType != publishedItem.ContentType.Alias) continue;
44-
45-
var record = new RecordBuilder()
46-
.BuildFromContent(publishedItem, (p) => indexConfiguration.Properties.Any(q => q == p.Alias))
47-
.Build();
48-
49-
var result = await _indexService.UpdateData(index.Name, record);
50-
51-
if (!string.IsNullOrEmpty(result))
52-
_logger.LogError($"Failed to update data for Algolia index: {result}");
53-
}
12+
public ContentPublishedHandler(ILogger<ContentPublishedHandler> logger, IAlgoliaIndexDefinitionStorage<AlgoliaIndex> indexStorage, IAlgoliaIndexService indexService)
13+
: base(logger, indexStorage, indexService)
14+
{ }
5415

55-
}
56-
}
57-
catch(Exception ex)
58-
{
59-
_logger.LogError($"Failed to update data for Algolia index: {ex.Message}");
60-
}
61-
}
16+
public async Task HandleAsync(ContentPublishedNotification notification, CancellationToken cancellationToken) => await RebuildIndex(notification.PublishedEntities);
6217
}
6318
}

src/Umbraco.Cms.Integrations.Search.Algolia/Handlers/ContentUnpublishedHandler.cs

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,13 @@
1010

1111
namespace Umbraco.Cms.Integrations.Search.Algolia.Handlers
1212
{
13-
public class ContentUnpublishedHandler : INotificationAsyncHandler<ContentUnpublishedNotification>
13+
public class ContentUnpublishedHandler : BaseContentHandler, INotificationAsyncHandler<ContentUnpublishedNotification>
1414
{
15-
private readonly ILogger<ContentUnpublishedNotification> _logger;
15+
public ContentUnpublishedHandler(ILogger<ContentUnpublishedHandler> logger, IAlgoliaIndexDefinitionStorage<AlgoliaIndex> indexStorage, IAlgoliaIndexService indexService)
16+
: base(logger, indexStorage, indexService)
17+
{ }
1618

17-
private readonly IAlgoliaIndexDefinitionStorage<AlgoliaIndex> _scopeService;
18-
19-
private readonly IAlgoliaIndexService _indexService;
20-
21-
public ContentUnpublishedHandler(ILogger<ContentUnpublishedNotification> logger, IAlgoliaIndexDefinitionStorage<AlgoliaIndex> scopeService, IAlgoliaIndexService indexService)
22-
{
23-
_logger = logger;
24-
25-
_scopeService = scopeService;
26-
27-
_indexService = indexService;
28-
}
29-
30-
public async Task HandleAsync(ContentUnpublishedNotification notification, CancellationToken cancellationToken)
31-
{
32-
try
33-
{
34-
var indices = _scopeService.Get();
35-
36-
foreach (var publishedItem in notification.UnpublishedEntities)
37-
{
38-
foreach (var index in indices)
39-
{
40-
var indexConfiguration = JsonSerializer.Deserialize<List<ContentData>>(index.SerializedData)
41-
.FirstOrDefault(p => p.ContentType == publishedItem.ContentType.Alias);
42-
if (indexConfiguration == null || indexConfiguration.ContentType != publishedItem.ContentType.Alias) continue;
43-
44-
var result = await _indexService.DeleteData(index.Name, publishedItem.Key.ToString());
45-
46-
if (!string.IsNullOrEmpty(result))
47-
_logger.LogError($"Failed to delete data for Algolia index: {result}");
48-
}
49-
}
50-
}
51-
catch (Exception ex)
52-
{
53-
_logger.LogError($"Failed to delete data for Algolia index: {ex.Message}");
54-
}
55-
}
19+
public async Task HandleAsync(ContentUnpublishedNotification notification, CancellationToken cancellationToken) =>
20+
await RebuildIndex(notification.UnpublishedEntities, true);
5621
}
5722
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+

2+
using System.Text.Json.Serialization;
3+
4+
namespace Umbraco.Cms.Integrations.Search.Algolia.Models
5+
{
6+
public class Result
7+
{
8+
public bool Success { get; set; }
9+
10+
public string Error { get; set; }
11+
12+
public bool Failure => !Success;
13+
14+
protected Result(bool success, string error)
15+
{
16+
if (success && !string.IsNullOrEmpty(error))
17+
{
18+
throw new ArgumentException("A succesful Result cannot have an error message.", error);
19+
}
20+
21+
if (!success && string.IsNullOrEmpty(error))
22+
{
23+
throw new ArgumentException("A failure Result must have an error message.", error);
24+
}
25+
26+
Success = success;
27+
Error = error;
28+
}
29+
30+
public static Result Ok() => new (true, string.Empty);
31+
32+
public static Result Fail(string message) => new (false, message);
33+
34+
}
35+
}

0 commit comments

Comments
 (0)