private static DayEntry SelectDayEntry(DateTime date, out int affected) //selects one DayEntry from DB based on Date given { affected = 0; SqlParameter[] sparams = new SqlParameter[] { new SqlParameter("@DayDate", date.ToString("yyyy/MM/dd")) }; DataRowCollection rows = DBHelper.selectStatement("SELECT m.MealID FROM Meals m WHERE m.TimeEaten=@DayDate;", sparams); //selects Meals based on DayEaten if (rows.Count == 0) //If No day(meal entries for the Date) is found Stop/do Nothing { return(null); } DayEntry day = new DayEntry(); day._date = date; int affmeal; foreach (DataRow row in rows) //adds Meals data into MealEntries for this newly made DayEntry { day.addMealEntry(SelectMealEntry(((int)row["MealID"]), out affmeal)); affected += affmeal; } rows = DBHelper.selectStatement("SELECT n.Score, n.NoteTExt FROM Notes n WHERE n.OfDay=@DayDate;", sparams); //selects Note data for this day from Notes if (rows.Count == 1) //creates Note for this DayEntry is data was found { day._note = new Note(rows[0]["Score"] == DBNull.Value ? "" : rows[0]["Score"].ToString(), rows[0]["NoteText"] == DBNull.Value ? "" : rows[0]["NoteText"].ToString()); affected++; } day.setCalculatedValues(); //calculate the final food values return(day); }
private void button_addFood_Click(object sender, EventArgs e) //adds a new food(or references from Foods through the process of FoodEntry init) to selected meal { MealEntry meal = comboBox_meals.SelectedItem as MealEntry; if (meal == null) { MessageBox.Show("No Meal Selected!"); return; } if (textBox_amount.Text == "") //checks amount { MessageBox.Show("Needs Amount!"); return; } FoodItem food = null; if (radio_list.Checked) //adds food from the list of all foods { food = (FoodItem)listBox_foodItems.SelectedItem; if (food != null) { meal.addFoodEntry(new FoodEntry(food, Convert.ToDouble(textBox_amount.Text))); } else { MessageBox.Show("Invalid Selection for Food!"); } textBox_amount.Clear(); textBox_searchFood.Clear(); textBox_searchFood.Focus(); } else if (radio_pattern.Checked) //adds food by matching a string(similar to one written file format) { if (MainForm.regexEntry.Match(textBox_pattern.Text).Success) { meal.addFoodEntry(new FoodEntry(textBox_pattern.Text)); food = _day._mealEntries[comboBox_meals.SelectedIndex]._foodEntries.Last()._food; } else { MessageBox.Show("Invalid Pattern for Food!"); } textBox_pattern.Clear(); textBox_pattern.Focus(); } else { //adds new food by creating it with values given by user through custom textboxes try { food = new FoodItem(textBox_foodName.Text, textBox_brand.Text, Convert.ToDouble(textBox_fat.Text), Convert.ToDouble(textBox_carbs.Text), Convert.ToDouble(textBox_protein.Text), (Measure)comboBox_measure.SelectedIndex); meal.addFoodEntry(new FoodEntry(food, Convert.ToDouble(textBox_amount.Text))); } catch (Exception ex) { MessageBox.Show($"Invalid Entry Values! {ex.Message}"); } textBox_foodName.Clear(); textBox_brand.Clear(); textBox_fat.Clear(); textBox_carbs.Clear(); textBox_protein.Clear(); textBox_foodName.Focus(); } if (food != null) //updates MealEntry and FoodEntry calculated values(does the math for macros) { ((MealEntry)comboBox_meals.SelectedItem).setCalculatedValues(); _day.setCalculatedValues(); } updateDay(); }