Exemplo n.º 1
0
        public void Button1_Click(object sender, EventArgs e)
        {
            string con           = MyMethodsLib.GetConnectionString();
            int    MeasurementNo = MyMethodsLib.GetMeasurementNoWithTodayDate(Username, con);

            var today2    = DateTime.Now.Date;
            var tomorrow2 = today2.AddDays(1);

            var dateForMealFromDbs2 = MyMethodsLib.GetMealDate(MeasurementNo, con);

            if (tomorrow2 != dateForMealFromDbs2)
            {
                string mode      = "";
                string kgToLoose = MyMethodsLib.WeightResultOutput(Username, MyMethodsLib.KgToLoose(Username, weightBox.Text), weightBox.Text);
                if (kgToLoose != "0")
                {
                    if (slowModeRadioBtn.Checked)
                    {
                        mode = "M: Slow";
                    }
                    else if (steadyModeRadioBtn.Checked)
                    {
                        mode = "M: Steady";
                    }
                    else if (intenseModeRadioBtn.Checked)
                    {
                        mode = "M: Intense";
                    }
                    else
                    {
                        mode = "";
                        MessageBox.Show("Please, choose Your diet mode!");
                    }
                }
                else
                {
                    mode = "Not set";
                    MessageBox.Show("We cannot help you as Your Weight is perfectly fine!");
                }
                using (SqlConnection connection = new SqlConnection(MyMethodsLib.GetConnectionString()))
                {
                    try
                    {
                        string     DietMode = mode;
                        SqlCommand cmd2     = new SqlCommand
                        {
                            CommandText = "UPDATE [tblMeasures] SET DietMode=@DietMode WHERE MeasurementNo=@MeasurementNo"
                        };
                        connection.Open();
                        cmd2.Parameters.AddWithValue("@MeasurementNo", MeasurementNo);
                        cmd2.Parameters.AddWithValue("@DietMode", DietMode);
                        cmd2.Connection = connection;
                        cmd2.ExecuteNonQuery();
                        connection.Close();
                        dietModeBox.Text = DietMode;
                        MessageBox.Show("Your Diet Mode is set!");
                        if (DietMode != "Not set")
                        {
                            intakeBox.Text = MyMethodsLib.CaloriesIntake(Username).ToString();
                        }
                        else
                        {
                            intakeBox.Text = "N\\A";
                            MessageBox.Show("Sorry, not this time!");
                        }
                    }
                    catch (System.Exception)
                    {
                        if (connection.State == ConnectionState.Open)
                        {
                            connection.Close();
                        }
                        MessageBox.Show("Something went wrong with SQL query Line 905");
                    }
                }
            }

            string getPath        = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, @"..\..\..\"));
            string path           = getPath + "DietData";
            string breakJsonName  = "b.json";
            string break2JsonName = "b2.json";
            string lunchJsonName  = "l.json";
            string dinnerJsonName = "d.json";
            //IF THERE IS ALREADY DATE FOR TOMORROW, DO NOT ALLOW TO CHANGE DIET MODE,
            //OUTPUT THE MESSAGE BOX WITH RELEVANT INFO
            //START DOING METHODS FOR OUTPUTTING TEXT TO RICH BOXES
            //REMEMBER, THERE MIGHT BE NO ENTRY IN THIS DBS
            //SO REMEMBER TO CHECK IT
            var today              = DateTime.Now.Date;
            var tomorrow           = today.AddDays(1);
            var dateForMealFromDbs = MyMethodsLib.GetMealDate(MeasurementNo, con);

            if (tomorrow == dateForMealFromDbs)
            {
                MessageBox.Show("You can't change diet mode today, try tomorrow!");
            }
            else
            {
                var randomRecipes = MyMethodsLib.RandomizeRecipesSelection(MyMethodsLib.CaloriesIntake(Username).ToString(), MyMethodsLib.
                                                                           GetVeggieOptionFromDbs(Username, con), path, breakJsonName, break2JsonName, lunchJsonName, dinnerJsonName);
                MyMethodsLib.RecordRecipesToDbs(randomRecipes, MeasurementNo, con);
                int    breakID       = MyMethodsLib.GetBreakfastID(MeasurementNo, con);
                int    break2ID      = MyMethodsLib.GetBreakfast2ID(MeasurementNo, con);
                int    lunchID       = MyMethodsLib.GetLunchID(MeasurementNo, con);
                int    dinnerID      = MyMethodsLib.GetDinnerID(MeasurementNo, con);
                string break2RTFName = Username + "_" + tomorrow.ToString("ddMMyyyy") + "_breakfast2.rtf";
                string breakRTFName  = Username + "_" + tomorrow.ToString("ddMMyyyy") + "_breakfast.rtf";
                string lunchRTFName  = Username + "_" + tomorrow.ToString("ddMMyyyy") + "_lunch.rtf";
                string dinnerRTFName = Username + "_" + tomorrow.ToString("ddMMyyyy") + "_dinner.rtf";
                //breakfast
                MyMethodsLib.SetUpRecipe(path, breakJsonName, breakID, tomorrow, breakRTFName);
                //breakfast2
                MyMethodsLib.SetUpRecipe(path, break2JsonName, break2ID, tomorrow, break2RTFName);
                //lunch
                MyMethodsLib.SetUpRecipe(path, lunchJsonName, lunchID, tomorrow, lunchRTFName);
                //dinner
                MyMethodsLib.SetUpRecipe(path, dinnerJsonName, dinnerID, tomorrow, dinnerRTFName);
                //MessageBox.Show("Your Diet Mode is selected!");
            }
        }