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 static int InsertDay(DayEntry day) //insert one Day Entry data into DB { int affected = 0; if (day._note.ToString() != "") //attempts to select This Days Note the one tasked to be inserted) from Notes table { SqlParameter[] sparams = new SqlParameter[] { new SqlParameter("@UserID", 1), new SqlParameter("@OfDay", day._date.ToString("yyyy/MM/dd")) }; DataRowCollection rows = DBHelper.selectStatement("SELECT * FROM Notes WHERE UserID=@UserID AND OfDay=@OfDay;", sparams); //if day already exists do nothing if (rows.Count == 0) //if note not found inside table.. { sparams = new SqlParameter[] { new SqlParameter("@UserID", 1), new SqlParameter("@Title", "N"), new SqlParameter("@Score", day._note.gradeAverage()), new SqlParameter("@NoteText", day._note._text), new SqlParameter("@OfDay", day._date.ToString("yyyy/MM/dd")) }; affected += DBHelper.insertStatement("INSERT INTO Notes VALUES(@UserID, @Title, @Score, @NoteText, @OfDay);", sparams); //insert Note for this Day into Notes with This Days Date } } foreach (MealEntry meal in day._mealEntries) //insert Meals for this Day { affected += InsertMeal(meal, day._date); } return(affected); }
public AddDayForm(DateTime date) //init this form using the date selected from calendar in MainForm { InitializeComponent(); _daysPrevious = new List <DayEntry>(); foreach (DayEntry day in MainForm.Days) //searches for DayEntry in Days global list and clones it to set values for this _day(which is temporary until commit) { if (day._date == date) { _day = day.Clone(); break; } } if (_day == null) //creates new DayEntry to add later if not found in Days { _day = new DayEntry(); _day._date = date; } textBox_noteScore.Text = _day._note._grade; textBox_noteText.Text = _day._note._text; comboBox_measure.SelectedIndex = 0; listBox_foodItems.Items.AddRange(MainForm.Foods.ToArray()); //populated listbox with items from Foods if (listBox_foodItems.Items.Count != 0) { listBox_foodItems.SelectedIndex = 0; } comboBox_meals.DataSource = new BindingSource(_day._mealEntries, ""); //populates combobox with meals from current day with bindingsource for detect changes comboBox_meals.ValueMember = "_name"; updateDay(); //calls method that updates various UI things to corespond with changed data }
private void button_cancel_Click(object sender, EventArgs e) => Close(); //closes form without comitting any changes private void button_undo_Click(object sender, EventArgs e) //undo the last change made to current _day state; can revert back up to when form 1st opened(no changes) { //TODO: OPTIMIZE/Figure out better way(just use list?) Later! if (_daysPrevious.Count > 1) { _daysPrevious.Remove(_daysPrevious.Last()); //removes last(same as current) day object from list _day = _daysPrevious.Last().Clone(); //sets current day as a New DayEntry object with 2nd to last values(cloning from _daysPrevious list to avoid values changing in list also because of the reference) comboBox_meals.DataSource = new BindingSource(_day._mealEntries, ""); //updates selected meal(in case meal get undone) if (_day._mealEntries.Count > 0) { comboBox_meals.SelectedItem = _day._mealEntries.Last(); } //resets UI values without calling updateDay() to dupe values don't show up output.Clear(); output.AppendText(_day.ToString() + "\n\n"); label_title.Text = $"DAY: {_day._date.ToString("dd/MM/yyyy")}"; label_subtitle.Text = _day._note.ToStringInfo(); textBox_activ.Text = _day._activ; this.Update(); } else { MessageBox.Show("Cannot Undo Further!"); //initial state on form open } }
List <DayEntry> _daysPrevious; //previous states of _day for undoing changes #region Constructors public AddDayForm() //init new day(with Now) { InitializeComponent(); _day = new DayEntry(); _daysPrevious = new List <DayEntry>(); comboBox_measure.SelectedIndex = 0; listBox_foodItems.Items.AddRange(MainForm.Foods.ToArray()); listBox_foodItems.ValueMember = "ToStringLite()"; if (listBox_foodItems.Items.Count != 0) { listBox_foodItems.SelectedIndex = 0; } comboBox_meals.DataSource = new BindingSource(_day._mealEntries, ""); comboBox_meals.ValueMember = "_name"; updateDay(); }
public DayEntry Clone() //return a New copy of this object with the same values { List <MealEntry> ml = new List <MealEntry>(); foreach (var meal in this._mealEntries) { List <FoodEntry> fl = new List <FoodEntry>(); foreach (var fentry in meal._foodEntries) { fl.Add(new FoodEntry(fentry._food, fentry._amount)); } MealEntry newm = new MealEntry(fl, new Note(meal._note._grade, meal._note._text), meal._fatEntry, meal._carbsEntry, meal._proteinEntry, meal._portion); newm._name = meal._name; ml.Add(newm); } DayEntry clone = new DayEntry(this._date, ml, new Note(this._note._grade, this._note._text), this._fatEntry, this._carbsEntry, this._proteinEntry); clone.setActiv(this._activ); return(clone); }
public static List <DayEntry> SelectDayEntries(DateTime start, DateTime end, out int affected) //selects multiple DayEntries from DB between 2 given dates { DBHelper.conn.Open(); affected = 0; List <DayEntry> dayList = new List <DayEntry>(); //list with DayEntries loaded DayEntry day = null; DateTime current = start; //current date to check as going through the range while (current < end) { day = SelectDayEntry(current, out int affDay); if (day != null) //if no entries were found with the current date { dayList.Add(day); } affected += affDay; current = current.AddDays(1); } DBHelper.conn.Close(); return(dayList); }