Ejemplo n.º 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"));
            }
        }
Ejemplo n.º 2
0
        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;
            }
        }
Ejemplo n.º 3
0
        public static void DecreaseAmount(int inventoryId, ShoppingCartCookie shoppingCartCookie)
        {
            ProductCookie pc = shoppingCartCookie.Products.FirstOrDefault(p => p.InventoryId == inventoryId);

            if (pc != null)
            {
                pc.DecreaseAmount();
            }
        }
Ejemplo n.º 4
0
 public static bool CookieToShoppingCart(string value, out ShoppingCartCookie shoppingCart)
 {
     shoppingCart = ShoppingCartCookie.ShoppingCartCookieFromString(value);
     shoppingCart.Products.RemoveAt(0);
     if (shoppingCart != null)
     {
         return(true);
     }
     return(false);
 }
Ejemplo n.º 5
0
        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();
        }
Ejemplo n.º 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"));
        }
Ejemplo n.º 7
0
        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);
        }
Ejemplo n.º 8
0
        public void OnGet()
        {
            string currentShoppingCart = Request.Cookies["ShoppingCart"];

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