public IHttpActionResult PostAddItem([FromBody] string data) { using (DinerAppDB2Entities db = new DinerAppDB2Entities()) { //Menu cart = new Menu(); Menu menu = JsonConvert.DeserializeObject <Menu>(data); bool Exist = false; Exist = db.Menus.Any(r => r.menu_id.Equals(menu.menu_id)); if (Exist) { return(null); } if (Exist == false) { db.Menus.Add(menu); db.SaveChanges(); return(Ok(menu)); } return(Ok()); } }
//[Route("api/AddItem")] public IHttpActionResult PostAddItem([FromBody] string data) { using (DinerAppDB2Entities db = new DinerAppDB2Entities()) { Cart cart = new Cart(); CartDTO C = JsonConvert.DeserializeObject <CartDTO>(data); //gets menu_id from Menu var menu_id = (from item in db.Menus where C.name == item.name select item.menu_id).SingleOrDefault(); bool Exist = false; Exist = db.Carts.Any(r => r.menu_id.Equals(menu_id)); if (Exist) { var row = (from item in db.Carts where menu_id == item.menu_id select item).Single(); row.quantity += C.quantity; if (row.quantity > 20) { row.quantity = 20; } db.SaveChanges(); return(Ok(row.quantity)); } if (Exist == false) { cart = new Cart() { menu_id = menu_id, quantity = C.quantity }; db.Carts.Add(cart); db.SaveChanges(); return(Ok(cart)); } return(Ok()); } }
// [Route("api/Cart/{name}")] public IHttpActionResult UpdateCartItem([FromBody] string data) { using (DinerAppDB2Entities db = new DinerAppDB2Entities()) { var cart = JObject.Parse(data); var name = (string)cart["name"]; //gets the record that needs to be updated var item = (from listitem in db.Carts //join m in db.Menus on id equals m.menu_id into members where name == listitem.Menu.name select listitem).Single(); if (item == null) { return(NotFound()); } item.quantity = (int)cart["quantity"]; db.SaveChanges(); return(Ok(item.quantity)); } }
public IHttpActionResult DeleteCartItemByID(string name) { using (DinerAppDB2Entities db = new DinerAppDB2Entities()) { try { var row = (from item in db.Carts where name == item.Menu.name select item).Single(); db.Carts.Remove(row); db.SaveChanges(); return(Ok(row)); } catch (ArgumentOutOfRangeException e) { //log the exception e.Message return(BadRequest("The item is not in the Cart")); } } }
// [Route("api/Cart/{name}")] public IHttpActionResult UpdateMenuItem([FromBody] string data) { using (DinerAppDB2Entities db = new DinerAppDB2Entities()) { Menu menu = JsonConvert.DeserializeObject <Menu>(data); //gets the record that needs to be updated var item = (from listitem in db.Menus //join m in db.Menus on id equals m.menu_id into members where menu.name == listitem.name select listitem).Single(); if (item == null) { return(NotFound()); } item.cat_id = menu.cat_id; item.price = menu.price; db.SaveChanges(); return(Ok(menu)); } }