public void InsertOrUpdate(Menu menu) { if (menu.Id == default(int)) { // New entity context.Menus.Add(menu); } else { // Existing entity context.Entry(menu).State = EntityState.Modified; } }
protected List<Menu> FillMenu(List<Item> items, int day) { List<Menu> result = new List<Menu>(); int order = 0; foreach (var item in items) { item.Order = order; order++; }; // TODO: get template for menu from DB List<Item> copy_items = new List<Item>(); copy_items = items.ConvertAll(item => (Item)item.Clone()); Menu daymenu = new Menu() { Name = "Полный обед", Price = 35000, Items = copy_items, OnDate = _monday.AddDays(day), Type = MenuType.NormalMenu, }; result.Add(daymenu); copy_items = new List<Item>(); copy_items = items.ConvertAll(item => (Item)item.Clone()); if (copy_items.Count > 2) { copy_items.RemoveAt(1); } daymenu = new Menu() { Name = "Без первого", Price = 30000, Items = copy_items, OnDate = _monday.AddDays(day), Type = MenuType.NormalMenu, }; result.Add(daymenu); return result; }
public IHttpActionResult PostMenu(Menu menu) { if (!ModelState.IsValid) { return BadRequest(ModelState); } db.Menus.Add(menu); db.SaveChanges(); return CreatedAtRoute("DefaultApi", new { id = menu.Id }, menu); }
public IHttpActionResult PutMenu(int id, Menu menu) { if (menu.Items != null) { foreach (var item in menu.Items) { if (item.Id == 0) { db.Items.Add(item); db.SaveChanges(); } } ModelState.Clear(); } if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != menu.Id) { return BadRequest(); } foreach (var item in menu.Items) { item.Ratings = null; db.Entry(item).State = EntityState.Modified; } db.Entry(menu).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!MenuExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }