Skip to content

Commit

Permalink
Merge pull request #62 from Informatievlaanderen/feat/or-1244_small_r…
Browse files Browse the repository at this point in the history
…efactor

feat: or-1244 rename repo.save -> repo.index + async
  • Loading branch information
W0dan authored Oct 19, 2022
2 parents 4ec880c + bda4c98 commit 32578d0
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace AssociationRegistry.Public.Api.Projections;

using System.Linq;
using System.Threading.Tasks;
using Events;
using SearchVerenigingen;

Expand All @@ -15,8 +16,8 @@ public ElasticEventHandler(IElasticRepository elasticRepository, IVerenigingBrol
_brolFeeder = brolFeeder;
}

public void HandleEvent(VerenigingWerdGeregistreerd message)
=> _elasticRepository.Save(
public async Task HandleEvent(VerenigingWerdGeregistreerd message)
=> await _elasticRepository.IndexAsync(
new VerenigingDocument(
message.VCode,
message.Naam,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace AssociationRegistry.Public.Api.Projections;

using System.Threading.Tasks;
using Nest;

public class ElasticRepository : IElasticRepository
Expand All @@ -11,7 +12,7 @@ public ElasticRepository(IElasticClient elasticClient)
_elasticClient = elasticClient;
}

public void Save<TDocument>(TDocument document)
public void Index<TDocument>(TDocument document)
where TDocument : class
{
var response = _elasticClient.IndexDocument(document);
Expand All @@ -22,4 +23,16 @@ public void Save<TDocument>(TDocument document)
throw new IndexDocumentFailed(response.DebugInformation);
}
}

public async Task IndexAsync<TDocument>(TDocument document)
where TDocument : class
{
var response = await _elasticClient.IndexDocumentAsync(document);

if (!response.IsValid)
{
// todo: log ? (should never happen in test/staging/production)
throw new IndexDocumentFailed(response.DebugInformation);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
namespace AssociationRegistry.Public.Api.Projections;

using System.Threading.Tasks;

public interface IElasticRepository
{
void Save<TDocument>(TDocument document)
void Index<TDocument>(TDocument document)
where TDocument : class;

Task IndexAsync<TDocument>(TDocument document)
where TDocument : class;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ public MartenEventsConsumer(ElasticEventHandler eventHandler)
_eventHandler = eventHandler;
}

public Task ConsumeAsync(IReadOnlyList<StreamAction> streamActions)
public async Task ConsumeAsync(IReadOnlyList<StreamAction> streamActions)
{
foreach (var @event in streamActions.SelectMany(streamAction => streamAction.Events))
if (@event.EventType == typeof(VerenigingWerdGeregistreerd))
_eventHandler.HandleEvent((VerenigingWerdGeregistreerd)@event.Data);

return Task.CompletedTask;
await _eventHandler.HandleEvent((VerenigingWerdGeregistreerd)@event.Data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void Then_it_does_not_throw_an_exception()
var fixture = new Fixture();

_classFixture.ElasticRepository
.Save(
.Index(
new VerenigingDocument(
fixture.Create<string>(),
fixture.Create<string>(),
Expand Down

0 comments on commit 32578d0

Please sign in to comment.