示例#1
0
        public override Task <OrderResponse> SubmitOrder(OrderRequest request, grpc::ServerCallContext context)
        {
            logger.LogInformation("Handling Order Request Submission");
            var response = new OrderResponse();

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

            try
            {
                var agg = new OrderAggregate(Guid.NewGuid().ToString());
                var productAggregates = GetProductAggregates(request.LineItems.ToArray());
                var evts = agg.Accept(request, productAggregates);
                foreach (var evt in evts)
                {
                    this.eventEmitter.Emit(evt);
                }
                response.Accepted = true;
                response.OrderID  = agg.OrderID;
                return(Task.FromResult(response));
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Failed to submit order");
                response.Accepted = false;
                return(Task.FromResult(response));
            }
        }
        public void Test_Order_AcceptProducesProductReserveEvents()
        {
            var tx = new OrderRequest
            {
                UserID    = Guid.NewGuid().ToString(),
                CreatedOn = (ulong)DateTime.UtcNow.Ticks,
                TaxRate   = 5
            };

            tx.LineItems.Add(new LineItem {
                SKU = "ABC123", UnitPrice = 12, Quantity = 1
            });
            tx.LineItems.Add(new LineItem {
                SKU = "SOAP12", UnitPrice = 200, Quantity = 5
            });

            var oa = new OrderAggregate("order-2-1");

            var productAggregates = FakeAggs();

            var evts = oa.Accept(tx, productAggregates);

            Assert.Equal(3, evts.Count);
            Assert.IsType <OrderAcceptedEvent>(evts[0]);
            Assert.IsType <InventoryReservedEvent>(evts[1]);
            Assert.IsType <InventoryReservedEvent>(evts[2]);
            var evt = (InventoryReservedEvent)evts[2];

            Assert.Equal <uint>(5, evt.Quantity);
        }