Ejemplo n.º 1
0
        private static MealEntry SelectMealEntry(int MealID, out int affected)  //select MealEntry from DB based on MealID given
        {
            affected = 0;

            SqlParameter[]    sparams = new SqlParameter[] { new SqlParameter("@MealID", MealID) };
            DataRowCollection rows    = DBHelper.selectStatement("SELECT m.MealName, m.Portion, n.Score, n.NoteText " +
                                                                 "FROM Meals m LEFT JOIN Notes n ON m.NoteID=n.NoteID WHERE m.MealID=@MealID", sparams); //select Meal data for this Meal(based on ID) and its associated Note data

            if (rows.Count == 0)                                                                                                                         //If No meal is found Stop/do Nothing
            {
                return(null);
            }

            //create MealEntry object and populate some of its data
            MealEntry meal = new MealEntry();

            meal._name    = rows[0]["MealName"].ToString();
            meal._portion = (double)rows[0]["Portion"];
            meal._note    = new Note(rows[0]["Score"] == DBNull.Value ? "" : rows[0]["Score"].ToString(),
                                     rows[0]["NoteText"] == DBNull.Value ? "" : rows[0]["NoteText"].ToString());
            affected++;

            rows = DBHelper.selectStatement("SELECT f.FoodID, f.IsDish, f.FoodName, f.Brand, f.Fat, f.Carbs, f.Protein, f.SizeInfo, md.Amount " +
                                            "FROM FoodItems f JOIN MealData md ON f.FoodID=md.FoodID WHERE md.MealID=@MealID;", sparams); //select FoodItems associated with this MealID from MealData
            foreach (DataRow row in rows)                                                                                                 //add the rows information as FoodEntry object to this newly made MealEntry
            {
                if ((bool)row["IsDish"])                                                                                                  //if the food found is a Dish create a Dish and populate its data
                {
                    Dish foodDish = new Dish(row["FoodName"].ToString(), row["Brand"].ToString(),
                                             (double)row["Fat"], (double)row["Carbs"], (double)row["Protein"],
                                             row["SizeInfo"] == DBNull.Value ? Measure.Grams : Measure.Pieces);

                    SqlParameter[]    sparamsD = new SqlParameter[] { new SqlParameter("@DishID", row["FoodID"]) };
                    DataRowCollection rowsD    = DBHelper.selectStatement("SELECT f.FoodName, f.Brand, f.Fat, f.Carbs, f.Protein, f.SizeInfo, dd.Amount " +
                                                                          "FROM FoodItems f JOIN DishData dd ON f.FoodID=dd.IngredientID WHERE dd.DishID=@DishID;", sparamsD); //select FoodItems associated with this FoodIF(DishID in DishData) from DishData
                    foreach (DataRow rowD in rowsD)                                                                                                                            //add the rows information as FoodEntry object to this newly made Dish
                    {
                        foodDish.addFoodEntry(new FoodEntry(new FoodItem(rowD["FoodName"].ToString(), rowD["Brand"].ToString(),
                                                                         (double)rowD["Fat"], (double)rowD["Carbs"], (double)rowD["Protein"],
                                                                         rowD["SizeInfo"] == DBNull.Value ? Measure.Grams : Measure.Pieces), (double)rowD["Amount"]));
                        affected++;
                    }
                    meal.addFoodEntry(new FoodEntry(foodDish, (double)row["Amount"]));
                }
                else
                {   //if the food found is not a Dish create a basic FoodItem
                    FoodItem food = new FoodItem(row["FoodName"].ToString(), row["Brand"].ToString(),
                                                 (double)row["Fat"], (double)row["Carbs"], (double)row["Protein"],
                                                 row["SizeInfo"] == DBNull.Value ? Measure.Grams : Measure.Pieces);
                    meal.addFoodEntry(new FoodEntry(food, (double)row["Amount"]));
                }
                affected++;
            }

            meal.setCalculatedValues(); //calculate the final food values
            return(meal);
        }
Ejemplo n.º 2
0
        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();
        }