Domain Event Handlers refactored with some missing cases
This commit is contained in:
		
							parent
							
								
									3ed136b00f
								
							
						
					
					
						commit
						aee1ac6a06
					
				@ -7,7 +7,8 @@ using System.Threading.Tasks;
 | 
			
		||||
 | 
			
		||||
namespace Ordering.API.Application.DomainEventHandlers.BuyerAndPaymentMethodVerified
 | 
			
		||||
{
 | 
			
		||||
    public class UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler : IAsyncNotificationHandler<BuyerAndPaymentMethodVerifiedDomainEvent>
 | 
			
		||||
    public class UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler 
 | 
			
		||||
                   : IAsyncNotificationHandler<BuyerAndPaymentMethodVerifiedDomainEvent>
 | 
			
		||||
    {
 | 
			
		||||
        private readonly IOrderRepository<Order> _orderRepository;
 | 
			
		||||
        private readonly ILoggerFactory _logger;
 | 
			
		||||
 | 
			
		||||
@ -3,10 +3,22 @@ using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
 | 
			
		||||
using MediatR;
 | 
			
		||||
using Ordering.Domain.Events;
 | 
			
		||||
 | 
			
		||||
namespace Ordering.API.Application.DomainEventHandlers.OrderStartedEvent
 | 
			
		||||
{
 | 
			
		||||
    public class SendEmailToCustomerWhenOrderStartedDomainEventHandler
 | 
			
		||||
    {
 | 
			
		||||
         //TO DO
 | 
			
		||||
                   //: IAsyncNotificationHandler<OrderStartedDomainEvent>
 | 
			
		||||
    { 
 | 
			
		||||
        public SendEmailToCustomerWhenOrderStartedDomainEventHandler()
 | 
			
		||||
        {
 | 
			
		||||
        
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        //public async Task Handle(OrderStartedDomainEvent orderNotification)
 | 
			
		||||
        //{
 | 
			
		||||
        //    //TBD - Send email logic
 | 
			
		||||
        //}
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -8,7 +8,8 @@ using System.Threading.Tasks;
 | 
			
		||||
 | 
			
		||||
namespace Ordering.API.Application.DomainEventHandlers.OrderStartedEvent
 | 
			
		||||
{
 | 
			
		||||
    public class ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler : IAsyncNotificationHandler<OrderStartedDomainEvent>
 | 
			
		||||
    public class ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler 
 | 
			
		||||
                        : IAsyncNotificationHandler<OrderStartedDomainEvent>
 | 
			
		||||
    {
 | 
			
		||||
        private readonly ILoggerFactory _logger;
 | 
			
		||||
        private readonly IBuyerRepository<Buyer> _buyerRepository;
 | 
			
		||||
@ -21,33 +22,34 @@ namespace Ordering.API.Application.DomainEventHandlers.OrderStartedEvent
 | 
			
		||||
            _logger = logger ?? throw new ArgumentNullException(nameof(logger));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task Handle(OrderStartedDomainEvent orderNotification)
 | 
			
		||||
        public async Task Handle(OrderStartedDomainEvent orderStartedEvent)
 | 
			
		||||
        {
 | 
			
		||||
            var cardTypeId = orderNotification.CardTypeId != 0 ? orderNotification.CardTypeId : 1;
 | 
			
		||||
            var cardTypeId = (orderStartedEvent.CardTypeId != 0) ? orderStartedEvent.CardTypeId : 1;
 | 
			
		||||
 | 
			
		||||
            var buyerGuid = _identityService.GetUserIdentity();
 | 
			
		||||
            var buyer = await _buyerRepository.FindAsync(buyerGuid);
 | 
			
		||||
            var userGuid = _identityService.GetUserIdentity();
 | 
			
		||||
 | 
			
		||||
            if (buyer == null)
 | 
			
		||||
            {
 | 
			
		||||
                buyer = new Buyer(buyerGuid);
 | 
			
		||||
            var buyer = await _buyerRepository.FindAsync(userGuid);
 | 
			
		||||
            bool buyerOriginallyExisted = (buyer == null) ? false : true;
 | 
			
		||||
 | 
			
		||||
            if (!buyerOriginallyExisted)
 | 
			
		||||
            {                
 | 
			
		||||
                buyer = new Buyer(userGuid);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            var paymentMethod = buyer.VerifyOrAddPaymentMethod(cardTypeId,
 | 
			
		||||
                $"Payment Method on {DateTime.UtcNow}",
 | 
			
		||||
                orderNotification.CardNumber,
 | 
			
		||||
                orderNotification.CardSecurityNumber,
 | 
			
		||||
                orderNotification.CardHolderName,
 | 
			
		||||
                orderNotification.CardExpiration,
 | 
			
		||||
                orderNotification.Order.Id);
 | 
			
		||||
            buyer.VerifyOrAddPaymentMethod(cardTypeId,
 | 
			
		||||
                                           $"Payment Method on {DateTime.UtcNow}",
 | 
			
		||||
                                           orderStartedEvent.CardNumber,
 | 
			
		||||
                                           orderStartedEvent.CardSecurityNumber,
 | 
			
		||||
                                           orderStartedEvent.CardHolderName,
 | 
			
		||||
                                           orderStartedEvent.CardExpiration,
 | 
			
		||||
                                           orderStartedEvent.Order.Id);
 | 
			
		||||
 | 
			
		||||
            _buyerRepository.Add(buyer);
 | 
			
		||||
            var buyerUpdated = buyerOriginallyExisted ? _buyerRepository.Update(buyer) : _buyerRepository.Add(buyer);
 | 
			
		||||
 | 
			
		||||
            await _buyerRepository.UnitOfWork
 | 
			
		||||
                .SaveEntitiesAsync();
 | 
			
		||||
 | 
			
		||||
            _logger.CreateLogger(nameof(ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler)).LogTrace($"A new payment method has been successfully added for orderId: {orderNotification.Order.Id}.");
 | 
			
		||||
 | 
			
		||||
            _logger.CreateLogger(nameof(ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler)).LogTrace($"Buyer {buyerUpdated.Id} and related payment method were validated or updated for orderId: {orderStartedEvent.Order.Id}.");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -16,13 +16,15 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Autof
 | 
			
		||||
        protected override void Load(ContainerBuilder builder)
 | 
			
		||||
        {
 | 
			
		||||
            builder.RegisterAssemblyTypes(typeof(IMediator).GetTypeInfo().Assembly)
 | 
			
		||||
                .AsImplementedInterfaces();            
 | 
			
		||||
                .AsImplementedInterfaces();
 | 
			
		||||
 | 
			
		||||
            // Register all the Command classes (they implement IAsyncRequestHandler) in assembly holding the Commands
 | 
			
		||||
            builder.RegisterAssemblyTypes(typeof(CreateOrderCommand).GetTypeInfo().Assembly)
 | 
			
		||||
                .As(o => o.GetInterfaces()
 | 
			
		||||
                    .Where(i => i.IsClosedTypeOf(typeof(IAsyncRequestHandler<,>)))
 | 
			
		||||
                    .Select(i => new KeyedService("IAsyncRequestHandler", i)));
 | 
			
		||||
            
 | 
			
		||||
 | 
			
		||||
            // Register all the Domain Event Handler classes (they implement IAsyncNotificationHandler<>) in assembly holding the Domain Events
 | 
			
		||||
            builder
 | 
			
		||||
                .RegisterAssemblyTypes(typeof(ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler).GetTypeInfo().Assembly)
 | 
			
		||||
                .Where(t => t.IsClosedTypeOf(typeof(IAsyncNotificationHandler<>)))
 | 
			
		||||
 | 
			
		||||
@ -9,7 +9,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.B
 | 
			
		||||
    public interface IBuyerRepository<T> : IRepository<T> where T : IAggregateRoot
 | 
			
		||||
    {
 | 
			
		||||
        Buyer Add(Buyer buyer);
 | 
			
		||||
 | 
			
		||||
        Buyer Update(Buyer buyer);
 | 
			
		||||
        Task<Buyer> FindAsync(string BuyerIdentityGuid);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -40,6 +40,13 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Repositor
 | 
			
		||||
            
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public Buyer Update(Buyer buyer)
 | 
			
		||||
        {
 | 
			
		||||
            return _context.Buyers
 | 
			
		||||
                    .Update(buyer)
 | 
			
		||||
                    .Entity;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task<Buyer> FindAsync(string identity)
 | 
			
		||||
        {
 | 
			
		||||
            var buyer = await _context.Buyers
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user