Esempio n. 1
0
        // Helper Method
        public void UserCartCheck()
        {
            HttpCookie cookie = Request.Cookies["CartCookie"];

            if (cookie != null)
            {
                int cartid = Convert.ToInt32(cookie["CartId"]);
                List <UnauthenticatedCart> unauthenticatedCarts = new List <UnauthenticatedCart>();
                unauthenticatedCarts = _context.UnauthenticatedCarts.Where(p => p.cartId == cartid).ToList();
                AuthenticatedCart authenticatedCart = new AuthenticatedCart();
                if (unauthenticatedCarts.Count == 0)
                {
                    return;
                }
                authenticatedCart.userId = FetchUserId();
                foreach (var p in unauthenticatedCarts)
                {
                    authenticatedCart.movieId = p.movieId;
                    if (!_context.AuthenticatedCarts.Any(m => (m.userId == authenticatedCart.userId && m.movieId == authenticatedCart.movieId)))
                    {
                        _context.AuthenticatedCarts.Add(authenticatedCart);
                    }
                    _context.UnauthenticatedCarts.Remove(p);
                    _context.SaveChanges();
                }
            }
        }
Esempio n. 2
0
        public ActionResult AddToCart(int selectedId = -1)
        {
            if (selectedId == -1)
            {
                return(new HttpNotFoundResult());
            }
            bool mAdded    = false;
            int  CartCount = 0;

            if (User.Identity.IsAuthenticated)
            {
                AuthenticatedCart authenticatedCart = new AuthenticatedCart();
                string            userid            = FetchUserId();
                authenticatedCart = _context.AuthenticatedCarts.SingleOrDefault(p => p.userId == userid && p.movieId == selectedId);
                CartCount         = _context.AuthenticatedCarts.Where(p => p.userId == userid).ToList().Count;
                if (authenticatedCart != null)
                {
                    _context.AuthenticatedCarts.Remove(authenticatedCart);
                    CartCount--;
                    mAdded = false;
                }
                else
                {
                    authenticatedCart         = new AuthenticatedCart();
                    authenticatedCart.movieId = selectedId;
                    authenticatedCart.userId  = userid;
                    _context.AuthenticatedCarts.Add(authenticatedCart);
                    CartCount++;
                    mAdded = true;
                }
            }
            else
            {
                HttpCookie cookie = new HttpCookie("CartCookie");
                cookie = Request.Cookies["CartCookie"];
                int cartid;
                if (cookie == null)
                {
                    cookie           = new HttpCookie("CartCookie");
                    cartid           = CartIdGen();
                    cookie["CartId"] = Convert.ToString(cartid);
                    cookie.Expires   = DateTime.Now.AddMonths(3);
                    Response.Cookies.Add(cookie);
                }
                else
                {
                    cartid = Convert.ToInt32(cookie["CartId"]);
                }
                UnauthenticatedCart unauthenticatedCart = new UnauthenticatedCart();
                unauthenticatedCart = _context.UnauthenticatedCarts.SingleOrDefault(p => p.cartId == cartid && p.movieId == selectedId);
                CartCount           = _context.UnauthenticatedCarts.Where(p => p.cartId == cartid).ToList().Count;
                if (unauthenticatedCart != null)
                {
                    _context.UnauthenticatedCarts.Remove(unauthenticatedCart);
                    CartCount--;
                    mAdded = false;
                }
                else
                {
                    unauthenticatedCart         = new UnauthenticatedCart();
                    unauthenticatedCart.cartId  = cartid;
                    unauthenticatedCart.movieId = selectedId;
                    _context.UnauthenticatedCarts.Add(unauthenticatedCart);
                    CartCount++;
                    mAdded = true;
                }
            }
            _context.SaveChanges();
            CartButtonViewModel cartButtonViewModel = new CartButtonViewModel()
            {
                movieAdded     = mAdded,
                MovieId        = selectedId,
                TotalCartCount = CartCount
            };

            return(PartialView("~/Views/Movie/_CartButtonPartial.cshtml", cartButtonViewModel));
        }