Ejemplo n.º 1
0
        public async Task <CommandResult> Handle(PurchaseCommand purchaseCommand)
        {
            CommandResult commandResponse = ValidateCommand(purchaseCommand);

            //if (!commandResponse.Succeed)
            //{
            //    return commandResponse;
            //}
            //if (!await IsStoreServiceOn()) // REST call with auto retry policy
            //{
            //    commandResponse.Error = "Sorry! Store Service is not On. You have to wait until it is open";

            //    return commandResponse;
            //}
            if (await GetCurrentStoreItems() > 1000L) // GRPC call with Circuit breaker policy
            {
                commandResponse.Error = "Sorry! Current storeitems more than 1000. So no more purchase possible";

                return(commandResponse);
            }

            Purchase purchase = Map(purchaseCommand);
            await purchaseRepository.Save(purchase);

            ProductPurchasedEvent productPurchasedEvent = Map(purchase);
            await serviceBus.Publish(Constants.MessageQueues.PurchasedQueue, productPurchasedEvent);

            EmailParams emailParams = BuildEmailParameters(productPurchasedEvent);
            await emailService.SendEmail(emailParams);

            return(new CommandResult());
        }
Ejemplo n.º 2
0
        private EmailParams BuildEmailParameters(ProductPurchasedEvent @event)
        {
            long purchasedTotalPrice = 0;

            @event.LineItems.ToList().ForEach(item => purchasedTotalPrice += item.PurchaedTotalPrice);

            var emailParams = new EmailParams
            {
                Subject = "Product Purchased",
                Body    = $"The Product Purchased Amount={purchasedTotalPrice}"
            };

            emailParams.ToList.Add(Constants.EmailAddressess.AdminPurchaseEmailAddress);

            return(emailParams);
        }
Ejemplo n.º 3
0
        private ProductPurchasedEvent Map(Purchase purchase)
        {
            var productPurchasedEvent = new ProductPurchasedEvent
            {
                PurchaseDate = purchase.PurchaseDate
            };

            foreach (ProductLineItem productLineItem in purchase.LineItems)
            {
                var lineItem = new PurchasedLineItem
                {
                    ProductId          = productLineItem.Product.Id,
                    PurchasedUnitPrice = productLineItem.PurchaseUnitPrice,
                    PurchasedQuantity  = productLineItem.PurchaseQuantity
                };
                productPurchasedEvent.LineItems.Add(lineItem);
            }

            return(productPurchasedEvent);
        }
        public async Task ShouldPublishMessageWhenValidQueueProvided()
        {
            var lineItems = new List <PurchasedLineItem>
            {
                new PurchasedLineItem {
                    ProductId = Guid.NewGuid().ToString(), PurchasedUnitPrice = 100, PurchasedQuantity = 100
                },
                new PurchasedLineItem {
                    ProductId = Guid.NewGuid().ToString(), PurchasedUnitPrice = 200, PurchasedQuantity = 200
                },
            };

            var productPurchasedEvent = new ProductPurchasedEvent
            {
                PurchaseDate = DateTime.UtcNow,
                LineItems    = lineItems
            };

            await rabbitMqServiceBus.Publish(QueueName, productPurchasedEvent);
        }
Ejemplo n.º 5
0
 private void OnProductPurchasedEvent(ProductPurchasedEvent productPurchasedEvent)
 {
     IsPurchaseOrderProcessed = productPurchasedEvent.IsPurchaseOrderProcessed;
 }