public ShopCart GetShopCart(long userId) { ShopCart shopCart = ShopCartRepository.GetAll().Where(model => model.UserId == userId).FirstOrDefault(); if (shopCart == null) { shopCart = CreateShopCart(userId); } return(shopCart); }
private ShopCart CreateShopCart(long userId) { ShopCart shopCart = new ShopCart() { UserId = userId }; ShopCartRepository.Insert(shopCart); CurrentUnitOfWork.SaveChanges(); return(shopCart); }
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); }