예제 #1
0
        public async Task Handle(OrderStartedDomainEvent orderStartedEvent)
        {
            var cardTypeId = (orderStartedEvent.CardTypeId != 0) ? orderStartedEvent.CardTypeId : 1;

            var userGuid = _identityService.GetUserIdentity();

            var buyer = await _buyerRepository.FindAsync(userGuid);

            bool buyerOriginallyExisted = (buyer == null) ? false : true;

            if (!buyerOriginallyExisted)
            {
                buyer = new Buyer(userGuid);
            }

            buyer.VerifyOrAddPaymentMethod(cardTypeId,
                                           $"Payment Method on {DateTime.UtcNow}",
                                           orderStartedEvent.CardNumber,
                                           orderStartedEvent.CardSecurityNumber,
                                           orderStartedEvent.CardHolderName,
                                           orderStartedEvent.CardExpiration,
                                           orderStartedEvent.Order.Id);

            var buyerUpdated = buyerOriginallyExisted ? _buyerRepository.Update(buyer) : _buyerRepository.Add(buyer);

            await _buyerRepository.UnitOfWork
            .SaveEntitiesAsync();

            _logger.CreateLogger(nameof(ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler)).LogTrace($"Buyer {buyerUpdated.Id} and related payment method were validated or updated for orderId: {orderStartedEvent.Order.Id}.");
        }
예제 #2
0
        public async Task Handle(OrderStartedDomainEvent notification)
        {
            var guid = _identityService.GetUserIdentity();

            var buyer = await _buyerRepository.FindAsync(guid);

            var buyerOriginallyExisted = (buyer != null);

            if (!buyerOriginallyExisted)
            {
                buyer = new Buyer(guid);
            }

            buyer.VerifyOrAddPaymentMethod(
                notification.CardNumber,
                notification.CardSecurityNumber,
                notification.CardHolder,
                notification.CardExpiration,
                notification.Order.Id);

            var buyerUpdated = buyerOriginallyExisted ? _buyerRepository.Update(buyer) : _buyerRepository.Add(buyer);

            await _buyerRepository.UnitOfWork.SaveEntitiesAsync();

            _logger.CreateLogger(typeof(ValidateOrAddBuyerOnOrderStartedEventHandler)).LogTrace($"Buyer {buyerUpdated.Id} and related payment method were validated or updated for orderId: {notification.Order.Id}.");
        }
        public async Task Handle(OrderStartedDomainEvent orderStartedEvent, CancellationToken cancellationToken)
        {
            var cardTypeId = (orderStartedEvent.CardTypeId != 0) ? orderStartedEvent.CardTypeId : 1;
            var buyer      = await _buyerRepository.FindAsync(orderStartedEvent.UserId);

            bool buyerOriginallyExisted = (buyer == null) ? false : true;

            if (!buyerOriginallyExisted)
            {
                buyer = new Buyer(orderStartedEvent.UserId, orderStartedEvent.UserName);
            }

            buyer.VerifyOrAddPaymentMethod(cardTypeId,
                                           $"Payment Method on {DateTime.UtcNow}",
                                           orderStartedEvent.CardNumber,
                                           orderStartedEvent.CardSecurityNumber,
                                           orderStartedEvent.CardHolderName,
                                           orderStartedEvent.CardExpiration,
                                           orderStartedEvent.Order.Id);

            var buyerUpdated = buyerOriginallyExisted ?
                               _buyerRepository.Update(buyer) :
                               _buyerRepository.Add(buyer);

            await _buyerRepository.UnitOfWork
            .SaveEntitiesAsync();

            var orderStatusChangedTosubmittedIntegrationEvent = new OrderStatusChangedToSubmittedIntegrationEvent(orderStartedEvent.Order.Id, orderStartedEvent.Order.OrderStatus.Name, buyer.Name);
            await _orderingIntegrationEventService.PublishThroughEventBusAsync(orderStatusChangedTosubmittedIntegrationEvent);

            _logger.CreateLogger(nameof(ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler)).LogTrace($"Buyer {buyerUpdated.Id} and related payment method were validated or updated for orderId: {orderStartedEvent.Order.Id}.");
        }
예제 #4
0
        public async Task Handle(OrderStartedDomainEvent notification, CancellationToken cancellationToken)
        {
            var cardTypeId = notification.CardTypeId != 0 ? notification.CardTypeId : 1;

            var buyer = await _buyerRepository.FindAsync(notification.UserId);

            bool buyerOriginallyExisted = buyer != null;

            if (!buyerOriginallyExisted)
            {
                buyer = new Buyer(notification.UserId, notification.UserName);
            }

            buyer.VerifyOrAddPaymentMethod(
                cardTypeId,
                $"Payment Method on {DateTime.UtcNow}",
                notification.CardNumber,
                notification.CardSecurityNumber,
                notification.CardHolderName,
                notification.CardExpiration,
                notification.Order.Id);

            var buyerUpdated = buyerOriginallyExisted ? _buyerRepository.Update(buyer) : _buyerRepository.Add(buyer);

            _ = await _buyerRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);

            Log(notification, buyerUpdated);
        }
        public async Task Handle(OrderStartedDomainEvent orderStartedEvent, CancellationToken cancellationToken)
        {
            var cardTypeId = (orderStartedEvent.CardTypeId != 0) ? orderStartedEvent.CardTypeId : 1;
            var buyer      = await _buyerRepository.FindAsync(orderStartedEvent.UserId);

            bool buyerOriginallyExisted = (buyer == null) ? false : true;

            if (!buyerOriginallyExisted)
            {
                buyer = new Buyer(orderStartedEvent.UserId, orderStartedEvent.UserName);
            }

            buyer.VerifyOrAddPaymentMethod(cardTypeId,
                                           $"Payment Method on {DateTime.UtcNow}",
                                           orderStartedEvent.CardNumber,
                                           orderStartedEvent.CardSecurityNumber,
                                           orderStartedEvent.CardHolderName,
                                           orderStartedEvent.CardExpiration,
                                           orderStartedEvent.Order.Id);

            var buyerUpdated = buyerOriginallyExisted ?
                               _buyerRepository.Update(buyer) :
                               _buyerRepository.Add(buyer);

            await _buyerRepository.UnitOfWork
            .SaveEntitiesAsync(cancellationToken);

            var orderStatusChangedTosubmittedIntegrationEvent = new OrderStatusChangedToSubmittedIntegrationEvent(orderStartedEvent.Order.Id, orderStartedEvent.Order.OrderStatus.Name, buyer.Name);
            await _messageOutbox.Send(orderStatusChangedTosubmittedIntegrationEvent);
        }
예제 #6
0
        public async Task Handle(OrderStartedDomainEvent @event, CancellationToken cancellationToken)
        {
            //Side effects of creating an order is to create a buyer (if it does not exist)
            //var buyer = new BuyerInfo(@event.UserName, @event.UserId, @event.Order.Id, doesBuyerExistService);
            //buyer=buyerRepository.Upsert(buyer);

            var buyer = await buyerRepository.FindAsync(@event.UserId);

            if (buyer == null)
            {
                logger.LogInformation("buyer didn't exist");
                buyer = new Buyer(@event.UserName, @event.UserId, @event.Order.Id);
                logger.LogInformation("buyer instantiated");

                buyer = buyerRepository.Add(buyer);
            }


            //Probably the wrong way
            buyer.AddDomainEvent(new BuyerCreatedDomainEvent(@event.Order.Id, buyer.Id, buyer.Name, buyer.IdentityGuid));
            logger.LogInformation("BuyerCreatedDomainEvent added to list of buyer events");

            await buyerRepository.UnitOfWork.SaveChangesAsync();

            logger.LogInformation("SaveChanges called on buyer");
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="orderStartedDomainEvent">订单已开始领域事件</param>
        /// <param name="cancellationToken">取消Token</param>
        /// <returns>任务</returns>
        public async Task Handle(OrderStartedDomainEvent orderStartedDomainEvent, CancellationToken cancellationToken)
        {
            var buyer = await _buyerRepository.FindAsync(orderStartedDomainEvent.UserId);

            var existBuyer = buyer != null;

            if (!existBuyer)
            {
                buyer = new Buyer(orderStartedDomainEvent.UserId, orderStartedDomainEvent.UserName);
            }
            buyer.VerifyOrAddPaymentMethod(orderStartedDomainEvent.CardTypeId,
                                           $"Payment Mehtod on {DateTime.Now}",
                                           orderStartedDomainEvent.CardNumber,
                                           orderStartedDomainEvent.CardSecurityNumber,
                                           orderStartedDomainEvent.CardHolderName,
                                           orderStartedDomainEvent.CardExpiration,
                                           orderStartedDomainEvent.Order.Id);
            _ = existBuyer ?
                _buyerRepository.Update(buyer) :
                _buyerRepository.Add(buyer);

            //分发领域事件
            await _buyerRepository.UnitOfWork
            .SaveEntitiesAsync();
        }
예제 #8
0
        public async Task <bool> False(string id)
        {
            var _buyer = await buyerRepository.FindAsync(id);

            if (_buyer == null)
            {
                return(true);
            }
            return(false);
        }
예제 #9
0
        public async Task <bool> Handle(CreateOrderCommand message)
        {
            // Add/Update the Buyer AggregateRoot
            // DDD patterns comment: Add child entities and value-objects through the Order Aggregate-Root
            // methods and constructor so validations, invariants and business logic
            // make sure that consistency is preserved across the whole aggregate

            var cardTypeId = message.CardTypeId != 0 ? message.CardTypeId : 1;

            var buyerGuid = _identityService.GetUserIdentity();
            var buyer     = await _buyerRepository.FindAsync(buyerGuid);

            if (buyer == null)
            {
                buyer = new Buyer(buyerGuid);
            }

            var payment = buyer.AddPaymentMethod(cardTypeId,
                                                 $"Payment Method on {DateTime.UtcNow}",
                                                 message.CardNumber,
                                                 message.CardSecurityNumber,
                                                 message.CardHolderName,
                                                 message.CardExpiration);

            _buyerRepository.Add(buyer);

            await _buyerRepository.UnitOfWork
            .SaveChangesAsync();

            // Create the Order AggregateRoot
            // DDD patterns comment: Add child entities and value-objects through the Order Aggregate-Root
            // methods and constructor so validations, invariants and business logic
            // make sure that consistency is preserved across the whole aggregate

            var order = new Order(buyer.Id, payment.Id, new Address(message.Street, message.City, message.State, message.Country, message.ZipCode));

            foreach (var item in message.OrderItems)
            {
                order.AddOrderItem(item.ProductId, item.ProductName, item.UnitPrice, item.Discount, item.PictureUrl, item.Units);
            }

            _orderRepository.Add(order);

            var result = await _orderRepository.UnitOfWork
                         .SaveChangesAsync();

            return(result > 0);
        }
        public async Task Handle(OrderStartedDomainEvent orderStartedEvent, CancellationToken cancellationToken)
        {
            var buyer = await _buyerRepository.FindAsync(orderStartedEvent.UserId);

            bool buyerOriginallyExisted = (buyer == null) ? false : true;

            if (!buyerOriginallyExisted)
            {
                buyer = new Buyer(orderStartedEvent.UserId, orderStartedEvent.UserName);
            }

            var buyerUpdated = buyerOriginallyExisted ?
                               _buyerRepository.Update(buyer) :
                               _buyerRepository.Add(buyer);

            await _buyerRepository.UnitOfWork
            .SaveEntitiesAsync(cancellationToken);
        }
예제 #11
0
        public async Task Handle(OrderStartedDomainEvent notification, CancellationToken cancellationToken)
        {
            var buyer = await _buyerRepository.FindAsync(notification.UserId);

            bool buyerOriginallyExisted = (buyer == null) ? false : true;

            if (!buyerOriginallyExisted)
            {
                buyer = new Buyer(notification.UserId, notification.UserName);
            }
            var buyerUpdated = buyerOriginallyExisted ?
                               _buyerRepository.UpdateBuyer(buyer) :
                               _buyerRepository.AddBuyer(buyer);

            await _buyerRepository.unitOfWork.SaveEntitiesAsync();

            _logger.CreateLogger(nameof(ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler));
        }
        public async Task Handle(OrderStartedDomainEvent orderStartedEvent, CancellationToken cancellationToken)
        {
            var cardTypeId = (orderStartedEvent.CardTypeId != 0) ? orderStartedEvent.CardTypeId : 1;
            var buyer      = await _buyerRepository.FindAsync(orderStartedEvent.UserId);

            bool buyerOriginallyExisted = (buyer == null) ? false : true;

            if (!buyerOriginallyExisted)
            {
                buyer = new Buyer(orderStartedEvent.UserId);
            }

            var buyerUpdated = buyerOriginallyExisted ? _buyerRepository.Update(buyer) : _buyerRepository.Add(buyer);

            await _buyerRepository.UnitOfWork
            .SaveEntitiesAsync();

            _logger.CreateLogger(nameof(ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler)).LogTrace($"Buyer {buyerUpdated.Id} and related payment method were validated or updated for orderId: {orderStartedEvent.Order.Id}.");
        }
        public async Task Handle(VerifyBuyerAndPaymentCommand message, IMessageHandlerContext context)
        {
            var buyer = await buyerRepository.FindAsync(message.UserId);

            var cardTypeId = (message.CardTypeId != 0) ? message.CardTypeId : 1;

            var buyerOriginallyExisted = (buyer != null);

            if (!buyerOriginallyExisted)
            {
                buyer = new Buyer(message.UserId);
            }

            buyer.VerifyOrAddPaymentMethod(cardTypeId,
                                           $"Payment Method on {DateTime.UtcNow}",
                                           message.CardNumber,
                                           message.CardSecurityNumber,
                                           message.CardHolderName,
                                           message.CardExpiration,
                                           message.OrderId);

            await buyerRepository.UnitOfWork.SaveEntitiesAsync();
        }
예제 #14
0
        public async Task Handle(OrderStartedDomainEvent notification, CancellationToken cancellationToken)
        {
            var cardTypeId = (notification.CardTypeId != 0) ? notification.CardTypeId : 1;
            var userGuid   = _identityService.GetUserIdentity();
            var buyer      = await _buyerRepository.FindAsync();

            bool buyerOriginallyExisted = (buyer == null) ? false : true;

            if (!buyerOriginallyExisted)
            {
                buyer = new Buyer(userGuid);
            }

            buyer.VerifyOrAddPaymentMethod(cardTypeId,
                                           $"Payment Method on {DateTime.UtcNow}",
                                           notification.CardNumber,
                                           notification.CardSecurityNumber,
                                           notification.CardHolderName,
                                           notification.CardExpiration,
                                           notification.Order.Id);

            var buyerUpdated = buyerOriginallyExisted ?
                               _buyerRepository.Update(buyer) :
                               _buyerRepository.Add(buyer);

            await _buyerRepository.UnitOfWork
            .SaveChangesAsync(cancellationToken);

            var orderStatusChangedToSubmittedIntegrationEvent = new OrderStatusChangedToSubmittedIntegrationEvent(
                notification.Order.Id, notification.Order.OrderStatus.Name, buyer.Name);

            await _orderIntegrationEventService.AddAndSaveEventAsync(orderStatusChangedToSubmittedIntegrationEvent);

            _logger.CreateLogger <ValidateOrAddBuyerAggregateWhenOrderStartedDomainEventHandler>()
            .LogTrace($"Buyer {buyerUpdated.Id} and related payment method were validated or updated " +
                      $"for order id {notification.Order.Id}");
        }