Esempio n. 1
0
        public async Task Handle(SubmitOrderCommand message, IMessageHandlerContext context)
        {
            using (var _context = new SalesContext())
            {
                Log.Info("Handle SubmitOrderCommand");

                var items = new List <Item>();

                var products = _context.Products.ToList();

                message.Products.ForEach(p => items.Add(new Item()
                {
                    Product = products.Single(s => s.Id == p)
                }));

                var order = new Divergent.Sales.Data.Models.Order()
                {
                    CustomerId  = message.CustomerId,
                    DateTimeUtc = DateTime.UtcNow,
                    Items       = items,
                    State       = "New"
                };

                _context.Orders.Add(order);
                await _context.SaveChangesAsync();

                // Publish event
                await context.Publish <OrderSubmittedEvent>(e =>
                {
                    e.OrderId    = order.Id;
                    e.CustomerId = message.CustomerId;
                    e.Products   = message.Products;
                });
            }
        }
        public async Task Handle(SubmitOrderCommand message, IMessageHandlerContext context)
        {
            Log.Info("Handle SubmitOrderCommand");

            var items = new List <Item>();

            var products = _context.Products.ToList();

            message.Products.ForEach(p => items.Add(new Item()
            {
                Id      = Guid.NewGuid(),
                OrderId = message.OrderId,
                Product = products.Single(s => s.Id == p)
            }));

            var order = new Divergent.Sales.Data.Models.Order()
            {
                CustomerId  = message.CustomerId,
                DateTimeUtc = DateTime.UtcNow,
                Id          = message.OrderId,
                Items       = items,
                State       = "New"
            };

            _context.Orders.Add(order);
            await _context.SaveChangesAsync();

            // TODO: Publish OrderSubmittedEvent event
        }