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));
            }
        }
Beispiel #2
0
        public bool EmitOrderCanceledEvent(OrderCanceledEvent evt)
        {
            try
            {
                var options = new KafkaOptions(new Uri("http://localhost:9092"));

                var router = new BrokerRouter(options);
                var client = new Producer(router);

                string messageJson = JsonConvert.SerializeObject(evt);
                Console.WriteLine($"Emitting order canceled event for order {evt.OrderID}");
                client.SendMessageAsync(CANCELED_TOPIC, new[] { new Message(messageJson) }).Wait();

                return(true);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Failed to emit order canceled event {ex.ToString()}");
                return(false);
            }
        }