Beispiel #1
0
        public async Task <OrderDto> CreateAsync(CartOverview cartOverview, AddressDto address, PaymentDto payment)
        {
            var order = await _orderRepository.CreateAsync(new Order
            {
                AddressId     = address.Id,
                PaymentId     = payment.Id,
                UserId        = cartOverview.User.Id,
                OrderProducts = cartOverview.Products.Select(ToOrderProduct).ToList()
            });

            try
            {
                await _productService.SubtractAvailabilityAsync(
                    order.OrderProducts.Select(ToSubtractProductAvailability)
                    .ToList());
            }
            catch (Exception)
            {
                await _orderRepository.DeleteAsync(order);

                throw;
            }

            await _cartService.EmptyCartAsync(cartOverview.CartId);

            return(_mapper.Map <OrderDto>(order));
        }
Beispiel #2
0
        public async Task <CartOverview> GetCartOverviewAsync(int id)
        {
            var user = await _userRepository.GetAsync(id);

            user.ThrowIfNull(id);

            if (!user.Cart.CartProducts.Any())
            {
                return(new CartOverview());
            }

            var cartOverview = new CartOverview
            {
                User     = _mapper.Map <UserDto>(user),
                CartId   = user.CartId,
                Products = user.Cart.CartProducts.Select(ToProductOverview).ToList()
            };

            cartOverview.TotalPrice           = cartOverview.Products.Sum(_ => _.TotalPrice);
            cartOverview.TotalDiscountedPrice = cartOverview.Products.Sum(_ => _.TotalDiscountedPrice);
            cartOverview.FinalPrice           = cartOverview.Products.Sum(_ => _.FinalPrice);

            return(cartOverview);
        }