public CalculationResult Change([FromBody] ChangeInput data) { var logic = new ChangeLogic(); var r = logic.Change(data.TotalMoney, data.Price); var x = new CalculationResult(); x.Result = r; return(x); }
public string Cart([FromBody] ChangeInput change)//receive JSON object from Cart.js when the number in the cart is changed { Session session = db.Sessions.FirstOrDefault(x => x.Id == HttpContext.Request.Cookies["sessionId"]); int tempProd = int.Parse(change.ProductId); int tempValue = int.Parse(change.Value); if (session == null) //if the user is not login { string fakeUserId = HttpContext.Request.Cookies["cartItems"]; //get the guest fakeuserid CartItem cartitem = cartitems.map[fakeUserId]; //get the guest cartitem information based on the fakeuserid (same idea as the sessions in workshop) //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 == tempProd) //update the quantity of the KeyValuePair with the productId passed in { cartitem.item.Remove(cartitem.item[i]); cartitem.item.Add(new KeyValuePair <int, int>(tempProd, tempValue)); } } } else //else if the user is login { int userid = session.UserId; List <Cart> carts = db.Carts.Where(x => x.UserId == userid).ToList(); foreach (Cart item in carts)//Update the cart of the user with the new value into the databse { if (item.ProductId == tempProd) { item.Quantity = tempValue; db.SaveChanges(); } } ; } object data = new { status = "success" }; return(JsonSerializer.Serialize(data)); }
public void ChangeCartItemQuantity([FromBody] ChangeInput change)//receive JSON object from Cart.js when the number in the cart is changed { 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 == change.ProductId); item.Quantity = int.Parse(change.Value); db.Cart.Update(item); db.SaveChanges(); }