Ejemplo n.º 1
0
        public async Task <IActionResult> CreateProduct(Guid vendorId, Guid?productId, [FromBody] ProductModel productModel)
        {
            try
            {
                if (!productId.HasValue)
                {
                    productId = Guid.NewGuid();
                }

                var createdProduct = productModel.Clone();
                createdProduct.VendorId     = vendorId;
                createdProduct.CreationDate = DateTime.UtcNow;
                createdProduct.LastUpdate   = null;
                createdProduct.Id           = productId.Value;

                var savedProduct = await ServiceProxyUtils.GetProductService(productId.Value).Create(createdProduct);

                return(CreatedAtAction(nameof(Product), new { vendorId = vendorId, productId = savedProduct.Id }, savedProduct));
            }
            catch (Exception ex)
            {
                // TODO: handle error
                System.Diagnostics.Debug.WriteLine(ex.ToString());
                throw;
            }
        }
Ejemplo n.º 2
0
        private async Task OrderPaymentFailed(OrderModel order, string v)
        {
            // Persist order
            var orderDataService = ServiceProxyUtils.GetOrderService(order.Id);
            var updatedOrder     = order.Clone();

            updatedOrder.State = OrderState.Failed;
            updatedOrder.Payment.ProcessingDate = DateTime.UtcNow;
            updatedOrder.Payment.Status         = PaymentStatus.Failed;

            updatedOrder = await orderDataService.Update(updatedOrder);

            // Send to topic "order payment failed"
            await NotifyTopic(this.serviceBusConfiguration.OrderPaymentFailedTopicName, new OrderFinalizedEvent(updatedOrder));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Product(Guid vendorId, Guid productId)
        {
            try
            {
                var product = await ServiceProxyUtils.GetProductService(productId).Get(productId);

                return(product == null ?
                       (IActionResult)NotFound() :
                       new OkObjectResult(product));
            }
            catch (Exception ex)
            {
                // TODO: handle error
                System.Diagnostics.Debug.WriteLine(ex.ToString());
                throw;
            }
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> UpdateProduct(Guid vendorId, Guid productId, [FromBody] ProductModel product)
        {
            try
            {
                var updatedProduct = product.Clone();
                updatedProduct.Id         = productId;
                updatedProduct.VendorId   = vendorId;
                updatedProduct.LastUpdate = DateTime.UtcNow;

                var savedProduct = await ServiceProxyUtils.GetProductService(productId).Update(updatedProduct);

                return(new OkObjectResult(savedProduct));
            }
            catch (Exception ex)
            {
                // TODO: handle error
                System.Diagnostics.Debug.WriteLine(ex.ToString());
                throw;
            }
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> Get(Guid orderId)
        {
            try
            {
                var orderDataService = ServiceProxyUtils.GetOrderService(orderId);
                var order            = await orderDataService.Get(orderId);

                return(order == null ?
                       (IActionResult)this.NotFound() :
                       this.Ok(order));
            }
            catch (AggregateException ex)
            {
                if (ex.InnerException is OrderNotFoundException)
                {
                    return(this.NotFound());
                }

                throw;
            }
        }
Ejemplo n.º 6
0
        private void CreateOrderHandler(BrokeredMessage message)
        {
            try
            {
                var command = message.GetBody <CreateOrderCommand>();

                var order = new OrderModel()
                {
                    Id              = command.OrderId,
                    CreationDate    = DateTime.UtcNow,
                    State           = OrderState.Created,
                    Items           = command.Items,
                    DeliveryAddress = command.DeliveryAddress,
                };

                order.Total   = order.Items.Sum(x => x.UnitPrice * x.Price);
                order.Payment = new PaymentModel()
                {
                    CreditCardNumber = command.Payment.CreditCardNumber,
                    CreditCardType   = command.Payment.CreditCardType,
                    ExpirationMonth  = command.Payment.ExpirationMonth,
                    ExpirationYear   = command.Payment.ExpirationYear,
                    Status           = PaymentStatus.Pending,
                    Value            = order.Total,
                };

                // Persist order
                var orderDataService = ServiceProxyUtils.GetOrderService(order.Id);
                var createdOrder     = orderDataService.Create(order);

                // Send to topic "ordercreated"
                NotifyTopic(new OrderCreatedEvent(createdOrder));


                // Send to queue "orderverifyinventory"
            }
            catch (Exception ex)
            {
            }
        }
Ejemplo n.º 7
0
        public async Task <IActionResult> Products(Guid vendorId)
        {
            try
            {
                var products        = new List <ProductModel>();
                var productServices = await ServiceProxyUtils.GetProductServicePartitions(fabricClient);

                foreach (var productService in productServices)
                {
                    products.AddRange(await productService.List(vendorId));
                }


                return(new OkObjectResult(products));
            }
            catch (Exception ex)
            {
                // TODO: handle error
                System.Diagnostics.Debug.WriteLine(ex.ToString());
                throw;
            }
        }
Ejemplo n.º 8
0
        public async Task <IActionResult> PendingOrders()
        {
            var pendingOrders = new List <OrderModel>();

            try
            {
                foreach (var orderDataService in await ServiceProxyUtils.GetOrderServicePartitions(this.fabricClient))
                {
                    var orders = await orderDataService.GetPending();

                    if (orders != null)
                    {
                        pendingOrders.AddRange(orders);
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }

            return(new OkObjectResult(pendingOrders));
        }
Ejemplo n.º 9
0
        private async Task CreateOrderHandler(BrokeredMessage message)
        {
            try
            {
                var command = message.GetJsonBody <CreateOrderCommand>();

                var orderItems = new List <OrderItemModel>();
                foreach (var item in command.Items)
                {
                    var product = await this.catalogProductAPIClient.Get(item.ProductId);

                    if (product == null)
                    {
                        await NotifyTopic(this.serviceBusConfiguration.OrderCreationFailedTopicName, new OrderCreationFailedEvent()
                        {
                            OrderId = command.OrderId,
                            Reason  = $"Product {item.ProductId} not found",
                        });
                    }

                    var orderItem = new OrderItemModel()
                    {
                        ProductId = product.Id,
                        Quantity  = item.Quantity,
                        UnitPrice = product.Price,
                        Price     = product.Price * item.Quantity,
                        VendorId  = product.VendorId
                    };
                    orderItems.Add(orderItem);
                }

                var order = new OrderModel()
                {
                    Id              = command.OrderId,
                    CreationDate    = DateTime.UtcNow,
                    State           = OrderState.Created,
                    Items           = orderItems,
                    DeliveryAddress = command.DeliveryAddress,
                };

                order.Total   = order.Items.Sum(x => x.Price);
                order.Payment = new PaymentModel()
                {
                    CreditCardNumber = command.Payment.CreditCardNumber,
                    CreditCardType   = command.Payment.CreditCardType,
                    ExpirationMonth  = command.Payment.ExpirationMonth,
                    ExpirationYear   = command.Payment.ExpirationYear,
                    Status           = PaymentStatus.Pending,
                    Value            = order.Total,
                };

                // Persist order
                var orderDataService = ServiceProxyUtils.GetOrderService(order.Id);
                var createdOrder     = await orderDataService.Create(order);

                // Send to topic "ordercreated"
                await NotifyTopic(this.serviceBusConfiguration.OrderCreatedTopicName, new OrderCreatedEvent(createdOrder));

                // Send to queue "orderverifypayment"
                await SendCommand(this.serviceBusConfiguration.VerifyOrderPaymentQueueName, new VerifyOrderPaymentCommand(createdOrder));
            }
            catch (Exception ex)
            {
                await message.AbandonAsync();
            }
        }