public async Task <Cart> Get(string userId)
        {
            IUserActor actor = GetActor(userId);

            Dictionary <Guid, int> products = await actor.GetCart();

            return(new Cart()
            {
                UserId = userId,
                Items = products.Select(
                    p => new CartItem {
                    ProductId = p.Key.ToString(), Quantity = p.Value
                }).ToArray()
            });
        }
Ejemplo n.º 2
0
        public async Task <CheckoutSummary> Checkout(string userId)
        {
            var result = new CheckoutSummary()
            {
                Date     = DateTime.UtcNow,
                Products = new List <CheckoutProduct>()
            };

            IUserActor actor = CreateUserActor(userId);
            var        cart  = await actor.GetCart();

            IProductCatalogService productCatalogService = GetProductCatalogService();
            var products = await productCatalogService.GetAllProducts();

            var totalCost = 0.0;

            foreach (var item in cart)
            {
                var productId = item.Key;
                var quantity  = item.Value;
                var product   = products.FirstOrDefault(p => p.Id == productId);
                if (product != null)
                {
                    result.Products.Add(new CheckoutProduct()
                    {
                        Product  = product,
                        Quantity = quantity,
                        Price    = product.Price * quantity
                    });
                    totalCost += (product.Price * quantity);
                }
            }
            result.TotalPrice = totalCost;

            //Clearing the cart since user has checkout all the products and add to history
            await actor.ClearCart();

            await AddToHistory(result);

            return(result);
        }
Ejemplo n.º 3
0
        public async Task <CartDto> GetCart([FromQuery] string userId)
        {
            try
            {
                CreateActor(userId);
                var cart = await _userActor.GetCart();

                return(new CartDto()
                {
                    UserId = userId,
                    Items = cart.Select(c => new CartItem()
                    {
                        ProductId = c.Key.ToString(),
                        Quantity = c.Value
                    }).ToArray()
                });
            }
            catch (Exception error)
            {
                throw error;
            }
        }