public IActionResult SaveItemsIngr(ItemIngredientViewModel i) { if (i.IsChoosed == true) { var k = _context.ingredientItems.Where(l => l.ItemId == i.ItemId).Where(f => f.IngredientId == i.IngredientId).FirstOrDefault(); if (k == null) { var n = new IngredientItem(); n.IngredientId = i.IngredientId; n.ItemId = i.ItemId; n.Quantity = i.Quantity; _context.Add(n); _context.SaveChanges(); } else { if (k.Quantity != i.Quantity) { k.Quantity = i.Quantity; _context.Update(k); _context.SaveChanges(); } } } else { var k = _context.ingredientItems.Where(l => l.ItemId == i.ItemId).Where(f => f.IngredientId == i.IngredientId).FirstOrDefault(); if (k != null) { _context.Remove(k); _context.SaveChanges(); } } return(Ok(i)); }
public IActionResult GetIngrs(int itemid, int branchid) { var allingrs = _context.Ingredients.Where(b => b.BranchId == branchid).ToList(); var ingrs = new List <ItemIngredientViewModel>(); foreach (var a in allingrs) { var i = new ItemIngredientViewModel(); i.ItemId = itemid; i.IngredientId = a.IngredientId; i.IngredientName = a.IngredientName; var exist = _context.ingredientItems.Where(l => l.ItemId == itemid).Where(q => q.IngredientId == a.IngredientId).FirstOrDefault(); if (exist == null) { i.IsChoosed = false; } else { i.IsChoosed = true; i.Quantity = exist.Quantity; } ingrs.Add(i); } return(Ok(ingrs)); }