Beispiel #1
0
        private bool TryToPriceBasket(Basket basket, BasketPricingResults results)
        {
            var pricesToAsk  = basket.Items.Select(x => x.Name).Distinct(StringComparer.OrdinalIgnoreCase);
            var prices       = this.priceRepository.GetItemPrices(pricesToAsk);
            var pricesByName = prices.ToDictionary(x => x.Name, StringComparer.OrdinalIgnoreCase);

            results.ItemPrices = new List <BasketItemPrice>();
            var isFailed = false;

            var itemCount = 0;

            foreach (var item in basket.Items.GroupBy(x => x.Name))
            {
                var itemName      = item.Key;
                var quantity      = item.Count();
                var itemPrice     = pricesByName.TryGetValue(itemName, out var price) ? price.Price : default(decimal?);
                var baskItemPrice =
                    new BasketItemPrice
                {
                    Name      = item.Key,
                    UnitPrice = itemPrice,
                    Quantity  = quantity
                };

                itemCount += quantity;
                results.ItemPrices.Add(baskItemPrice);

                if (!baskItemPrice.UnitPrice.HasValue)
                {
                    results.Messages.Add(string.Format(Errors.PriceUnavailable, baskItemPrice.Name));
                    isFailed = true;
                }
            }

            results.ItemCount = itemCount;

            return(isFailed);
        }
Beispiel #2
0
        private decimal CalculatePrice(BasketItemPrice basketItemPrice)
        {
            var price = basketItemPrice.UnitPrice.Value * basketItemPrice.Quantity;

            return(price);
        }