Esempio n. 1
0
        public async Task <CommandResponse <Order> > ExecuteAsync(CreateOrderCommand command, CommandResponse <Order> previousResult)
        {
            ShoppingCart.Model.ShoppingCart cart = (await _dispatcher.DispatchAsync(new GetCartQuery {
                AuthenticatedUserId = command.AuthenticatedUserId
            })).Result;
            if (cart.Items.Count == 0)
            {
                return(CommandResponse <Order> .WithError("Shopping cart must not be empty to checkout"));
            }
            Order order = new Order
            {
                Id           = Guid.NewGuid(),
                CreatedAtUtc = DateTime.UtcNow,
                OrderItems   = cart.Items.Select(x => new OrderItem
                {
                    Name      = x.Product.Name,
                    Price     = x.Product.Price,
                    ProductId = x.Product.Id,
                    Quantity  = x.Quantity
                }).ToArray(),
                PaymentMade = false,
                PercentageDiscountApplied = 10.0,
                UserId = command.AuthenticatedUserId
            };

            order.Total = order.OrderItems.Sum(i => i.Price * i.Quantity) * (100 - order.PercentageDiscountApplied) / 100.0;
            await _repository.CreateAsync(order);

            return(CommandResponse <Order> .Ok(order));
        }
Esempio n. 2
0
        public double calculateFor(Cart shoppingCart)
        {
            var cartItems = shoppingCart.getShoppingCartItems();
            var products  = cartItems.Select(x => x.getProduct()).ToList();

            double numberOfDeliveries = products.GroupBy(x => x.getCategory(), (key, group) => group.ToList()).Count();
            double numberOfProducts   = products.Count;

            return((CostPerDelivery * numberOfDeliveries) + (CostPerProduct * numberOfProducts) + FixedCost);
        }
Esempio n. 3
0
        public async Task <CommandResponse <Order> > ExecuteAsync(CreateOrderCommand command, CommandResponse <Order> previousResult)
        {
            _logger.LogInformation("Creating order for user {0} from basket", command.AuthenticatedUserId);
            try
            {
                ShoppingCart.Model.ShoppingCart cart = (await _dispatcher.DispatchAsync(new GetCartQuery {
                    AuthenticatedUserId = command.AuthenticatedUserId
                })).Result;
                if (cart.Items.Count == 0)
                {
                    return(new CommandResponse <Order> {
                        ErrorMessage = "Shopping cart must not be empty to checkout", IsSuccess = false
                    });
                }
                Order order = new Order
                {
                    Id           = Guid.NewGuid(),
                    CreatedAtUtc = DateTime.UtcNow,
                    OrderItems   = cart.Items.Select(x => new OrderItem
                    {
                        Name      = x.Product.Name,
                        Price     = x.Product.Price,
                        ProductId = x.Product.Id,
                        Quantity  = x.Quantity
                    }).ToArray(),
                    PaymentMade = false,
                    PercentageDiscountApplied = 10.0,
                    UserId = command.AuthenticatedUserId
                };
                order.Total = order.OrderItems.Sum(i => i.Price * i.Quantity) * (100 - order.PercentageDiscountApplied) / 100.0;
                await _repository.CreateAsync(order);

                _logger.LogInformation("Created order for user {0} from basket", command.AuthenticatedUserId);
                return(CommandResponse <Order> .Ok(order));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Unable to create order for user {0}", command.AuthenticatedUserId);
                throw new CheckoutException($"Unable to create order for user {command.AuthenticatedUserId}");
            }
        }