public void EditItemQuantity(int productId, int quantity) { string cachedProducts = this._cookieCachingService.Get("products"); var deserializedProducts = JsonConvert.DeserializeObject <List <ProductShoppingCartCache> >(cachedProducts); ProductShoppingCartCache product = deserializedProducts.FirstOrDefault(p => p.Id == productId); product.Quantity = quantity; this._cookieCachingService.Set("products", JsonConvert.SerializeObject(deserializedProducts), 30); }
public void AddItemToCart(int productId, string productName, int quantity) { string productsCache = this._cacheService.Get("products"); if (productsCache == null) { var products = new List <ProductShoppingCartCache> { new ProductShoppingCartCache { Id = productId, Quantity = quantity, ProductName = productName } }; this._cacheService.Set("products", JsonConvert.SerializeObject(products), 30); } else { var products = JsonConvert.DeserializeObject <List <ProductShoppingCartCache> >(productsCache); if (products.Any(p => p.Id == productId)) { products.First(p => p.Id == productId).Quantity += quantity; } else { var productsInShoppingCart = new ProductShoppingCartCache { Id = productId, Quantity = quantity, ProductName = productName }; products.Add(productsInShoppingCart); } this._cacheService.Set("products", JsonConvert.SerializeObject(products), 30); } }