Skip to content

Commit c2bb182

Browse files
authored
831 step deletion integration events refactor (#989)
Instead of having the journey database repository drive event generation, we emit the events through the command handling.
1 parent a46dafb commit c2bb182

File tree

17 files changed

+304
-52
lines changed

17 files changed

+304
-52
lines changed
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
using System.Threading.Tasks;
1+
using System.Linq;
22
using Equinor.ProCoSys.Preservation.Command.Events;
33
using Equinor.ProCoSys.Preservation.Domain.AggregateModels.JourneyAggregate;
44

55
namespace Equinor.ProCoSys.Preservation.Command.EventHandlers.IntegrationEvents.EventHelpers;
66

7-
public class CreateJourneyDeletedEventHelper : ICreateEventHelper<Journey, JourneyDeleteEvent>
7+
public static class CreateJourneyDeletedEventHelper
88
{
9-
public Task<JourneyDeleteEvent> CreateEvent(Journey entity) => Task.FromResult(new JourneyDeleteEvent(entity.Guid, entity.Plant));
9+
public static JourneyDeletedEvents CreateEvent(Journey entity)
10+
{
11+
var journeyDeletedEvent = new JourneyDeleteEvent(entity.Guid, entity.Plant);
12+
var stepDeletedEvents = entity.Steps.Select(CreateStepDeletedEventHelper.CreateEvent).ToList();
13+
14+
return new JourneyDeletedEvents(journeyDeletedEvent, stepDeletedEvents);
15+
}
1016
}

src/Equinor.ProCoSys.Preservation.Command/EventHandlers/IntegrationEvents/EventHelpers/CreateStepDeleteEventHelper.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Equinor.ProCoSys.Preservation.Command.Events;
2+
using Equinor.ProCoSys.Preservation.Domain.AggregateModels.JourneyAggregate;
3+
4+
namespace Equinor.ProCoSys.Preservation.Command.EventHandlers.IntegrationEvents.EventHelpers;
5+
6+
public static class CreateStepDeletedEventHelper
7+
{
8+
public static StepDeleteEvent CreateEvent(Step entity) => new(entity.Guid, entity.Plant);
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Collections.Generic;
2+
using Equinor.ProCoSys.Preservation.Command.Events;
3+
4+
namespace Equinor.ProCoSys.Preservation.Command.EventHandlers.IntegrationEvents.EventHelpers;
5+
6+
public record JourneyDeletedEvents(
7+
JourneyDeleteEvent JourneyDeleteEvent,
8+
IEnumerable<StepDeleteEvent> StepDeleteEvents);
9+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
using Equinor.ProCoSys.Preservation.Command.EventHandlers.IntegrationEvents.EventHelpers;
4+
using Equinor.ProCoSys.Preservation.Command.EventPublishers;
5+
using Equinor.ProCoSys.Preservation.Domain.AggregateModels.JourneyAggregate;
6+
using Equinor.ProCoSys.Preservation.Domain.Events;
7+
using MediatR;
8+
9+
namespace Equinor.ProCoSys.Preservation.Command.EventHandlers.IntegrationEvents;
10+
11+
public class JourneyDeletedEventHandler(IIntegrationEventPublisher integrationEventPublisher)
12+
: INotificationHandler<DeletedEvent<Journey>>
13+
{
14+
public async Task Handle(DeletedEvent<Journey> notification, CancellationToken cancellationToken)
15+
{
16+
var journeyDeleteEvents = CreateJourneyDeletedEventHelper.CreateEvent(notification.Entity);
17+
await integrationEventPublisher.PublishAsync(journeyDeleteEvents.JourneyDeleteEvent, cancellationToken);
18+
19+
foreach (var stepDeleteEvent in journeyDeleteEvents.StepDeleteEvents)
20+
{
21+
await integrationEventPublisher.PublishAsync(stepDeleteEvent, cancellationToken);
22+
}
23+
}
24+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
using Equinor.ProCoSys.Preservation.Command.EventHandlers.IntegrationEvents.EventHelpers;
4+
using Equinor.ProCoSys.Preservation.Command.EventPublishers;
5+
using Equinor.ProCoSys.Preservation.Domain.AggregateModels.JourneyAggregate;
6+
using Equinor.ProCoSys.Preservation.Domain.Events;
7+
using MediatR;
8+
9+
namespace Equinor.ProCoSys.Preservation.Command.EventHandlers.IntegrationEvents;
10+
11+
public class StepDeletedEventHandler(IIntegrationEventPublisher integrationEventPublisher)
12+
: INotificationHandler<DeletedEvent<Step>>
13+
{
14+
public async Task Handle(DeletedEvent<Step> notification, CancellationToken cancellationToken)
15+
{
16+
var deleteEvent = CreateStepDeletedEventHelper.CreateEvent(notification.Entity);
17+
await integrationEventPublisher.PublishAsync(deleteEvent, cancellationToken);
18+
}
19+
}

src/Equinor.ProCoSys.Preservation.Command/JourneyCommands/DeleteJourney/DeleteJourneyCommandHandler.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Threading.Tasks;
33
using Equinor.ProCoSys.Preservation.Domain;
44
using Equinor.ProCoSys.Preservation.Domain.AggregateModels.JourneyAggregate;
5+
using Equinor.ProCoSys.Preservation.Domain.Events;
56
using MediatR;
67
using ServiceResult;
78

@@ -25,6 +26,8 @@ public async Task<Result<Unit>> Handle(DeleteJourneyCommand request, Cancellatio
2526
journey.SetRowVersion(request.RowVersion);
2627
_journeyRepository.Remove(journey);
2728

29+
journey.AddDomainEvent(new DeletedEvent<Journey>(journey));
30+
2831
await _unitOfWork.SaveChangesAsync(cancellationToken);
2932
return new SuccessResult<Unit>(Unit.Value);
3033
}

src/Equinor.ProCoSys.Preservation.Domain/AggregateModels/JourneyAggregate/Journey.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public void RemoveStep(Step step)
8282
}
8383

8484
_steps.Remove(step);
85-
step.SetRemoved();
85+
AddDomainEvent(new DeletedEvent<Step>(step));
8686
}
8787

8888
public void SwapSteps(int stepId1, int stepId2)
@@ -202,7 +202,5 @@ public void SetModified(Person modifiedBy)
202202

203203
AddDomainEvent(new ModifiedEvent<Journey>(this));
204204
}
205-
206-
public void SetRemoved() => AddDomainEvent(new DeletedEvent<Journey>(this));
207205
}
208206
}

src/Equinor.ProCoSys.Preservation.Domain/AggregateModels/JourneyAggregate/Step.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,5 @@ public void SetResponsible(Responsible responsible)
120120

121121
AddDomainEvent(new ModifiedEvent<Step>(this));
122122
}
123-
124-
public void SetRemoved() => AddDomainEvent(new DeletedEvent<Step>(this));
125123
}
126124
}

src/Equinor.ProCoSys.Preservation.Infrastructure/Repositories/JourneyRepository.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,15 @@ public Task<List<Journey>> GetJourneysWithAutoTransferStepsAsync(AutoTransferMet
2929
.Where(journey => journey.Steps.Any(s => s.AutoTransferMethod == autoTransferMethod))
3030
.ToListAsync();
3131

32-
public void RemoveStep(Step step)
33-
{
34-
_context.Steps.Remove(step);
35-
step.SetRemoved();
36-
}
32+
public void RemoveStep(Step step) => _context.Steps.Remove(step);
3733

3834
public override void Remove(Journey journey)
3935
{
4036
foreach (var step in journey.Steps)
4137
{
4238
_context.Steps.Remove(step);
43-
step.SetRemoved();
4439
}
4540
base.Remove(journey);
46-
journey.SetRemoved();
4741
}
4842
}
4943
}

0 commit comments

Comments
 (0)