public static RecipeDataModel CreateRecipe() { RecipeDataModel recipe = null; using (SQLiteConnection connection = DatabaseUtility.GetNewConnection()) { YeastIngredientDataModel yeastIngredient = YeastUtility.CreateYeastIngredient(connection); using (SQLiteCommand insertCommand = connection.CreateCommand()) { insertCommand.CommandText = "INSERT INTO Recipes (size, boilTime, name, beerStyleInfo, yeastIngredientInfo, mashProfileInfo) VALUES(0, 0, '', 0, @yeastIngredientInfo, 0)"; insertCommand.Parameters.AddWithValue("yeastIngredientInfo", yeastIngredient.YeastIngredientId); insertCommand.ExecuteNonQuery(); } recipe = new RecipeDataModel(DatabaseUtility.GetLastInsertedRowId(connection)) { YeastIngredient = yeastIngredient }; connection.Close(); } return recipe; }
public static BatchDataModel CreateBatch(RecipeDataModel recipe) { BatchDataModel batch = null; using (SQLiteConnection connection = DatabaseUtility.GetNewConnection()) { DateTime currentDate = DateTime.Now; using (SQLiteCommand insertCommand = connection.CreateCommand()) { insertCommand.CommandText = "INSERT INTO Batches (brewerName, assistantBrewerName, brewingDate, recipeInfo) VALUES ('', '', @brewingDate, @recipeInfo)"; insertCommand.Parameters.AddWithValue("brewingDate", currentDate.ToString()); insertCommand.Parameters.AddWithValue("recipeInfo", recipe.RecipeId); insertCommand.ExecuteNonQuery(); } batch = new BatchDataModel(DatabaseUtility.GetLastInsertedRowId(connection)) { BrewingDate = currentDate, Recipe = recipe }; connection.Close(); } return batch; }
public static void DeleteRecipe(RecipeDataModel recipe) { using (SQLiteConnection 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 (SQLiteCommand 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); RecipeDataModel recipe = new RecipeDataModel(recipeId) { Size = reader.GetFloat(1), BoilTime = reader.GetInt32(2), Name = reader.GetString(3), Style = recipeStyle, YeastIngredient = YeastUtility.GetYeastIngredientForRecipe(recipeId, connection) }; foreach (HopsIngredientDataModel hopsIngredient in HopsUtility.GetHopsIngredientsForRecipe(recipeId, connection)) { hopsIngredient.PropertyChanged += recipe.Ingredient_PropertyChanged; recipe.HopsIngredients.Add(hopsIngredient); } foreach (FermentableIngredientDataModel fermentableIngredient in FermentableUtility.GetFermentableIngredientsForRecipe(recipeId, connection)) { fermentableIngredient.PropertyChanged += recipe.Ingredient_PropertyChanged; recipe.FermentableIngredients.Add(fermentableIngredient); } yield return recipe; } } } connection.Close(); } }
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(); } }
private bool CanAddNewBatch(RecipeDataModel recipe) { return recipe != null; }
private void AddNewBatch(RecipeDataModel recipe) { SaveCurrentBatch(); CurrentBatch = BatchUtility.CreateBatch(recipe); SavedBatches.Add(CurrentBatch); }
private void DeleteRecipe(RecipeDataModel recipe) { RecipeUtility.DeleteRecipe(recipe); // set the current recipe to the previous recipe in the collection int previousRecipeIndex = SavedRecipes.IndexOf(recipe) - 1; CurrentRecipe = previousRecipeIndex == -1 ? SavedRecipes.FirstOrDefault() : SavedRecipes[previousRecipeIndex]; SavedRecipes.Remove(recipe); }