/// <summary>
        /// GET | Adds an item to the cart.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool AddToCart(int id)
        {
            ProductModel selectedProduct = products.FindById(id);
            CartModel    cart            = null;

            if (User.Identity.IsAuthenticated)
            {
                cart = carts.FindByUserId(User.Identity.GetUserId());
            }
            else
            {
                cart = carts.FindById(BrowserStorage.GetCartId(System.Web.HttpContext.Current));
            }

            // See if we have a cart or need a new one
            if (cart == null)
            {
                cart = CreateCart();
            }

            // Add products to cart or update amount
            int count = cart.CartItems.Count(x => x.Product == selectedProduct);

            if (count == 0)
            {
                cart.CartItems.Add(new CartItemModel
                {
                    Product = selectedProduct,
                    Count   = 1
                });
            }
            else
            {
                CartItemModel item = cart.CartItems.Single(x => x.Product == selectedProduct);
                item.Count++;
            }
            carts.Edit(cart);

            // Call SignalR
            var context = GlobalHost.ConnectionManager.GetHubContext <ProductPageHub>();

            context.Clients.All.ReadyToBuy(selectedProduct.Id, carts.ProductCountInCarts(selectedProduct));

            return(true);
        }
        /// <summary>
        /// Gets the Users carts or Anonymous user if not logged in.
        /// </summary>
        /// <returns></returns>
        private CartModel GetCart()
        {
            CartModel cart = null;

            if (User.Identity.IsAuthenticated)
            {
                cart = carts.FindByUserId(User.Identity.GetUserId());
            }
            else
            {
                cart = carts.FindById(BrowserStorage.GetCartId(System.Web.HttpContext.Current));
            }
            if (cart == null)
            {
                cart           = new CartModel();
                cart.CartItems = new List <CartItemModel>();
            }
            return(cart);
        }
Exemple #3
0
        private void MergeCarts(string uid)
        {
            try
            {
                var cid = BrowserStorage.GetCartId(System.Web.HttpContext.Current);
                if (cid > 0)
                {
                    CartModel userCart    = carts.FindByUserId(uid);
                    CartModel sessionCart = carts.FindById(cid);

                    if (sessionCart != null)
                    {
                        if (userCart == null)
                        {
                            userCart = new CartModel
                            {
                                LastAccessed = DateTime.Now,
                                UserId       = uid,
                                CartItems    = new List <CartItemModel>()
                            }
                        }
                        ;
                        carts.Add(userCart);

                        foreach (CartItemModel item in sessionCart.CartItems)
                        {
                            if (!userCart.CartItems.Contains(item))
                            {
                                userCart.CartItems.Add(item);
                            }
                        }
                        carts.Remove(sessionCart);
                        BrowserStorage.ClearCartId(System.Web.HttpContext.Current);
                    }
                }
            }
            catch (Exception)
            {
            }
        }