public void Post(string cookId, [FromBody] ShoppingListItem value) { if (Guid.TryParse(cookId, out Guid _cookId) && ControllerHelper.CookExists(_cookId)) { if (ControllerHelper.GetShoppingList(_cookId).Any(sli => ControllerHelper.TypeEquals(sli.Ingredient, value.Ingredient))) { // Ingredient exists in the shopping list => update Ingredient ingToUpdate = ControllerHelper.GetShoppingList(_cookId).First(sli => ControllerHelper.TypeEquals(sli.Ingredient, value.Ingredient)).Ingredient; if (ingToUpdate.Unit != value.Ingredient.Unit) { ingToUpdate.Quantity += UnitHelper.ConvertTo(value.Ingredient.Quantity, Enum.Parse <Unit>(value.Ingredient.Unit), Enum.Parse <Unit>(ingToUpdate.Unit)); } else { ingToUpdate.Quantity += value.Ingredient.Quantity; } } else { // New ingredient top add => create value.Id = Guid.NewGuid(); ControllerHelper.GetShoppingList(_cookId).Add(value); } } }
public void Post(string cookId, [FromBody] Ingredient value) { if (Guid.TryParse(cookId, out Guid _cookId) && ControllerHelper.CookExists(_cookId)) { if (ControllerHelper.GetInventory(_cookId).Any(ing => ControllerHelper.TypeEquals(ing, value))) { // Ingredient exists in the inventory => update Ingredient ingToUpdate = ControllerHelper.GetInventory(_cookId).First(ing => ControllerHelper.TypeEquals(ing, value)); if (ingToUpdate.Unit != value.Unit) { ingToUpdate.Quantity += UnitHelper.ConvertTo(value.Quantity, Enum.Parse <Unit>(value.Unit), Enum.Parse <Unit>(ingToUpdate.Unit)); } else { ingToUpdate.Quantity += value.Quantity; } } else { // New ingredient top add => create ControllerHelper.GetInventory(_cookId).Add(value); } } }