Skip to content

Commit aee1ac6

Browse files
Domain Event Handlers refactored with some missing cases
1 parent 3ed136b commit aee1ac6

6 files changed

Lines changed: 48 additions & 24 deletions

File tree

src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/BuyerAndPaymentMethodVerified/UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
namespace Ordering.API.Application.DomainEventHandlers.BuyerAndPaymentMethodVerified
99
{
10-
public class UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler : IAsyncNotificationHandler<BuyerAndPaymentMethodVerifiedDomainEvent>
10+
public class UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler
11+
: IAsyncNotificationHandler<BuyerAndPaymentMethodVerifiedDomainEvent>
1112
{
1213
private readonly IOrderRepository<Order> _orderRepository;
1314
private readonly ILoggerFactory _logger;

src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStartedEvent/SendEmailToCustomerWhenOrderStartedDomainEventHandler.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,22 @@
33
using System.Linq;
44
using System.Threading.Tasks;
55

6+
using MediatR;
7+
using Ordering.Domain.Events;
8+
69
namespace Ordering.API.Application.DomainEventHandlers.OrderStartedEvent
710
{
811
public class SendEmailToCustomerWhenOrderStartedDomainEventHandler
9-
{
10-
//TO DO
12+
//: IAsyncNotificationHandler<OrderStartedDomainEvent>
13+
{
14+
public SendEmailToCustomerWhenOrderStartedDomainEventHandler()
15+
{
16+
17+
}
18+
19+
//public async Task Handle(OrderStartedDomainEvent orderNotification)
20+
//{
21+
// //TBD - Send email logic
22+
//}
1123
}
1224
}

src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStartedEvent/ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler.cs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
namespace Ordering.API.Application.DomainEventHandlers.OrderStartedEvent
1010
{
11-
public class ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler : IAsyncNotificationHandler<OrderStartedDomainEvent>
11+
public class ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler
12+
: IAsyncNotificationHandler<OrderStartedDomainEvent>
1213
{
1314
private readonly ILoggerFactory _logger;
1415
private readonly IBuyerRepository<Buyer> _buyerRepository;
@@ -21,33 +22,34 @@ public ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler(ILoggerFact
2122
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
2223
}
2324

24-
public async Task Handle(OrderStartedDomainEvent orderNotification)
25+
public async Task Handle(OrderStartedDomainEvent orderStartedEvent)
2526
{
26-
var cardTypeId = orderNotification.CardTypeId != 0 ? orderNotification.CardTypeId : 1;
27+
var cardTypeId = (orderStartedEvent.CardTypeId != 0) ? orderStartedEvent.CardTypeId : 1;
2728

28-
var buyerGuid = _identityService.GetUserIdentity();
29-
var buyer = await _buyerRepository.FindAsync(buyerGuid);
29+
var userGuid = _identityService.GetUserIdentity();
3030

31-
if (buyer == null)
32-
{
33-
buyer = new Buyer(buyerGuid);
31+
var buyer = await _buyerRepository.FindAsync(userGuid);
32+
bool buyerOriginallyExisted = (buyer == null) ? false : true;
33+
34+
if (!buyerOriginallyExisted)
35+
{
36+
buyer = new Buyer(userGuid);
3437
}
3538

36-
var paymentMethod = buyer.VerifyOrAddPaymentMethod(cardTypeId,
37-
$"Payment Method on {DateTime.UtcNow}",
38-
orderNotification.CardNumber,
39-
orderNotification.CardSecurityNumber,
40-
orderNotification.CardHolderName,
41-
orderNotification.CardExpiration,
42-
orderNotification.Order.Id);
39+
buyer.VerifyOrAddPaymentMethod(cardTypeId,
40+
$"Payment Method on {DateTime.UtcNow}",
41+
orderStartedEvent.CardNumber,
42+
orderStartedEvent.CardSecurityNumber,
43+
orderStartedEvent.CardHolderName,
44+
orderStartedEvent.CardExpiration,
45+
orderStartedEvent.Order.Id);
4346

44-
_buyerRepository.Add(buyer);
47+
var buyerUpdated = buyerOriginallyExisted ? _buyerRepository.Update(buyer) : _buyerRepository.Add(buyer);
4548

4649
await _buyerRepository.UnitOfWork
4750
.SaveEntitiesAsync();
4851

49-
_logger.CreateLogger(nameof(ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler)).LogTrace($"A new payment method has been successfully added for orderId: {orderNotification.Order.Id}.");
50-
52+
_logger.CreateLogger(nameof(ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler)).LogTrace($"Buyer {buyerUpdated.Id} and related payment method were validated or updated for orderId: {orderStartedEvent.Order.Id}.");
5153
}
5254
}
5355
}

src/Services/Ordering/Ordering.API/Infrastructure/AutofacModules/MediatorModule.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ public class MediatorModule : Autofac.Module
1616
protected override void Load(ContainerBuilder builder)
1717
{
1818
builder.RegisterAssemblyTypes(typeof(IMediator).GetTypeInfo().Assembly)
19-
.AsImplementedInterfaces();
19+
.AsImplementedInterfaces();
2020

21+
// Register all the Command classes (they implement IAsyncRequestHandler) in assembly holding the Commands
2122
builder.RegisterAssemblyTypes(typeof(CreateOrderCommand).GetTypeInfo().Assembly)
2223
.As(o => o.GetInterfaces()
2324
.Where(i => i.IsClosedTypeOf(typeof(IAsyncRequestHandler<,>)))
2425
.Select(i => new KeyedService("IAsyncRequestHandler", i)));
25-
26+
27+
// Register all the Domain Event Handler classes (they implement IAsyncNotificationHandler<>) in assembly holding the Domain Events
2628
builder
2729
.RegisterAssemblyTypes(typeof(ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler).GetTypeInfo().Assembly)
2830
.Where(t => t.IsClosedTypeOf(typeof(IAsyncNotificationHandler<>)))

src/Services/Ordering/Ordering.Domain/AggregatesModel/BuyerAggregate/IBuyerRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.B
99
public interface IBuyerRepository<T> : IRepository<T> where T : IAggregateRoot
1010
{
1111
Buyer Add(Buyer buyer);
12-
12+
Buyer Update(Buyer buyer);
1313
Task<Buyer> FindAsync(string BuyerIdentityGuid);
1414
}
1515
}

src/Services/Ordering/Ordering.Infrastructure/Repositories/BuyerRepository.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ public Buyer Add(Buyer buyer)
4040

4141
}
4242

43+
public Buyer Update(Buyer buyer)
44+
{
45+
return _context.Buyers
46+
.Update(buyer)
47+
.Entity;
48+
}
49+
4350
public async Task<Buyer> FindAsync(string identity)
4451
{
4552
var buyer = await _context.Buyers

0 commit comments

Comments
 (0)