public static void SaveRecipe(RecipeDataModel recipe) { using SQLiteConnection connection = DatabaseUtility.GetNewConnection(); using SQLiteCommand updateRecipeCommand = connection.CreateCommand(); updateRecipeCommand.CommandText = "UPDATE Recipes SET size = @size, boilTime = @boilTime, name = @name, yeastIngredientInfo = @yeastIngredientInfo, beerStyleInfo = (SELECT id FROM Styles WHERE name = @beerStyleName) " + "WHERE id = @id"; updateRecipeCommand.Parameters.AddWithValue("id", recipe.RecipeId); updateRecipeCommand.Parameters.AddWithValue("size", recipe.Size); updateRecipeCommand.Parameters.AddWithValue("boilTime", recipe.BoilTime); updateRecipeCommand.Parameters.AddWithValue("name", recipe.Name); updateRecipeCommand.Parameters.AddWithValue("yeastIngredientInfo", recipe.YeastIngredient != null ? ((YeastIngredientDataModel)recipe.YeastIngredient).YeastIngredientId : 0); updateRecipeCommand.Parameters.AddWithValue("beerStyleName", recipe.Style != null ? recipe.Style.Name : ""); updateRecipeCommand.ExecuteNonQuery(); if (recipe.YeastIngredient != null) { YeastUtility.UpdateYeastIngredient((YeastIngredientDataModel)recipe.YeastIngredient, connection); } foreach (FermentableIngredientDataModel fermentableIngredient in recipe.FermentableIngredients) { FermentableUtility.UpdateFermentableIngredient(fermentableIngredient, connection); } foreach (HopsIngredientDataModel hopsIngredient in recipe.HopsIngredients) { HopsUtility.UpdateHopsIngredient(hopsIngredient, connection); } connection.Close(); }
public static IEnumerable <IngredientTypeBase> GetAvailableIngredients() { var ingredients = new List <IngredientTypeBase>(); using SQLiteConnection connection = DatabaseUtility.GetNewConnection(); ingredients.AddRange(HopsUtility.GetAvailableHopsVarieties(connection)); ingredients.AddRange(FermentableUtility.GetAvailableFermentables(connection)); ingredients.AddRange(YeastUtility.GetAvailableYeasts(connection)); connection.Close(); return(ingredients); }
public static void DeleteRecipe(RecipeDataModel recipe) { using var connection = DatabaseUtility.GetNewConnection(); if (recipe.YeastIngredient != null) { YeastUtility.DeleteYeastIngredient(((YeastIngredientDataModel)recipe.YeastIngredient).YeastIngredientId, connection); } foreach (FermentableIngredientDataModel fermentableIngredient in recipe.FermentableIngredients) { FermentableUtility.DeleteFermentableIngredient(fermentableIngredient.FermentableId, connection); } foreach (HopsIngredientDataModel hopsIngredient in recipe.HopsIngredients) { HopsUtility.DeleteHopsIngredient(hopsIngredient.HopsId, connection); } using var deleteRecipeCommand = connection.CreateCommand(); deleteRecipeCommand.CommandText = "DELETE FROM Recipes WHERE id = @id"; deleteRecipeCommand.Parameters.AddWithValue("id", recipe.RecipeId); deleteRecipeCommand.ExecuteNonQuery(); }
public static IEnumerable <RecipeDataModel> GetSavedRecipes(IList <Style> availableBeerStyles) { using SQLiteConnection connection = DatabaseUtility.GetNewConnection(); using SQLiteCommand getRecipesCommand = connection.CreateCommand(); getRecipesCommand.CommandText = "SELECT Recipes.id, Recipes.size, Recipes.boilTime, Recipes.name, Styles.name FROM Recipes " + "LEFT JOIN Styles ON Styles.id = Recipes.beerStyleInfo"; using SQLiteDataReader reader = getRecipesCommand.ExecuteReader(); while (reader.Read()) { string styleName = reader[4].ToString(); Style recipeStyle = availableBeerStyles.FirstOrDefault(style => style.Name == styleName); int recipeId = reader.GetInt32(0); var recipe = new RecipeDataModel(recipeId) { Size = reader.GetFloat(1), BoilTime = reader.GetInt32(2), Name = reader.GetString(3), Style = recipeStyle, YeastIngredient = YeastUtility.GetYeastIngredientForRecipe(recipeId, connection) }; foreach (var hopsIngredient in HopsUtility.GetHopsIngredientsForRecipe(recipeId, connection)) { hopsIngredient.PropertyChanged += recipe.Ingredient_PropertyChanged; recipe.HopsIngredients.Add(hopsIngredient); } foreach (var fermentableIngredient in FermentableUtility.GetFermentableIngredientsForRecipe(recipeId, connection)) { fermentableIngredient.PropertyChanged += recipe.Ingredient_PropertyChanged; recipe.FermentableIngredients.Add(fermentableIngredient); } yield return(recipe); } connection.Close(); }