public override Task <CancelOrderResponse> CancelOrder(CancelOrderRequest request, grpc::ServerCallContext context)
        {
            Console.WriteLine("Handling Order cancellation request");


            var result = new CancelOrderResponse();

            try
            {
                var exists = orderManagementClient.OrderExists(new GetOrderRequest {
                    OrderID = request.OrderID
                });
                if (!exists.Exists)
                {
                    result.Canceled = false;
                    return(Task.FromResult(result));
                }
                var originalOrder = orderManagementClient.GetOrder(new GetOrderRequest {
                    OrderID = request.OrderID
                });

                var evt = new OrderCanceledEvent
                {
                    OrderID    = request.OrderID,
                    UserID     = request.UserID,
                    CreatedOn  = (ulong)DateTime.UtcNow.Ticks,
                    ActivityID = Guid.NewGuid().ToString(),
                };

                if (eventEmitter.EmitOrderCanceledEvent(evt))
                {
                    result.ConfirmationCode = Guid.NewGuid().ToString();
                    result.Canceled         = true;
                    foreach (var li in originalOrder.LineItems)
                    {
                        eventEmitter.EmitInventoryReleasedEvent(new InventoryReleasedEvent
                        {
                            SKU        = li.SKU,
                            ReleasedOn = (ulong)DateTime.UtcNow.Ticks,
                            Quantity   = li.Quantity,
                            OrderID    = request.OrderID,
                            UserID     = request.UserID,
                            EventID    = Guid.NewGuid().ToString()
                        });
                    }
                }
                else
                {
                    result.Canceled = false;
                }
                return(Task.FromResult(result));
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Failed to handle order cancellation {ex.ToString()}");
                result.Canceled = false;
                return(Task.FromResult(result));
            }
        }
Esempio n. 2
0
        public override Task <CancelOrderResponse> CancelOrder(CancelOrderRequest request, grpc::ServerCallContext context)
        {
            logger.LogInformation("Handling Order cancellation request");
            var result = new CancelOrderResponse();

            try
            {
                var agg = new OrderAggregate(request.OrderID);
                // TODO: load aggregate with order events
                var exists = orderManagementClient.OrderExists(new GetOrderRequest {
                    OrderID = request.OrderID
                });
                if (!exists.Exists)
                {
                    result.Canceled = false;
                    return(Task.FromResult(result));
                }
                var originalOrder = orderManagementClient.GetOrder(new GetOrderRequest {
                    OrderID = request.OrderID
                });

                var productAggregates = GetProductAggregates(originalOrder.LineItems.ToArray());

                IList <DomainEvent> evts = agg.Cancel(request.UserID, productAggregates);
                foreach (var evt in evts)
                {
                    this.eventEmitter.Emit(evt);
                }
                result.Canceled         = true;
                result.ConfirmationCode = Guid.NewGuid().ToString(); // unused
                return(Task.FromResult(result));
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Failed to cancel order");
                result.Canceled = false;
                return(Task.FromResult(result));
            }
        }