private void FillOrRefreshTreeOfRecipes() { recipesTreeView.Nodes.Clear(); var recipes = new RecipeRepository(_connectionString).GetAll(); foreach (var recipe in recipes) { TreeNode dishNode = new TreeNode(); if (recipesTreeView.Nodes.ContainsKey(recipe.DishID.ToString())) { dishNode = recipesTreeView.Nodes.Find(recipe.DishID.ToString(), false).First(); } else { var dishName = new DishRepository(_connectionString).GetById(recipe.DishID)?.DishName; dishNode = recipesTreeView.Nodes.Add(recipe.DishID.ToString(), dishName); } foreach (var pair in recipe.IngredientIdsAndQuantities) { var ingr = new IngredientRepository(_connectionString).GetById(pair.Key); var unitName = new UnitRepository(_connectionString).GetAll().FirstOrDefault(x => x.UnitID == ingr.UnitID).UnitName; dishNode.Nodes.Add(ingr.IngredientName + " " + pair.Value + " " + unitName); } } }
private void editRecipe_Click(object sender, EventArgs e) { var dishName = recipesTreeView.SelectedNode.Text; var dishID = new DishRepository(_connectionString).GetAll().First(x => x.DishName == dishName).DishID; var recipe = new RecipeRepository(_connectionString).GetById(dishID); recipe.Dishes = new DishRepository(_connectionString).GetAll(); recipe.Ingredients = new IngredientRepository(_connectionString).GetAll(); recipe.Units = new UnitRepository(_connectionString).GetAll(); var dialogResult = new AddOrEditRecipeForm(recipe, _connectionString).ShowDialog(this); if (dialogResult == DialogResult.Cancel) { FillOrRefreshTreeOfRecipes(); } }
public AddOrEditRecipeForm(Recipe recipe, string connString) { InitializeComponent(); _connectionString = connString; _recipe = recipe; if (_recipe.DishID == 0) { Text = "Добавить рецепт"; var existingRecipes = new RecipeRepository(_connectionString).GetAll().Select(x => x.DishID); comboBoxDishes.DataSource = _recipe.Dishes.Where(x => !existingRecipes.Contains(x.DishID)).Select(d => d.DishName).OrderBy(x => x).ToList(); } else { Text = "Редактировать рецепт"; comboBoxDishes.Enabled = false; comboBoxDishes.Text = new DishRepository(_connectionString).GetById(recipe.DishID).DishName; FillListBox(); } }