Example #1
0
        public async Task <AddOrderResult> Handle(AddOrderRequest message)
        {
            _validator.ValidateAndThrow(message);

            var customer = _context.Customers.First(x => x.Id == message.Order.Customer.Id);

            var order = new Order(customer);

            foreach (var orderItem in message.Order.Items)
            {
                var lineItem = new OrderItem(_context.Products.First(x => x.Id == orderItem.Product.Id), orderItem.Quantity);
                order.AddItem(lineItem);
            }

            customer?.Orders.Add(order);

            var orderTotal = order.CalculateTotal();

            order.Customer.DebitAccount(orderTotal);

            await _context.SaveChangesAsync();

            var result = new AddOrderResult {
                OrderNumber = order.Id, ExpectedShipDate = DateTime.Now.AddDays(15), OrderTotal = order.CalculateTotal()
            };

            await _mediator.Publish(new OrderAddedNotification(result.OrderNumber));

            return(result);
        }
Example #2
0
        public async Task <AddProductResult> Handle(AddProductRequest request)
        {
            var product = new Product(request.PartNumber, request.Price);

            _context.Products.Add(product);

            await _context.SaveChangesAsync();

            return(new AddProductResult {
                Message = "Product Added", PartNumber = product.PartNumber, ProductId = product.Id
            });
        }