private void DeletePhaseExecute(object obj) { if (obj is Phase phase) { RecipePhases.Remove(phase); } }
private void PositionUpPhaseExecute(object obj) { if (obj is Phase phase) { var upperPhase = RecipePhases.ElementAt(phase.Position + 1); upperPhase.Position = phase.Position; phase.Position += 1; RecipePhases = new ObservableCollection <Phase>(RecipePhases.OrderBy(_ => _.Position)); } }
public AddRecipeViewModel() { #region Init Commands #region Init Phase - Commands AddPhaseCommand = new RelayCommand(AddPhaseExecute); DuplicatePhaseCommand = new RelayCommand(DuplicatePhaseExecute); DeletePhaseCommand = new RelayCommand(DeletePhaseExecute); PositionUpPhaseCommand = new RelayCommand(PositionUpPhaseExecute, PositionUpPhaseCanExecute); PositionDownPhaseCommand = new RelayCommand(PositionDownPhaseExecute, PositionDownPhaseCanExecute); AddPumpIntervalCommand = new RelayCommand(AddPumpIntervalExecute); #endregion #region Init Ingredient - Commands AddIngredientCommand = new RelayCommand(AddIngredientExecute); DuplicateIngredientCommand = new RelayCommand(DuplicateIngredientExecute); DeleteIngredientCommand = new RelayCommand(DeleteIngredientExecute); #endregion SaveRecipeCommand = new RelayCommand(SaveRecipeExecute, SaveRecipeCanExecute); #endregion using var db = new BreweryContext(); PumpIntervals = new ObservableCollection <PumpInterval>(db.PumpIntervals.ToList()); foreach (var ingredient in db.FoundationIngrediets.ToList()) { RecipeIngredients.Add(new Ingredient { Name = ingredient.Name, Amount = ingredient.Amount, Typ = ingredient.Typ }); } RecipePhases.Add(new Phase { Position = 0, Name = "Aufwärmen", PeriodOfMinutes = 60, TargetTemperature = 50, Typ = PhaseTyp.HeatUp, PumpInterval = PumpIntervals[0] }); RecipePhases.Add(new Phase { Position = 1, Name = "Halten", PeriodOfMinutes = 60, TargetTemperature = 50, Typ = PhaseTyp.KeepHeat, PumpInterval = PumpIntervals[2] }); RecipePhases.Add(new Phase { Position = 2, Name = "Aufwärmen 2", PeriodOfMinutes = 60, TargetTemperature = 70, Typ = PhaseTyp.HeatUp, PumpInterval = PumpIntervals[2] }); RecipePhases.Add(new Phase { Position = 3, Name = "Rast 2", PeriodOfMinutes = 60, TargetTemperature = 70, Typ = PhaseTyp.KeepHeat, PumpInterval = PumpIntervals[2] }); }
private void AddPhaseExecute(object obj) { if (obj is Phase phase) { Phase newPhase = new Phase { Position = RecipePhases.Count, Name = phase.Name, PeriodOfMinutes = phase.PeriodOfMinutes, TargetTemperature = phase.TargetTemperature, Typ = phase.Typ, PumpInterval = phase.PumpInterval }; RecipePhases.Add(newPhase); } else { int targetTemperature = 0; if (RecipePhases.Count > 0) { targetTemperature = RecipePhases[RecipePhases.Count - 1].TargetTemperature; } RecipePhases.Add(new Phase { Position = RecipePhases.Count, Name = "", PeriodOfMinutes = 0, TargetTemperature = targetTemperature, Typ = PhaseTyp.KeepHeat, PumpInterval = PumpIntervals[0] }); } }
private void SaveRecipeExecute(object obj) { using var db = new BreweryContext(); //Rezept neu if (recipeId == 0) { foreach (var phase in RecipePhases) { var pumpIntervall = db.PumpIntervals.FirstOrDefault(p => p.ID == phase.PumpInterval.ID); if (pumpIntervall != null) { phase.PumpInterval = pumpIntervall; phase.PumpIntervalID = pumpIntervall.ID; } } Recipe recipe = new Recipe() { Description = RecipeDescription, Name = RecipeName, Ingredients = new List <Ingredient>(RecipeIngredients), Phases = new List <Phase>(RecipePhases), IsFavorite = false }; db.Recipes.Add(recipe); db.SaveChanges(); } else { //Rezept schon vorhanden var recipe = db.Recipes.FirstOrDefault(r => r.ID == recipeId); if (recipe != null) { foreach (var phase in RecipePhases) { var pumpIntervall = db.PumpIntervals.FirstOrDefault(p => p.ID == phase.PumpInterval.ID); if (pumpIntervall != null) { phase.PumpInterval = pumpIntervall; phase.PumpIntervalID = pumpIntervall.ID; } } recipe.Description = RecipeDescription; recipe.Name = RecipeName; recipe.Ingredients = new List <Ingredient>(RecipeIngredients); recipe.Phases = new List <Phase>(RecipePhases); recipe.IsFavorite = false; db.Entry(recipe).State = EntityState.Modified; db.SaveChanges(); //Löschen der nicht mehr genutzten Ingredients in der DB var dbRecipeIngredients = db.Ingredients.Where(i => i.RecipeID == recipe.ID); foreach (var dbRecipeIngredient in dbRecipeIngredients) { var item = RecipeIngredients.FirstOrDefault(i => i.ID == dbRecipeIngredient.ID); if (item == null) { db.Ingredients.Remove(dbRecipeIngredient); db.SaveChanges(); } } //Löschen der nicht mehr genutzten Phases in der DB var dbRecipePhases = db.Phases.Where(i => i.RecipeID == recipe.ID); foreach (var dbRecipePhase in dbRecipePhases) { var item = RecipePhases.FirstOrDefault(i => i.ID == dbRecipePhase.ID); if (item == null) { db.Phases.Remove(dbRecipePhase); db.SaveChanges(); } } } } }