Beispiel #1
0
        public async Task <ApiBasket> Get(string userId)
        {
            IUserActor actor = GetActor(userId);

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

            return(new ApiBasket()
            {
                UserId = userId,
                Items = products.Select(x => new ApiBasketItem {
                    ProductId = x.Key.ToString(), Quantity = x.Value
                }).ToArray()
            });
        }
Beispiel #2
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 #3
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);
        }