示例#1
0
        //receive JSON data from Add.js. (When an item is added to the cart from gallery)
        public JsonResult AddItemToCart([FromBody] Addinput product)
        {
            string sessionId; try { sessionId = HttpContext.Request.Cookies["sessionId"]; } catch (NullReferenceException) { sessionId = null; }
            string userId; if (sessionId != null)

            {
                userId = HttpContext.Request.Cookies["userId"];
            }
            else
            {
                userId = Environment.MachineName;
            }

            CartItem item = db.Cart.FirstOrDefault(x => x.UserId == userId && x.pId == product.ProductId);

            if (item == null)
            {
                item = new CartItem();

                item.UserId   = userId;
                item.pId      = product.ProductId;
                item.Quantity = 1;
                item.product  = db.Products.FirstOrDefault(x => x.ProductId == int.Parse(product.ProductId));
                db.Add(item);
            }
            else
            {
                item.Quantity += 1;
                db.Update(item);
            }

            db.SaveChanges();

            List <CartItem> cart = db.Cart.Where(x => x.UserId == userId).ToList();

            int total = 0;

            foreach (CartItem x in cart)
            {
                total += x.Quantity;
            }

            return(Json(new
            {
                status = "success",
                total = total
            }));
        }
        public JsonResult RemoveItem([FromBody] Addinput productId)
        {
            string  sessionId = HttpContext.Request.Cookies["sessionId"];
            int     id        = int.Parse(productId.ProductId);
            Session session   = db.Sessions.FirstOrDefault(x => x.Id == sessionId);

            //if the user is not login, their session will be null
            if (session == null)
            {
                //fakeUserId is a cookies that will be sent to the guest(not login) to track their activity
                string   fakeUserId = HttpContext.Request.Cookies["cartItems"];
                CartItem cartitem   = cartitems.map[fakeUserId];

                //inside cartitem class is a list of KeyValuePair of <int,int> --> can refer to the cartitem class file for more information
                //KeyValuePair is used to store the productid and quantity of each of the products.
                for (int i = 0; i < cartitem.item.Count(); i++)
                {
                    if (cartitem.item[i].Key == id) //get the KeyValuePair of that particular product and increment the quantity by 1
                    {
                        cartitem.item.Remove(cartitem.item[i]);
                    }
                }
                cartitems.map[fakeUserId] = cartitem; //update the cartitems (cartitems is a singleton object that store cartitem of guest)(Same idea as sessions in the workshop)
            }
            else //else if the user has login
            {
                List <Cart> carts = db.Carts.Where(x => x.UserId == session.UserId).ToList(); //get the carts information of the user

                foreach (Cart item in carts)                                                  //get the cart row and increment the quantity by 1
                {
                    if (item.ProductId == id)
                    {
                        db.Carts.Remove(item);
                        db.SaveChanges();
                    }
                }
            }
            return(Json(new
            {
                status = "success"
            }));
        }
示例#3
0
        public JsonResult DeleteCartItem([FromBody] Addinput product)
        {
            string sessionId; try { sessionId = HttpContext.Request.Cookies["sessionId"]; } catch (NullReferenceException) { sessionId = null; }
            string userId; if (sessionId != null)

            {
                userId = HttpContext.Request.Cookies["userId"];
            }
            else
            {
                userId = Environment.MachineName;
            }

            CartItem item = db.Cart.FirstOrDefault(x => x.UserId == userId && x.pId == product.ProductId);

            db.Cart.Remove(item);
            db.SaveChanges();

            return(Json(new
            {
                status = "success"
            }));
        }
        //receive JSON data from Add.js. (When an item is added to the cart from gallery)
        public JsonResult Addin([FromBody] Addinput addinput)
        {
            Session session = db.Sessions.FirstOrDefault(x => x.Id == HttpContext.Request.Cookies["sessionId"]);

            bool newItem   = true;
            int  productid = int.Parse(addinput.ProductId);
            int  total     = 0;

            //if the user is not login, their session will be null
            if (session == null)
            {
                //fakeUserId is a cookies that will be sent to the guest(not login) to track their activity
                string   fakeUserId = HttpContext.Request.Cookies["cartItems"];
                CartItem cartitem;

                //if they dont have this cookies(null), create a cookie of fakeUserId and send to them
                if (fakeUserId == null)
                {
                    fakeUserId = Guid.NewGuid().ToString();
                    Response.Cookies.Append("cartItems", fakeUserId);
                    cartitem = new CartItem();
                }
                else //else if they have the fakeUserId cookie, find their record from the cartitems
                {
                    cartitem = null;
                    cartitems.map.TryGetValue(fakeUserId, out cartitem); //check if the fakeUserId is in our record or not
                    if (cartitem == null)                                //if its not in our record
                    {
                        cartitem = new CartItem();
                    }
                }
                //inside cartitem class is a list of KeyValuePair of <int,int> --> can refer to the cartitem class file for more information
                //KeyValuePair is used to store the productid and quantity of each of the products.
                foreach (KeyValuePair <int, int> item in cartitem.item)
                {
                    if (item.Key == productid) //to check if the product has been added before
                    {
                        newItem = false;
                    }
                }
                if (cartitem.item.Count() == 0 || newItem == true)                //if its a new product or the list is empty
                {
                    cartitem.item.Add(new KeyValuePair <int, int>(productid, 1)); //Add a new KeyValuePair of this productid with quantity of 1
                }
                else
                {
                    for (int i = 0; i < cartitem.item.Count(); i++)
                    {
                        if (cartitem.item[i].Key == productid) //get the KeyValuePair of that particular product and increment the quantity by 1
                        {
                            int quantity = cartitem.item[i].Value;
                            cartitem.item.Remove(cartitem.item[i]);
                            cartitem.item.Add(new KeyValuePair <int, int>(productid, quantity + 1));
                            break;
                        }
                    }
                }
                cartitems.map[fakeUserId] = cartitem; //update the cartitems (cartitems is a singleton object that store cartitem of guest)(Same idea as sessions in the workshop)

                //this part is to get the total quantity of products that the guest has. So that can be reflected on the cart image.
                for (int i = 0; i < cartitem.item.Count(); i++)
                {
                    total += cartitem.item[i].Value;
                }
            }
            else //else if the user has login
            {
                List <Cart> carts = db.Carts.Where(x => x.UserId == session.UserId).ToList(); //get the carts information of the user
                foreach (Cart item in carts) //check if its a item or not
                {
                    if (item.ProductId == productid)
                    {
                        newItem = false;
                    }
                }
                if (carts.Count() == 0 || newItem == true) //if its a new item or the user has no items in his cart
                {
                    Cart item = new Cart
                    {
                        UserId    = session.UserId,
                        ProductId = productid,
                        Quantity  = 1
                    };
                    db.Add(item);
                    db.SaveChanges();//save the item to the user cart database
                }
                else //else if its not a new item
                {
                    foreach (Cart item in carts)//get the cart row and increment the quantity by 1
                    {
                        if (item.ProductId == productid)
                        {
                            item.Quantity += 1;
                            db.SaveChanges();
                        }
                    }
                }

                //this part is to get the total quantity of products that the user has. So that can be reflected on the cart image.
                carts = db.Carts.Where(x => x.UserId == session.UserId).ToList();
                foreach (Cart item in carts)
                {
                    total += item.Quantity;
                }
            }
            //return the total as JSON to the Add.js
            return(Json(new
            {
                status = "success",
                total = total
            }));
        }