public override Task <OrderResponse> SubmitOrder(OrderRequest request, grpc::ServerCallContext context)
        {
            Console.WriteLine("Handling Order Request Submission");
            var response = new OrderResponse();

            if (!isValidRequest(request))
            {
                response.Accepted = false;
                return(Task.FromResult(response));
            }

            var evt = OrderAcceptedEvent.FromProto(request);

            if (eventEmitter.EmitOrderAcceptedEvent(evt))
            {
                foreach (var li in evt.LineItems)
                {
                    var reservedEvent = new InventoryReservedEvent
                    {
                        OrderID    = evt.OrderID,
                        ReservedOn = (ulong)DateTime.UtcNow.Ticks,
                        SKU        = li.SKU,
                        Quantity   = li.Quantity,
                        UserID     = evt.UserID
                    };
                    if (!eventEmitter.EmitInventoryReservedEvent(reservedEvent))
                    {
                        response.Accepted = false;
                        return(Task.FromResult(response));
                    }
                }
                response.OrderID  = evt.OrderID;
                response.Accepted = true;
            }
            else
            {
                response.Accepted = false;
            }
            return(Task.FromResult(response));
        }
Example #2
0
        public bool EmitInventoryReservedEvent(InventoryReservedEvent evt)
        {
            try
            {
                evt.EventID = Guid.NewGuid().ToString();
                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 Inventory Reserved Event for Order {evt.OrderID}, SKU {evt.SKU}");
                client.SendMessageAsync(RESERVED_TOPIC, new[] { new Message(messageJson) }).Wait();

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