コード例 #1
0
        protected virtual Task ValidateCartItemsAsync()
        {
            foreach (var lineItem in Cart.Items.ToList())
            {
                lineItem.ValidationErrors.Clear();

                if (lineItem.Product == null || !lineItem.Product.IsActive || !lineItem.Product.IsBuyable)
                {
                    lineItem.ValidationErrors.Add(new UnavailableError());
                }
                else
                {
                    var isProductAvailable = new ProductIsAvailableSpecification(lineItem.Product).IsSatisfiedBy(lineItem.Quantity);
                    if (!isProductAvailable)
                    {
                        var availableQuantity = lineItem.Product.AvailableQuantity;
                        lineItem.ValidationErrors.Add(new QuantityError(availableQuantity));
                    }

                    var tierPrice = lineItem.Product.Price.GetTierPrice(lineItem.Quantity);
                    if (tierPrice.Price > lineItem.SalePrice)
                    {
                        lineItem.ValidationErrors.Add(new PriceError(lineItem.SalePrice, lineItem.SalePriceWithTax, tierPrice.Price, tierPrice.PriceWithTax));
                    }
                }

                lineItem.IsValid = !lineItem.ValidationErrors.Any();
            }
            return(Task.CompletedTask);
        }
コード例 #2
0
        protected virtual async Task ValidateCartItemsAsync()
        {
            var products = await GetCartProductsAsync();

            foreach (var lineItem in Cart.Items.ToList())
            {
                lineItem.ValidationErrors.Clear();

                var product = products.FirstOrDefault(p => p.Id == lineItem.ProductId);
                if (product == null || !product.IsActive || !product.IsBuyable)
                {
                    lineItem.ValidationErrors.Add(new UnavailableError());
                    lineItem.IsValid = false;
                }
                else
                {
                    var isProductAvailable = new ProductIsAvailableSpecification(product).IsSatisfiedBy(lineItem.Quantity);
                    if (!isProductAvailable)
                    {
                        lineItem.IsValid = false;

                        var availableQuantity = product.AvailableQuantity;
                        lineItem.ValidationErrors.Add(new QuantityError(availableQuantity));
                    }

                    var tierPrice = product.Price.GetTierPrice(lineItem.Quantity);
                    if (tierPrice.Price > lineItem.SalePrice)
                    {
                        lineItem.ValidationErrors.Add(new PriceError(lineItem.SalePrice, lineItem.SalePriceWithTax, tierPrice.Price, tierPrice.PriceWithTax));
                    }
                }
            }
        }
コード例 #3
0
        public virtual async Task AddItemAsync(Product product, int quantity)
        {
            EnsureCartExists();

            var isProductAvailable = new ProductIsAvailableSpecification(product).IsSatisfiedBy(quantity);

            if (isProductAvailable)
            {
                var lineItem = product.ToLineItem(Cart.Language, quantity);
                await AddLineItemAsync(lineItem);
            }
        }
コード例 #4
0
 public ActionResult <IEnumerable <ProductDetails> > GetActive()
 {
     using (var context = new ProductContext())
     {
         // In EF for .Net Framework logging can be turned on by:
         // context.Database.Log = s => Console.WriteLine(s);
         var specification = new ProductIsAvailableSpecification().And(new ProductHasEnoughStockSpecification());
         return(context.Products
                .Where(specification)
                .Select(ProductDetails.FromDb)
                .ToList());
     }
 }
コード例 #5
0
        public CartLineItemValidator(ShoppingCart cart)
        {
            RuleSet("strict", () =>
            {
                RuleFor(x => x).Custom((lineItem, context) =>
                {
                    lineItem.ValidationErrors.Clear();

                    if (lineItem.Product == null || !lineItem.Product.IsActive || !lineItem.Product.IsBuyable)
                    {
                        var unavailableError = new UnavailableError();
                        lineItem.ValidationErrors.Add(unavailableError);
                        context.AddFailure(new ValidationFailure(nameof(lineItem.Product), "The product is not longer available for purchase"));
                    }
                    else if (lineItem.Product.Price == null || lineItem.Product.Price.GetTierPrice(lineItem.Quantity).Price == 0)
                    {
                        var unavailableError = new UnavailableError();
                        lineItem.ValidationErrors.Add(unavailableError);
                    }
                    else
                    {
                        var isProductAvailable = new ProductIsAvailableSpecification(lineItem.Product).IsSatisfiedBy(lineItem.Quantity);
                        if (!isProductAvailable)
                        {
                            var availableQuantity = lineItem.Product.AvailableQuantity;
                            var qtyError          = new QuantityError(availableQuantity);
                            lineItem.ValidationErrors.Add(qtyError);
                            context.AddFailure(new ValidationFailure(nameof(lineItem.Product.AvailableQuantity), "The product available qty is changed"));
                        }

                        var tierPrice = lineItem.Product.Price.GetTierPrice(lineItem.Quantity);
                        if (tierPrice.Price != lineItem.SalePrice)
                        {
                            var priceError = new PriceError(lineItem.SalePrice, lineItem.SalePriceWithTax, tierPrice.Price, tierPrice.PriceWithTax);
                            lineItem.ValidationErrors.Add(priceError);
                            context.AddFailure(new ValidationFailure(nameof(lineItem.SalePrice), "The product price is changed"));
                        }
                    }
                });
            });
        }
        public void ShouldReturnCorrectResults()
        {
            var isAvailable = new ProductIsAvailableSpecification();

            var products = new List <Product>
            {
                new Product
                {
                    ProductId = 1,
                    IsActive  = true
                },
                new Product
                {
                    ProductId = 2,
                    IsActive  = false
                }
            };

            var availableProducts = products.AsQueryable().Where(isAvailable).ToList();

            Assert.AreEqual(availableProducts.Count, 1);
            Assert.AreEqual(availableProducts.Single().ProductId, 1);
        }