예제 #1
0
        public async Task <CheckoutSummary> CheckoutAsync(string userId)
        {
            var result = new CheckoutSummary();

            result.Date     = DateTime.UtcNow;
            result.Products = new List <CheckoutProduct>();
            IUserActor userActor = GetUserActor(userId);

            BasketItem[] basket = await userActor.GetBasket();

            var catalogService = GetProductCatalogService();

            foreach (var basketLine in basket)
            {
                var product = await catalogService.GetProduct(basketLine.ProductId);

                var checkoutProduct = new CheckoutProduct
                {
                    Product  = product,
                    Price    = product.Price,
                    Quantity = basketLine.Quantity
                };

                result.Products.Add(checkoutProduct);
            }

            result.TotalPrice = result.Products.Sum(p => p.Price);
            await userActor.ClearBasket();

            await AddToHistoryAsync(result);

            return(result);
        }
예제 #2
0
        public async Task <CheckoutSummary> Checkout(string userId)
        {
            var result = new CheckoutSummary();

            //call user actor to get the basket
            IUserActor             userActor = GetUserActor(userId);
            Dictionary <Guid, int> basket    = await userActor.GetBasket();

            //get catalog client
            IProductCatalogService catalogService = GetProductCatalogService();

            //constuct CheckoutProduct items by calling to the catalog
            foreach (KeyValuePair <Guid, int> basketLine in basket)
            {
                Product product = await catalogService.GetProduct(basketLine.Key);

                var checkoutProduct = new CheckoutProduct
                {
                    Product  = product,
                    Price    = product.Price,
                    Quantity = basketLine.Value
                };
                result.Products.Add(checkoutProduct);
            }

            //generate total price
            result.TotalPrice = result.Products.Sum(p => p.Price);

            //clear user basket
            await userActor.ClearBasket();

            await AddToHistory(result);

            return(result);
        }
예제 #3
0
        public async Task <CheckoutSummary> Checkout(string userId)
        {
            var result = new CheckoutSummary();

            result.Date     = DateTime.UtcNow;
            result.Products = new List <CheckoutProduct>();

            IUserActor             userActor = GetUserActor(userId);
            Dictionary <Guid, int> basket    = await userActor.GetBasket();

            IProductCatalogService catalogService = GetProductCatalogService();

            foreach (KeyValuePair <Guid, int> basketLine in basket)
            {
                Product product = await catalogService.GetProduct(basketLine.Key);

                var checkoutProduct = new CheckoutProduct
                {
                    Product  = product,
                    Price    = product.Price,
                    Quantity = basketLine.Value
                };
                result.Products.Add(checkoutProduct);
            }

            result.TotalPrice = result.Products.Sum(p => p.Price);

            await userActor.ClearBasket();

            await AddToHistory(result);

            return(result);
        }
예제 #4
0
        public async Task <CheckoutSummary> CheckoutAsync(string userId)
        {
            var result = new CheckoutSummary();

            result.Date     = DateTime.UtcNow;
            result.Products = new List <CheckoutProduct>();

            // Get the user basket
            IUserActor userActor = GetUserActor(userId);

            BasketItem[] basket = await userActor.GetBasket();

            IProductCatalogService catalogService = GetProductCatalogService();

            foreach (var item in basket)
            {
                var product = await catalogService.GetProduct(item.ProductId);

                var checkoutProduct = new CheckoutProduct()
                {
                    Product  = product,
                    Price    = product.Price,
                    Quantity = item.Quantity
                };

                result.Products.Add(checkoutProduct);
            }

            return(result);
        }
예제 #5
0
        public async Task <ApiBasket> Get(string userId)
        {
            IUserActor actor = GetActor(userId);

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

            return(new ApiBasket()
            {
                UserId = userId,
                Items = products.Select(p => new ApiBasketItem {
                    ProductId = p.Key.ToString(), Quantity = p.Value
                }).ToArray()
            });
        }
예제 #6
0
        public async Task <ApiBasket> Get(string userId)
        {
            IUserActor userActor = GetActor(userId);

            Dictionary <Guid, int> basketItems = await userActor.GetBasket();

            ApiBasket apiBasket = new ApiBasket()
            {
                UserId = userId,
                Items  = basketItems.Select(i => new ApiBasketItem {
                    ProductId = i.Key.ToString(), Quantity = i.Value
                }).ToArray()
            };

            return(apiBasket);
        }
예제 #7
0
        public async Task <ApiBasket> GetAsync(string userId)
        {
            IUserActor actor = GetActor(userId);

            BasketItem[] products = await actor.GetBasket();

            return(new ApiBasket()
            {
                UserId = userId,
                Items = products.Select(p => new ApiBasketItem
                {
                    ProductId = p.ProductId.ToString(),
                    Quantity = p.Quantity
                })
                        .ToArray()
            });
        }
예제 #8
0
        public async Task <ApiBasket> GetAsync(string userId)
        {
            IUserActor actor = GetActor(userId);

            // We should not use any complicated types over the wire (interfaces, generics, etc)
            //Dictionary<Guid, int> products = await actor.GetBasket();
            BasketItem[] products = await actor.GetBasket();

            return(new ApiBasket()
            {
                UserId = userId,
                Items = products.Select( // Convert from Product to ApiBasketItem
                    p => new ApiBasketItem
                {
                    ProductId = p.ProductId.ToString(), //Key.ToString(),
                    Quantity = p.Quantity               //Value
                }).ToArray()
            });
        }
        public async Task <CheckoutSummary> CheckoutAsync(string userId)
        {
            //holds result of the checkout
            var result = new CheckoutSummary();

            result.Date     = DateTime.UtcNow;
            result.Products = new List <CheckoutProduct>();

            //call user actor to get the basket
            IUserActor userActor = GetUserActor(userId);

            BasketItem[] basket = await userActor.GetBasket();

            //get catalog client
            IProductCatalogService catalogService = GetProductCatalogService();

            //get detailed product information for each basket item by calling product service
            foreach (BasketItem basketLine in basket)
            {
                Product product = await catalogService.GetProductAsync(basketLine.ProductId);

                var checkoutProduct = new CheckoutProduct
                {
                    Product  = product,
                    Price    = product.Price,
                    Quantity = basketLine.Quantity
                };
                result.Products.Add(checkoutProduct);
            }

            //generate total price
            result.TotalPrice = result.Products.Sum(p => p.Price);

            //clear user basket
            await userActor.ClearBasket();

            await AddToHistoryAsync(result);

            return(result);
        }
예제 #10
0
        public async Task <CheckoutSummary> CheckoutAsync(string userId)
        {
            CheckoutSummary result = new CheckoutSummary()
            {
                Date     = DateTime.UtcNow,
                Products = new List <CheckoutProduct>()
            };

            // get user basket
            IUserActor userActor = GetUserActor(userId);
            var        basket    = await userActor.GetBasket();

            IProductCatalogService catalogService = GetProductCatalogService();

            // build the products using the catalog service data and the basket line items
            foreach (var basketItem in basket)
            {
                Product product = await catalogService.GetProductAsync(basketItem.ProductId);

                if (product != null)
                {
                    var checkoutProduct = new CheckoutProduct()
                    {
                        Product  = product,
                        Price    = product.Price,
                        Quantity = basketItem.Quantity
                    };

                    result.Products.Add(checkoutProduct);
                }
            }

            // add the current checkout summary to history (for later retrieval)
            await AddToHistoryAsync(result);

            return(result);
        }