예제 #1
0
        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));
        }
예제 #2
0
        public static OrderAcceptedEvent FromProto(OrderRequest tx)
        {
            var evt = new OrderAcceptedEvent
            {
                TaxRate   = tx.TaxRate,
                CreatedOn = tx.CreatedOn,
                UserID    = tx.UserID
            };

            evt.LineItems = new List <EventLineItem>();
            foreach (LineItem li in tx.LineItems)
            {
                evt.LineItems.Add(new EventLineItem
                {
                    SKU       = li.SKU,
                    Quantity  = li.Quantity,
                    UnitPrice = li.UnitPrice
                });
            }

            return(evt);
        }
예제 #3
0
        public bool EmitOrderAcceptedEvent(OrderAcceptedEvent evt)
        {
            try
            {
                evt.OrderID = 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 Order Accepted Event {evt.OrderID}");
                client.SendMessageAsync(ORDERS_TOPIC, new[] { new Message(messageJson) }).Wait();

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