示例#1
0
        public void AddProductToCart(ShoppingBag bag, Guid shopGuid, ShopProduct actualProduct, int quantity)
        {
            var cart = bag.GetShoppingCartAndCreateIfNeededForGuestOnlyOrInBagDomain(shopGuid);

            cart.AddProductToCart(actualProduct, quantity);
            _unitOfWork.BagRepository.Update(bag);
        }
示例#2
0
        public void CheckDiscountPolicyWithoutUpdate(ShoppingBag bag, Guid shopGuid)
        {
            var      cart = bag.GetShoppingCartAndCreateIfNeededForGuestOnlyOrInBagDomain(shopGuid);
            Shop     shop = _unitOfWork.ShopRepository.FindByIdOrNull(shopGuid);
            BaseUser user = _unitOfWork.BaseUserRepository.FindByIdOrNull(bag.UserGuid);

            //Copy the list so you can iterate and add the discount to it
            ICollection <Tuple <ShopProduct, int> > tempPurchasedProducts = new List <Tuple <ShopProduct, int> >();

            foreach (Tuple <ShopProduct, int> record in cart.PurchasedProducts)
            {
                tempPurchasedProducts.Add(record);
            }
            foreach (IDiscountPolicy policy in shop.DiscountPolicies)
            {
                bool alreadyAddedDiscount = false;
                foreach (Tuple <ShopProduct, int> record in tempPurchasedProducts)
                {
                    var discountProductAndQuantity = policy.ApplyPolicy(cart, record.Item1.Guid, record.Item2, user, _unitOfWork);
                    if (discountProductAndQuantity != null && !alreadyAddedDiscount)
                    {
                        cart.AddProductToCart(discountProductAndQuantity.Item1, discountProductAndQuantity.Item2);
                        alreadyAddedDiscount = true;
                    }
                }
            }
        }
示例#3
0
        public void PurchaseCart(ShoppingBag bag, Guid shopGuid)
        {
            var cart = bag.GetShoppingCartAndCreateIfNeededForGuestOnlyOrInBagDomain(shopGuid);

            cart.PurchaseCart();
            _unitOfWork.BagRepository.Update(bag);
        }
示例#4
0
        public bool EditProductInCart(ShoppingBag bag, Guid shopGuid, Guid shopProductGuid, int newAmount)
        {
            var cart   = bag.GetShoppingCartAndCreateIfNeededForGuestOnlyOrInBagDomain(shopGuid);
            var result = cart.EditProductInCart(shopProductGuid, newAmount);

            _unitOfWork.BagRepository.Update(bag);
            return(result);
        }
示例#5
0
        public bool RemoveProductFromCart(ShoppingBag bag, Guid shopGuid, Guid shopProductGuid)
        {
            var cart   = bag.GetShoppingCartAndCreateIfNeededForGuestOnlyOrInBagDomain(shopGuid);
            var result = cart.RemoveProductFromCart(shopProductGuid);

            //if (cart.PurchasedProducts.Count == 0)
            //    bag.ShoppingCarts.Remove(cart);
            _unitOfWork.BagRepository.Update(bag);
            return(result);
        }
示例#6
0
        public void ClearAllDiscounts(ShoppingBag bag, Guid shopGuid)
        {
            var cart = bag.GetShoppingCartAndCreateIfNeededForGuestOnlyOrInBagDomain(shopGuid);
            var tmp  = cart.PurchasedProducts.Where(sp => sp.Item1.Price > 0).ToList();

            cart.PurchasedProducts.Clear();
            foreach (Tuple <ShopProduct, int> sp in tmp)
            {
                cart.PurchasedProducts.Add(sp);
            }
            _unitOfWork.BagRepository.Update(bag);
        }
示例#7
0
        public bool PurchaseCart(Shop shop, ShoppingBag bag)
        {
            var  cart            = bag.GetShoppingCartAndCreateIfNeededForGuestOnlyOrInBagDomain(shop.Guid);
            bool canPurchaseCart = true;

            foreach (var productAndAmountBought in cart.PurchasedProducts)
            {
                var      userGuid = cart.UserGuid;
                BaseUser user;
                try
                {
                    user = _unitOfWork.BaseUserRepository.FindByIdOrNull(userGuid);
                }
                catch (Exception)
                {
                    user = null;
                }
                //check purchase policies
                var quantity = productAndAmountBought.Item2;
                foreach (IPurchasePolicy policy in shop.PurchasePolicies)
                {
                    if (!policy.CheckPolicy(cart, productAndAmountBought.Item1.Guid, quantity, user, _unitOfWork) && !policyInCompound(shop, policy))
                    {
                        canPurchaseCart = false;
                    }
                }
            }
            if (!canPurchaseCart)
            {
                //_unitOfWork.ShopRepository.Update(shop);
                //_unitOfWork.BagRepository.Update(bag);
                return(false);
            }

            foreach (var productAndAmountBought in cart.PurchasedProducts)
            {
                var userGuid = cart.UserGuid;

                var actualProduct = shop.ShopProducts.FirstOrDefault(p => p.Guid.Equals(productAndAmountBought.Item1.Guid));
                //decrease stock quantity if it wasnt a discount record

                var quantity = productAndAmountBought.Item2;
                if (actualProduct != null)
                {
                    shop.UsersPurchaseHistory.Add(new Tuple <Guid, ShopProduct, int>(userGuid, actualProduct, quantity));
                    actualProduct.Quantity -= quantity;
                }
                else
                {
                    shop.UsersPurchaseHistory.Add(new Tuple <Guid, ShopProduct, int>(userGuid, productAndAmountBought.Item1, quantity));
                }
            }
            double total_price = cart.PurchasedProducts.Aggregate(0d, (total, p) => total += Convert.ToDouble(p.Item1.Price) * p.Item2);
            var    newEvent    = new PurchasedCartEvent(cart.UserGuid, cart.ShopGuid, total_price);

            newEvent.SetMessages(_unitOfWork);
            UpdateCenter.RaiseEvent(newEvent);
            ShoppingBagDomain.PurchaseCart(bag, shop.Guid);
            _unitOfWork.ShopRepository.Update(shop);
            return(true);
        }