Beispiel #1
0
        public async Task <CheckoutSummary> CheckoutAsync(string userId)
        {
            var result = new CheckoutSummary()
            {
                Date     = DateTime.UtcNow,
                Products = new List <CheckoutProduct>()
            };

            IUserActor userActor = GetUserActor(userId);

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

            IProductCatalogService catalogService = GetProductCatalogService();

            foreach (var basketItem in basket)
            {
                Product product = await catalogService.GetProductAsync(basketItem.ProductId);

                var checkoutProduct = new CheckoutProduct
                {
                    Product  = product,
                    Price    = product.Price,
                    Quantity = basketItem.Quantity
                };
                result.Products.Add(checkoutProduct);
            }
            result.TotalPrice = result.Products.Sum(p => p.Price * p.Quantity);
            //clear user basket
            await userActor.ClearBasket();

            await AddToHistoryAsync(result);

            return(result);
        }
        public async Task <ApiProduct> GetByIdAsync(Guid productId)
        {
            ApiProduct result = new ApiProduct();

            Product product = await _service.GetProductAsync(productId);

            if (product != null)
            {
                result.Id          = product.Id;
                result.Description = product.Description;
                result.Price       = product.Price;
                result.IsAvailable = product.Availability > 0;
            }

            return(result);
        }
        public async Task <Product> GetProductAsync(string productNumber)
        {
            string cacheFileName = string.Format("Product{0}", productNumber);

            try
            {
                // Retrieve the items from the cache
                return(await _cacheService.GetDataAsync <Product>(cacheFileName));
            }
            catch (FileNotFoundException)
            {
            }
            // Retrieve the items from the service
            var product = await _productCatalogService.GetProductAsync(productNumber);

            // Save the items in the cache
            await _cacheService.SaveDataAsync(cacheFileName, product);

            return(product);
        }
        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);
        }
Beispiel #5
0
        public async Task <CheckoutSummary> CheckoutAsync(string userId)
        {
            var checkoutSummary = new CheckoutSummary();

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

            IUserActor userActor       = GetUserActor(userId);
            var        userBasketItems = await userActor.GetBasketAsync();

            var basket = new Dictionary <Guid, int>();

            foreach (var userBasketItem in userBasketItems)
            {
                basket.Add(userBasketItem.ProductId, userBasketItem.Quantity);
            }

            IProductCatalogService catalogService = GetProductCatalogService();

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

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

                checkoutSummary.Products.Add(checkoutProduct);

                checkoutSummary.TotalPrice += checkoutProduct.SubTotal;
            }

            await AddToHistoryAsync(checkoutSummary);

            return(checkoutSummary);
        }
Beispiel #6
0
        public async Task <CheckoutSummary> CheckoutAsync(string userId)
        {
            var result = new CheckoutSummary();

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

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

            //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.GetProductAsync(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.ClearBasketAsync();

            await AddToHistory(result);

            return(result);
        }
Beispiel #7
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);
        }