Beispiel #1
0
        public IActionResult OnPost()
        {
            NewProduct.Description = ProductDescription;
            NewProduct.Id          = OldProduct.Id;
            int click = Int32.Parse(Request.Form["click"]);

            if (click == 1)
            {
                ProductsDB.UpdateProduct(NewProduct, _configuration);
                string currentShoppingCart = Request.Cookies["ShoppingCart"];
                if (currentShoppingCart != null && currentShoppingCart != "")
                {
                    string newShoppingCart = ShoppingCartCookie.UpdateProduct(currentShoppingCart, NewProduct);
                    var    cookieOptions   = new CookieOptions {
                        Expires = DateTime.Now.AddDays(1)
                    };
                    Response.Cookies.Append("ShoppingCart", newShoppingCart, cookieOptions);
                }
                return(RedirectToPage("/Index"));
            }
            else
            {
                return(RedirectToPage("/Index"));
            }
        }
        public void Process(RestoreCartArgs args)
        {
            var cookie = new ShoppingCartCookie();

            if (args.CartItems == null)
            {
                args.CartItems = cookie.CartItems;
            }
            else
            {
                foreach (var item in cookie.CartItems)
                {
                    if (!args.CartItems.ContainsKey(item.Key))
                    {
                        args.CartItems.Add(item.Key, item.Value);
                    }
                    else if (args.CartItems[item.Key] < item.Value)
                    {
                        args.CartItems[item.Key] = item.Value;
                    }
                }
            }
            if (string.IsNullOrEmpty(args.CouponCode))
            {
                args.CouponCode = cookie.CouponCode;
            }
        }
        public static void DecreaseAmount(int inventoryId, ShoppingCartCookie shoppingCartCookie)
        {
            ProductCookie pc = shoppingCartCookie.Products.FirstOrDefault(p => p.InventoryId == inventoryId);

            if (pc != null)
            {
                pc.DecreaseAmount();
            }
        }
 public static bool CookieToShoppingCart(string value, out ShoppingCartCookie shoppingCart)
 {
     shoppingCart = ShoppingCartCookie.ShoppingCartCookieFromString(value);
     shoppingCart.Products.RemoveAt(0);
     if (shoppingCart != null)
     {
         return(true);
     }
     return(false);
 }
        public void Process(PersistCartArgs args)
        {
            var cookie = new ShoppingCartCookie();

            cookie.CartItems.Clear();
            foreach (var item in args.CartItems)
            {
                cookie.CartItems.Add(item.Key, item.Value);
            }
            cookie.CouponCode = args.CouponCode;
            cookie.Save();
        }
Beispiel #6
0
        public IActionResult OnPost()
        {
            int click = Int32.Parse(Request.Form["click"]);

            if (click == 1)
            {
                Product = ProductsDB.GetProduct(Product.Id, _configuration);
                string currentShoppingCart = Request.Cookies["ShoppingCart"];
                string newShoppingCart     = ShoppingCartCookie.AddProduct(Product, currentShoppingCart);
                var    cookieOptions       = new CookieOptions {
                    Expires = DateTime.Now.AddDays(1)
                };
                Response.Cookies.Append("ShoppingCart", newShoppingCart, cookieOptions);
            }
            return(RedirectToPage("/Index"));
        }
        public static async Task <ShoppingCart> CookieToShoppingCartAsync(ShoppingCartCookie shoppingCartCookie, WebshopDbContext db)
        {                                                                                                                                   //To prevend Showing products that arent in stock
            List <Inventory> inventories = await db.Inventory.Where(i => shoppingCartCookie.Products.Any(p => i.InventoryId == p.InventoryId) && i.Stock > 0).Include(i => i.Product).ThenInclude(i => i.ProductImages).Include(p => p.GamePlatform).ToListAsync();

            List <ShoppingCartItem> shoppingCartItems = new List <ShoppingCartItem>();

            foreach (var inv in inventories)
            {
                int amount = shoppingCartCookie.Products.First(p => p.InventoryId == inv.InventoryId).Amount;
                shoppingCartItems.Add(new ShoppingCartItem()
                {
                    Amount      = amount,
                    Inventory   = inv,
                    InventoryId = inv.InventoryId,
                });
            }
            ShoppingCart shoppingCart = new ShoppingCart();

            shoppingCart.ShoppingCartItems = shoppingCartItems;
            return(shoppingCart);
        }
Beispiel #8
0
        public void OnGet()
        {
            string currentShoppingCart = Request.Cookies["ShoppingCart"];

            ShoppingCart = ShoppingCartCookie.GetProducts(currentShoppingCart);
        }
 public static void RemoveProduct(int inventoryId, ShoppingCartCookie shoppingCartCookie)
 {
     shoppingCartCookie.Products.RemoveAll(p => p.InventoryId == inventoryId);
 }
 public static void AddProductToShoppingCart(ShoppingCartCookie shoppingCart, int inventorId)
 {
     shoppingCart.AddProduct(new ProductCookie(inventorId));
 }