Пример #1
0
        public ShopCart GetShopCart(long userId)
        {
            ShopCart shopCart = ShopCartRepository.GetAll().Where(model => model.UserId == userId).FirstOrDefault();

            if (shopCart == null)
            {
                shopCart = CreateShopCart(userId);
            }
            return(shopCart);
        }
Пример #2
0
        private ShopCart CreateShopCart(long userId)
        {
            ShopCart shopCart = new ShopCart()
            {
                UserId = userId
            };

            ShopCartRepository.Insert(shopCart);
            CurrentUnitOfWork.SaveChanges();
            return(shopCart);
        }
Пример #3
0
        public ShopCart AddItem(ShopCartItem shopCartItem)
        {
            var specification = SpecificationRepository.Get(shopCartItem.SpecificationId);

            if (specification.Product.Type == ProductType.Virtual)
            {
                throw new UserFriendlyException(L("VirtualProductCannotAddToShopCart"));
            }
            ShopCart     shopCart             = GetShopCart(Session.UserId.Value);
            ShopCartItem shopCartItemForCheck = ShopCartItemRepository.GetAll().Where(model => model.ShopCartId == shopCart.Id && model.SpecificationId == shopCartItem.SpecificationId).FirstOrDefault();

            if (shopCartItemForCheck != null)
            {
                shopCartItemForCheck.Count += shopCartItem.Count;
            }
            else
            {
                shopCartItem.ShopCartId = shopCart.Id;
                shopCart.ShopCartItems.Add(shopCartItem);
            }
            CurrentUnitOfWork.SaveChanges();
            return(shopCart);
        }