public void AddLunchPlan(LunchPlan lunchPlan) { string AddLunchPlanQuery = $"INSERT into LunchPlans (Week) VALUES (@Week)"; try { using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); if (conn.State == System.Data.ConnectionState.Open) { using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = AddLunchPlanQuery; cmd.Parameters.AddWithValue("@Week", lunchPlan.Week); cmd.ExecuteNonQuery(); } } } } catch (Exception eSql) { Debug.WriteLine("Exception: " + eSql.Message); } }
public List <LunchPlan> GetLunchPlansForWeek(int week) { List <LunchPlan> lunchPlansForWeek = new List <LunchPlan>(); string GetLunchPlanQuery = $"SELECT Id FROM LunchPlans WHERE Week = {week.ToString()}"; try { using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); if (conn.State == System.Data.ConnectionState.Open) { using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = GetLunchPlanQuery; using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { var lunchPlan = new LunchPlan(); lunchPlan.Id = reader.GetInt32(0); lunchPlan.Week = week; lunchPlansForWeek.Add(lunchPlan); } } } } } } catch (Exception eSql) { Debug.WriteLine("Exception: " + eSql.Message); } return(lunchPlansForWeek); }
/// <summary> /// Creates/grabs a lunchplan and calls a method, to add or update it, to the DB. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnSavePlan_Click_1(object sender, RoutedEventArgs e) { //List<string> mealsOfWeek = new List<string>(); //mealsOfWeek.Add(TBoxMonday.Text); //mealsOfWeek.Add(TBoxTuesday.Text); //mealsOfWeek.Add(TBoxWednesday.Text); //mealsOfWeek.Add(TBoxThursday.Text); //mealsOfWeek.Add(TBoxFriday.Text); LunchPlan lunchPlan = new LunchPlan(); //List<Meal> meals = new List<Meal>(); int currentWeekNumber = int.Parse(CmbBoxWeekNumbers.SelectedValue.ToString()); //If lunchplan exists, grabs it from model, and uses it to call method. if (model.LunchPlans.Any(l => l.Week == currentWeekNumber)) { lunchPlan = model.LunchPlans.Where(l => l.Week == currentWeekNumber).LastOrDefault(); CheckAndAddMealsVsLunchPlans(lunchPlan); } //If lunchplan doesn't exists, creates a new one, grabs the ID from the DB, and calls a method to add it to DB. //Note that using an SQL statement, that returns the ID at the same time as adding it, would probably be more clean. else { lunchPlan.Week = currentWeekNumber; lunchPlanHandler.AddLunchPlan(lunchPlan); lunchPlan.Id = lunchPlanHandler.GetLunchPlansForWeek(currentWeekNumber).FirstOrDefault().Id; model.LunchPlans.Add(lunchPlan); CheckAndAddMealsVsLunchPlans(lunchPlan); } }
public bool AddLunchPlan(LunchPlan lunchPlan) { try { DbAccess.AddLunchPlan(lunchPlan); Model.LunchPlans.Add(lunchPlan); return(true); } catch (Exception e) { Debug.WriteLine(e.Message); return(false); } }
public bool UpdateLunchPlan(LunchPlan lunchPlan) { try { DbAccess.UpdateLunchPlan(lunchPlan); var lp = Model.LunchPlans.Where(l => l.Id == lunchPlan.Id).FirstOrDefault(); lp.Week = lunchPlan.Week; return(true); } catch (Exception e) { Debug.WriteLine(e.Message); return(false); } }
/// <summary> /// Returns meals for the provided week /// </summary> /// <param name="week">int</param> /// <returns></returns> public List <ViewMealsVsLunchPlansJoin> GetMealsForWeek(int week) { LunchPlan lp = GetLunchPlanForWeek(week); List <MealsVsLunchPlans> mealsForWeek = Model.MealsVsLunchPlans.Where(mvsl => mvsl.LunchPlanId == lp.Id).ToList(); List <ViewMealsVsLunchPlansJoin> result = new List <ViewMealsVsLunchPlansJoin>(); foreach (MealsVsLunchPlans mvsl in mealsForWeek) { ViewMealsVsLunchPlansJoin meal = new ViewMealsVsLunchPlansJoin { Meal = Model.Meals.Where(m => m.Id == mvsl.MealId).FirstOrDefault().Description, Weekday = mvsl.Weekday }; result.Add(meal); } return(result); }
public void UpdateLunchPlan(LunchPlan lunchPlan) { string Id = lunchPlan.Id.ToString(); string week = lunchPlan.Week.ToString(); try { using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); using (SqlCommand command = new SqlCommand("UPDATE LunchPlans SET Week = @Week WHERE Id = @Id", conn)) { command.Parameters.AddWithValue("@Id", Id); command.Parameters.AddWithValue("@Week", week); command.ExecuteNonQuery(); } } } catch (Exception eSql) { Debug.WriteLine("Exception: " + eSql.Message); } }
public LunchPlan GetLunchPlanForWeek(int week) { LunchPlan lunchPlan = Model.LunchPlans.Where(l => l.Week == week).FirstOrDefault(); return(lunchPlan); }
public LunchPlan GetLunchPlan(int id) { LunchPlan lunchPlan = Model.LunchPlans.Where(m => m.Id == id).FirstOrDefault(); return(lunchPlan); }
public Model GetDataAndCreateModel() { const string GetAdminsQuery = "SELECT * from Admins"; const string GetLunchPlansQuery = "SELECT * from LunchPlans"; const string GetMessagesQuery = "SELECT * from Messages"; const string GetMealsQuery = "SELECT * from Meals"; const string GetMealsVsLunchPlansQuery = "SELECT * from MealsVsLunchPlans"; const string GetIpQuery = "SELECT * FROM DeviceIP"; var admins = new ObservableCollection <Admin>(); var lunchPlans = new ObservableCollection <LunchPlan>(); var messages = new ObservableCollection <Message>(); var meals = new ObservableCollection <Meal>(); var mealsVsLunchPlansCollection = new ObservableCollection <MealsVsLunchPlans>(); var ip = new ObservableCollection <IpAddress>(); try { using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); if (conn.State == System.Data.ConnectionState.Open) { using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = GetAdminsQuery; using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { var admin = new Admin(); admin.Id = reader.GetInt32(0); admin.Username = reader.GetString(1); admin.PasswordSalt = (byte[])reader.GetValue(2); admin.PasswordHash = (byte[])reader.GetValue(3); admins.Add(admin); } } cmd.CommandText = GetMessagesQuery; using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { var message = new Message(); message.Id = reader.GetInt32(0); message.AdminId = reader.GetInt32(1); message.Date = reader.GetDateTime(2); message.Text = reader.GetString(3); message.Header = reader.GetString(4); messages.Add(message); } } cmd.CommandText = GetMealsQuery; using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { var meal = new Meal(); meal.Id = reader.GetInt32(0); meal.Description = reader.GetString(1); meal.TimesChosen = reader.GetInt32(2); meals.Add(meal); } } cmd.CommandText = GetLunchPlansQuery; using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { var lunchPlan = new LunchPlan(); lunchPlan.Id = reader.GetInt32(0); lunchPlan.Week = reader.GetInt32(1); lunchPlans.Add(lunchPlan); } } cmd.CommandText = GetMealsVsLunchPlansQuery; using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { var mealsVsLunchPlans = new MealsVsLunchPlans(); mealsVsLunchPlans.Id = reader.GetInt32(0); mealsVsLunchPlans.LunchPlanId = reader.GetInt32(1); mealsVsLunchPlans.MealId = reader.GetInt32(2); mealsVsLunchPlans.Weekday = reader.GetString(3); mealsVsLunchPlansCollection.Add(mealsVsLunchPlans); } } cmd.CommandText = GetIpQuery; using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { var address = new IpAddress { Id = reader.GetInt32(0), Ip = reader.GetString(1) }; ip.Add(address); } } } } } } catch (Exception eSql) { Debug.WriteLine("Exception: " + eSql.Message); } Model model = new Model(admins, lunchPlans, messages, meals, mealsVsLunchPlansCollection, ip); return(model); }
/// <summary> /// Display lunchplan for given week and checks weither there's any duplicates in the DB, and if so, removes them. /// </summary> /// <param name="week">The week number of the lunchplan to display.</param> public void ShowSelectedLunchPlan(int week) { //If no meal is selected, calls function to make the buttons for adding meals to weekdays unavailable to click and greyed out. if (ListViewDatabaseDishes.SelectedIndex == -1) { AddingMealButtonAccessorChange(false); } //Not sure why this is done here, but unables, and greys out, the button that adds a meal to the DB from the searchfield. BtnAddDishToDB.IsHitTestVisible = false; BtnAddDishToDB.Opacity = 0.4; LunchPlan lunchPlan = new LunchPlan(); //If more than one lunchplan exists for the selected weeknumber, it's/they're removed from the DB. if (model.LunchPlans.Any(l => l.Week == week)) { if (model.LunchPlans.Where(l => l.Week == week).ToList().Count > 1) { for (int i = 0; i < model.LunchPlans.Where(l => l.Week == week).ToList().Count - 1; i++) { lunchPlanHandler.DeleteLunchPlan(model.LunchPlans.Where(l => l.Week == week).ToList()[i].Id); } } //Gets the remaining lunchplan. lunchPlan = model.LunchPlans.Where(l => l.Week == week).FirstOrDefault(); //Gets any mealvslunchplans that corresponds to the lunchplan, and adds them to a list. List <MealsVsLunchPlans> mealsVsLunchPlan = new List <MealsVsLunchPlans>(); mealsVsLunchPlan = model.MealsVsLunchPlans.Where(mvl => mvl.LunchPlanId == lunchPlan.Id).ToList(); int mealId = 0; List <MealsVsLunchPlans> mvsToDelete = new List <MealsVsLunchPlans>(); //Checks weither there's any mvsl for "monday", in the list of mvsl of the current lunchplan. if (mealsVsLunchPlan.Any(mvl => mvl.Weekday.ToLower() == "monday" || mvl.Weekday.ToLower() == "mandag")) { //If there is more than one, it removes the earlier entries from the DB. Prob ugly way to do it, but since lists are reference types, I decided to do it like this. if (mealsVsLunchPlan.Where(mvl => mvl.Weekday.ToLower() == "monday").ToList().Count > 1) { for (int i = 0; i < mealsVsLunchPlan.Where(mvl => mvl.Weekday.ToLower() == "monday").ToList().Count - 1; i++) { mvsToDelete.Add(mealsVsLunchPlan.Where(mvl => mvl.Weekday.ToLower() == "monday").ToList()[i]); lunchPlanHandler.DeleteMealVsLunchPlan(mvsToDelete.Last().Id); } } mealId = mealsVsLunchPlan.Where(mvl => mvl.Weekday.ToLower() == "monday" || mvl.Weekday.ToLower() == "mandag").LastOrDefault().MealId; TBoxMonday.Text = model.Meals.Where(m => m.Id == mealId).FirstOrDefault().Description; } //If there is no mvsl for the current weekday, sets the text of the textblock to an empty string. else { TBoxMonday.Text = ""; } if (mealsVsLunchPlan.Any(mvl => mvl.Weekday.ToLower() == "tuesday" || mvl.Weekday.ToLower() == "tirsdag")) { if (mealsVsLunchPlan.Where(mvl => mvl.Weekday.ToLower() == "tuesday").ToList().Count > 1) { for (int i = 0; i < mealsVsLunchPlan.Where(mvl => mvl.Weekday.ToLower() == "tuesday").ToList().Count - 1; i++) { mvsToDelete.Add(mealsVsLunchPlan.Where(mvl => mvl.Weekday.ToLower() == "tuesday").ToList()[i]); lunchPlanHandler.DeleteMealVsLunchPlan(mvsToDelete.Last().Id); } } mealId = mealsVsLunchPlan.Where(mvl => mvl.Weekday.ToLower() == "tuesday" || mvl.Weekday.ToLower() == "tirsdag").LastOrDefault().MealId; TBoxTuesday.Text = model.Meals.Where(m => m.Id == mealId).FirstOrDefault().Description; } else { TBoxTuesday.Text = ""; } if (mealsVsLunchPlan.Any(mvl => mvl.Weekday.ToLower() == "wednesday" || mvl.Weekday.ToLower() == "onsdag")) { if (mealsVsLunchPlan.Where(mvl => mvl.Weekday.ToLower() == "wednesday").ToList().Count > 1) { for (int i = 0; i < mealsVsLunchPlan.Where(mvl => mvl.Weekday.ToLower() == "wednesday").ToList().Count - 1; i++) { mvsToDelete.Add(mealsVsLunchPlan.Where(mvl => mvl.Weekday.ToLower() == "wednesday").ToList()[i]); lunchPlanHandler.DeleteMealVsLunchPlan(mvsToDelete.Last().Id); } } mealId = mealsVsLunchPlan.Where(mvl => mvl.Weekday.ToLower() == "wednesday" || mvl.Weekday.ToLower() == "onsdag").LastOrDefault().MealId; TBoxWednesday.Text = model.Meals.Where(m => m.Id == mealId).FirstOrDefault().Description; } else { TBoxWednesday.Text = ""; } if (mealsVsLunchPlan.Any(mvl => mvl.Weekday.ToLower() == "thursday" || mvl.Weekday.ToLower() == "torsdag")) { if (mealsVsLunchPlan.Where(mvl => mvl.Weekday.ToLower() == "thursday").ToList().Count > 1) { for (int i = 0; i < mealsVsLunchPlan.Where(mvl => mvl.Weekday.ToLower() == "thursday").ToList().Count - 1; i++) { mvsToDelete.Add(mealsVsLunchPlan.Where(mvl => mvl.Weekday.ToLower() == "thursday").ToList()[i]); lunchPlanHandler.DeleteMealVsLunchPlan(mvsToDelete.Last().Id); } } mealId = mealsVsLunchPlan.Where(mvl => mvl.Weekday.ToLower() == "thursday" || mvl.Weekday.ToLower() == "torsdag").LastOrDefault().MealId; TBoxThursday.Text = model.Meals.Where(m => m.Id == mealId).FirstOrDefault().Description; } else { TBoxThursday.Text = ""; } //If the friday is an even week, sets the text as "Fri" if (week % 2 == 0) { TBoxFriday.Text = "Fri"; } else { if (mealsVsLunchPlan.Any(mvl => mvl.Weekday.ToLower() == "friday" || mvl.Weekday.ToLower() == "fredag")) { if (mealsVsLunchPlan.Where(mvl => mvl.Weekday.ToLower() == "friday").ToList().Count > 1) { for (int i = 0; i < mealsVsLunchPlan.Where(mvl => mvl.Weekday.ToLower() == "friday").ToList().Count - 1; i++) { mvsToDelete.Add(mealsVsLunchPlan.Where(mvl => mvl.Weekday.ToLower() == "friday").ToList()[i]); lunchPlanHandler.DeleteMealVsLunchPlan(mvsToDelete.Last().Id); } } mealId = mealsVsLunchPlan.Where(mvl => mvl.Weekday.ToLower() == "friday" || mvl.Weekday.ToLower() == "fredag").LastOrDefault().MealId; TBoxFriday.Text = model.Meals.Where(m => m.Id == mealId).FirstOrDefault().Description; } else { TBoxFriday.Text = ""; } } //Removes the same mvsl that's been removed from the DB, from the Model. Pretty useless since model is refreshed in the end of the function. foreach (MealsVsLunchPlans mvs in mvsToDelete) { model.MealsVsLunchPlans.Remove(mvs); } } //If there is no lunchplan for the selected week, it sets the textblocks of the weekdays to empty, except for friday, if it's an even week. else { TBoxMonday.Text = ""; TBoxTuesday.Text = ""; TBoxWednesday.Text = ""; TBoxThursday.Text = ""; if (week % 2 == 0) { TBoxFriday.Text = "Fri"; } else { TBoxFriday.Text = ""; } } //Gets a new model by calling the DB. model = dbHandler.DbAccess.GetDataAndCreateModel(); }
// MAKE THIS BE ON TEXT CHANGED EVENT - ALSO CHANGE TBOX // ADD YEAR TO LUNCHPLAN? OR DELETE LUNCHPLANS EVERY YEAR - ADD TEXTBLOCK FOR ERRORMESSAGE. MAKE IT POSSIBLE TO ADD MEALS TO SEVERAL DAYS OF WEEK? /// <summary> /// Is called when a new lunchplan is saved. Checks weither the meals of the lunchplan exists in the DB or not, and adds to their TimesChosen property. /// </summary> /// <param name="lunchPlan">The lunchplan for which to check the mvsl and meals.</param> private void CheckAndAddMealsVsLunchPlans(LunchPlan lunchPlan) { List <MealsVsLunchPlans> mealsVsLunchPlansToDelete = new List <MealsVsLunchPlans>(); List <Meal> mealsToAddToMvS = new List <Meal>(); List <string> weekdaysForMealsToAddToMvS = new List <string>(); // Checks weither the textfield has any text in it. if (TBoxMonday.Text != "") { // Checks weither there already is a mealsvslunchplan where the week, weekday and meal is identical. If so, it does nothing. if (model.MealsVsLunchPlans.Any(mvsl => mvsl.LunchPlanId == lunchPlan.Id && mvsl.Weekday == "Monday") && TBoxMonday.Text.ToLower() == model.Meals.Where(m => m.Id == model.MealsVsLunchPlans.Where(mvsl => mvsl.LunchPlanId == lunchPlan.Id && mvsl.Weekday == "Monday").FirstOrDefault().MealId).FirstOrDefault().Description.ToLower()) { } else { Meal meal = new Meal(); //Checks to see if there is a meal corresponding to the textfield in the DB. If so, it adds 1 to its TimesChosen and updates it in the DB. if (model.Meals.Any(m => m.Description.ToLower() == TBoxMonday.Text.ToLower())) { meal = model.Meals.Where(m => m.Description.ToLower() == TBoxMonday.Text.ToLower()).FirstOrDefault(); meal.TimesChosen = meal.TimesChosen + 1; mealHandler.UpdateMeal(meal); } //If there is not a corresponding meal, it adds a new one to the DB. Also outputs it to the log TBlock. else { meal.Description = TBoxMonday.Text; meal.TimesChosen = 1; mealHandler.AddMeal(meal); TBlockConsoleLog.Text += $"\nRetten '{meal.Description}' er blevet tilføjet til databasen."; } //Adds the meal and the day of week to lists used for adding to mealvslunchplans. mealsToAddToMvS.Add(meal); weekdaysForMealsToAddToMvS.Add("Monday"); } } //If the textfield is empty, checks weither there's a mealvslunchplan saved for the corresponding week and weekday, if so, it deletes it from the DB. else { if (model.MealsVsLunchPlans.Any(mvs => mvs.LunchPlanId == lunchPlan.Id && mvs.Weekday == "Monday")) { lunchPlanHandler.DeleteMealVsLunchPlan(model.MealsVsLunchPlans.Where(mvs => mvs.LunchPlanId == lunchPlan.Id && mvs.Weekday == "Monday").FirstOrDefault().Id); } } if (TBoxTuesday.Text != "") { if (model.MealsVsLunchPlans.Any(mvsl => mvsl.LunchPlanId == lunchPlan.Id && mvsl.Weekday == "Tuesday") && TBoxTuesday.Text.ToLower() == model.Meals.Where(m => m.Id == model.MealsVsLunchPlans.Where(mvsl => mvsl.LunchPlanId == lunchPlan.Id && mvsl.Weekday == "Tuesday").FirstOrDefault().MealId).FirstOrDefault().Description.ToLower()) { } else { Meal meal = new Meal(); if (model.Meals.Any(m => m.Description.ToLower() == TBoxTuesday.Text.ToLower())) { meal = model.Meals.Where(m => m.Description.ToLower() == TBoxTuesday.Text.ToLower()).FirstOrDefault(); meal.TimesChosen = meal.TimesChosen + 1; mealHandler.UpdateMeal(meal); } else { meal.Description = TBoxTuesday.Text; meal.TimesChosen = 1; mealHandler.AddMeal(meal); TBlockConsoleLog.Text += $"\nRetten '{meal.Description}' er blevet tilføjet til databasen."; } mealsToAddToMvS.Add(meal); weekdaysForMealsToAddToMvS.Add("Tuesday"); } } else { if (model.MealsVsLunchPlans.Any(mvs => mvs.LunchPlanId == lunchPlan.Id && mvs.Weekday == "Tuesday")) { lunchPlanHandler.DeleteMealVsLunchPlan(model.MealsVsLunchPlans.Where(mvs => mvs.LunchPlanId == lunchPlan.Id && mvs.Weekday == "Tuesday").FirstOrDefault().Id); } } if (TBoxWednesday.Text != "") { if (model.MealsVsLunchPlans.Any(mvsl => mvsl.LunchPlanId == lunchPlan.Id && mvsl.Weekday == "Wednesday") && TBoxWednesday.Text.ToLower() == model.Meals.Where(m => m.Id == model.MealsVsLunchPlans.Where(mvsl => mvsl.LunchPlanId == lunchPlan.Id && mvsl.Weekday == "Wednesday").FirstOrDefault().MealId).FirstOrDefault().Description.ToLower()) { } else { Meal meal = new Meal(); if (model.Meals.Any(m => m.Description.ToLower() == TBoxWednesday.Text.ToLower())) { meal = model.Meals.Where(m => m.Description.ToLower() == TBoxWednesday.Text.ToLower()).FirstOrDefault(); meal.TimesChosen = meal.TimesChosen + 1; mealHandler.UpdateMeal(meal); } else { meal.Description = TBoxWednesday.Text; meal.TimesChosen = 1; mealHandler.AddMeal(meal); TBlockConsoleLog.Text += $"\nRetten '{meal.Description}' er blevet tilføjet til databasen."; } mealsToAddToMvS.Add(meal); weekdaysForMealsToAddToMvS.Add("Wednesday"); } } else { if (model.MealsVsLunchPlans.Any(mvs => mvs.LunchPlanId == lunchPlan.Id && mvs.Weekday == "Wednesday")) { lunchPlanHandler.DeleteMealVsLunchPlan(model.MealsVsLunchPlans.Where(mvs => mvs.LunchPlanId == lunchPlan.Id && mvs.Weekday == "Wednesday").FirstOrDefault().Id); } } if (TBoxThursday.Text != "") { if (model.MealsVsLunchPlans.Any(mvsl => mvsl.LunchPlanId == lunchPlan.Id && mvsl.Weekday == "Thursday") && TBoxThursday.Text.ToLower() == model.Meals.Where(m => m.Id == model.MealsVsLunchPlans.Where(mvsl => mvsl.LunchPlanId == lunchPlan.Id && mvsl.Weekday == "Thursday").FirstOrDefault().MealId).FirstOrDefault().Description.ToLower()) { } else { Meal meal = new Meal(); if (model.Meals.Any(m => m.Description.ToLower() == TBoxThursday.Text.ToLower())) { meal = model.Meals.Where(m => m.Description.ToLower() == TBoxThursday.Text.ToLower()).FirstOrDefault(); meal.TimesChosen = meal.TimesChosen + 1; mealHandler.UpdateMeal(meal); } else { meal.Description = TBoxThursday.Text; meal.TimesChosen = 1; mealHandler.AddMeal(meal); TBlockConsoleLog.Text += $"\nRetten '{meal.Description}' er blevet tilføjet til databasen."; } mealsToAddToMvS.Add(meal); weekdaysForMealsToAddToMvS.Add("Thursday"); } } else { if (model.MealsVsLunchPlans.Any(mvs => mvs.LunchPlanId == lunchPlan.Id && mvs.Weekday == "Thursday")) { lunchPlanHandler.DeleteMealVsLunchPlan(model.MealsVsLunchPlans.Where(mvs => mvs.LunchPlanId == lunchPlan.Id && mvs.Weekday == "Thursday").FirstOrDefault().Id); } } if (TBoxFriday.Text != "") { if (model.MealsVsLunchPlans.Any(mvsl => mvsl.LunchPlanId == lunchPlan.Id && mvsl.Weekday == "Friday") && TBoxFriday.Text.ToLower() == model.Meals.Where(m => m.Id == model.MealsVsLunchPlans.Where(mvsl => mvsl.LunchPlanId == lunchPlan.Id && mvsl.Weekday == "Friday").FirstOrDefault().MealId).FirstOrDefault().Description.ToLower()) { } else { if (TBoxFriday.Text != "Fri") { Meal meal = new Meal(); if (model.Meals.Any(m => m.Description.ToLower() == TBoxFriday.Text.ToLower())) { meal = model.Meals.Where(m => m.Description.ToLower() == TBoxFriday.Text.ToLower()).FirstOrDefault(); meal.TimesChosen = meal.TimesChosen + 1; mealHandler.UpdateMeal(meal); } else { meal.Description = TBoxFriday.Text; meal.TimesChosen = 1; mealHandler.AddMeal(meal); TBlockConsoleLog.Text += $"\nRetten '{meal.Description}' er blevet tilføjet til databasen."; } mealsToAddToMvS.Add(meal); weekdaysForMealsToAddToMvS.Add("Friday"); } } } else { if (model.MealsVsLunchPlans.Any(mvs => mvs.LunchPlanId == lunchPlan.Id && mvs.Weekday == "Friday")) { lunchPlanHandler.DeleteMealVsLunchPlan(model.MealsVsLunchPlans.Where(mvs => mvs.LunchPlanId == lunchPlan.Id && mvs.Weekday == "Friday").FirstOrDefault().Id); } } //Gets a new and updated model, from the DB. This can probably be avoided, if the model gets updated simultationsly to the DB, in the methodcalls above. //The issue is that the meals don't have IDs until they've been added to the DB. model = dbHandler.DbAccess.GetDataAndCreateModel(); int counter = 0; //Adds the IDs to the meals, then adds them to mvsl. foreach (Meal meal in mealsToAddToMvS) { meal.Id = model.Meals.Where(m => m.Description == meal.Description).FirstOrDefault().Id; AddMealsVsLunchPlans(lunchPlan.Id, meal.Id, weekdaysForMealsToAddToMvS[counter]); counter++; } //Refreshes the list of meals, since some might've been added, and others might've gotten a higher number of TimesChosen, than some. ListViewDatabaseDishes.ItemsSource = model.Meals.OrderByDescending(m => m.TimesChosen); //Refreshes the searchbox to trigger the TextChanged method. string searchedText = TBoxSearchField.Text; TBoxSearchField.Text = ""; TBoxSearchField.Text = searchedText; //Outputs to the log TBlock TBlockConsoleLog.Text += $"\nMadplanen er blevet gemt i databasen."; //Refreshes the Model again. (The model calls the DB). There should be no difference between this way of doing it, and the one done a bit earlier. model = new Model(); //Calls the method that displays the lunchplan. ShowSelectedLunchPlan((int)CmbBoxWeekNumbers.SelectedItem); }