Ejemplo n.º 1
0
        public JsonResult DeleteItemFromCart(string productId)
        {
            string pidincart = Request.Cookies["cart"];

            if (pidincart == null)
            {
                return(Json(new { result = "fail" }));
            }
            else
            {
                List <string> cookielist = new List <string>();
                cookielist.AddRange(pidincart.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
                int index = cookielist.FindIndex(i => i == productId);
                if (index < 0)
                {
                    return(Json(new { result = "fail" }));
                }
                else
                {
                    while (cookielist.FindIndex(i => i == productId) != -1)
                    {
                        cookielist.Remove(productId);
                    }
                    string        cookiestring = cookielist.Aggregate((a, b) => a = a + "," + b);
                    CookieOptions options      = new CookieOptions();
                    options.Expires = DateTime.Now.AddDays(365);
                    Response.Cookies.Append("cart", cookiestring, options);
                    CartContent cartcontent = GetCartContent(cookielist);
                    return(Json(new { result = "success", total = cartcontent.total, quantity = cartcontent.quantity }));
                }
            }
        }
Ejemplo n.º 2
0
        public CartContent GetCartContent(List <String> cookielist)
        {
            CartContent cartcontent = new CartContent();
            // List<CartItem> itemsincart =new List<CartItem>();
            Dictionary <string, int> cartitem = new Dictionary <string, int>();

            foreach (string id in cookielist)
            {
                if (!cartitem.ContainsKey(id))
                {
                    cartitem.Add(id, 1);
                }
                else
                {
                    cartitem[id] += 1;
                }
            }
            // int total=0;
            // int quantity=0;
            foreach (KeyValuePair <string, int> entry in cartitem)
            {
                int     id = Convert.ToInt32(entry.Key);
                Product p  = _context.products
                             .SingleOrDefault(product => product.productId == id);
                cartcontent.total    += p.price * entry.Value;
                cartcontent.quantity += entry.Value;
                CartItem newcontent = new CartItem();
                newcontent.product  = p;
                newcontent.quantity = entry.Value;
                cartcontent.items.Add(newcontent);
            }
            return(cartcontent);
        }
Ejemplo n.º 3
0
        public JsonResult ChangeItemQuantityInCart(string productId, int quantity)
        {
            string pidincart = Request.Cookies["cart"];

            if (pidincart == null)
            {
                return(Json(new { result = "fail" }));
            }
            else
            {
                List <string> cookielist = new List <string>();
                cookielist.AddRange(pidincart.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
                int index = cookielist.FindIndex(i => i == productId);
                if (index < 0)
                {
                    return(Json(new { result = "fail" }));
                }
                else
                {
                    int currentcount = 0;
                    for (int i = 0; i < cookielist.Count; i++)
                    {
                        if (cookielist[i] == productId)
                        {
                            currentcount++;
                        }
                    }
                    int difference = quantity - currentcount;
                    if (difference == 0)
                    {
                        return(Json(new { result = "fail" }));
                    }
                    else
                    {
                        if (difference > 0)  //need add to cookie
                        {
                            while (difference > 0)
                            {
                                cookielist.Add(productId);
                                difference--;
                            }
                        }
                        else     //remove from cookie
                        {
                            while (difference < 0)
                            {
                                cookielist.Remove(productId);
                                difference++;
                            }
                        }
                        string        cookiestring = cookielist.Aggregate((a, b) => a = a + "," + b);
                        CookieOptions options      = new CookieOptions();
                        options.Expires = DateTime.Now.AddDays(365);
                        Response.Cookies.Append("cart", cookiestring, options);
                        CartContent cartcontent = GetCartContent(cookielist);
                        return(Json(new { result = "success", total = cartcontent.total, quantity = cartcontent.quantity }));
                    }
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Deletes the item from shopping cart.
        /// </summary>
        /// <param name="cartContentID">The cart item identifier.</param>
        /// <exception cref="ArgumentException">Item not found</exception>
        public async Task DeleteItem(int cartContentID)
        {
            if (!await this.unitOfWork.Set <CartContent>().AnyAsync(cc => cc.CartContentID == cartContentID))
            {
                throw new ArgumentException("Item not found");
            }

            CartContent cartItem = await this.unitOfWork.Set <CartContent>().FindAsync(cartContentID);

            this.unitOfWork.Set <CartContent>().Remove(cartItem);
            await this.unitOfWork.SaveAsync();
        }
Ejemplo n.º 5
0
        public IActionResult Cart()
        {
            string        pidincart  = Request.Cookies["cart"];
            List <string> cookielist = new List <string>();

            cookielist.AddRange(pidincart.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries));
            CartContent cartcontent = GetCartContent(cookielist);

            ViewBag.items    = cartcontent.items;
            ViewBag.total    = cartcontent.total;
            ViewBag.quantity = cartcontent.quantity;
            return(View("cartcontent"));
        }
Ejemplo n.º 6
0
        public async Task <Cart> CreateCart(CartContent cart)
        {
            CartContent content = new CartContent
            {
                PlatformIdentifier = _platformIdentifier,
                CartItems          = cart.CartItems,
                ShopperIdentifier  = cart.ShopperIdentifier,
                StoreIdentifier    = cart.StoreIdentifier,
                //TODO: See if we calculate the tax or just map it.
                Tax = cart.Tax,
            };

            Cart newEntity = new Cart
            {
                Contents = content
            };

            var entity = await _context.Carts.AddAsync(newEntity);

            return(entity);
        }
Ejemplo n.º 7
0
 public IActionResult Index(Node node, CartContent content)
 {
     return(View());
 }