public List <object> BrowseRecipeIngredients(int selectedRecipeId) { List <object> allRecipeIngredients = new List <object>(); using (MySqlConnection connection = GetMySqlConnection()) { try { connection.Open(); string queryRecipeIngredients = "SELECT i.name AS 'Ingredient', ri.Id AS 'RI Id', ri.amount AS 'Amount', mu.name AS 'Unit of Measure' FROM recipes r JOIN recipe_ingredients ri on r.id = ri.recipeId JOIN ingredients i on i.id = ri.ingredientId LEFT OUTER JOIN measures mu on mu.id = measureId WHERE r.id = @Id;"; MySqlCommand getIngredients = new MySqlCommand(queryRecipeIngredients, connection); getIngredients.Parameters.Add(new MySqlParameter("Id", selectedRecipeId)); MySqlDataReader ingredientsReader; ingredientsReader = getIngredients.ExecuteReader(); while (ingredientsReader.Read()) { String ingredientName = ingredientsReader.GetString(0); int Id = ingredientsReader.GetInt32(1); int amount = ingredientsReader.GetInt32(2); String measure = (ingredientsReader.IsDBNull(3)) ? "" : ingredientsReader.GetString(3); //if(!ingredientsReader.IsDBNull(2)) // create instance of RecipeStepsUIModel and add it to a list or array var recipeIngredient = new RecipeIngredientItem { Id = Id, ingredientName = ingredientName, amount = amount, measure = measure }; allRecipeIngredients.Add(recipeIngredient); // recipeStepList.push(recipeStep) //Console.WriteLine("Step #: {0} | Step Instruction: {1}", stepNumber, stepInstruction); } ingredientsReader.Close(); connection.Close(); //return new object[] { }; return(allRecipeIngredients); } catch (Exception e) { Console.WriteLine("Error connecting to database: " + e.Message); //return new object[] { }; return(new List <object> { }); } } }
public object ReadRecipeIngredient(RecipeIngredientItem recipeIngredientItem) { if (recipeIngredientItem == null) { return(false); } using (var context = new CookBookContext()) { try { return(context.Ingredients.FirstOrDefault(e => e.name == recipeIngredientItem.ingredientName)); } catch (Exception ex) { Console.WriteLine("Could not read Recipe Ingredient"); Console.WriteLine(ex.Message); return(null); } } }