Example #1
0
        public void DeletePersonalData(string name)
        {
            DietEntities db = new DietEntities();

            //db.Database.ExecuteSqlCommand("TRUNCATE TABLE dbo.Users");
            var rec =
                from records in db.Users
                where records.name == name
                select records;

            foreach (var delrec in rec)
            {
                db.Users.Remove(delrec);
            }

            try
            {
                db.SaveChanges();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                // Provide for exceptions.
            }
        }
Example #2
0
        public void AddWeight(string date, double newWeight)
        {
            DietEntities db         = new DietEntities();
            int          nextRecNum = 0;
            var          records    = (from items in db.Weights
                                       select items);

            foreach (var item in records)
            {
                if (item.recNum > nextRecNum)
                {
                    nextRecNum = item.recNum;
                }
            }
            nextRecNum++;

            Weight weight = new Weight();

            weight.recNum      = nextRecNum;
            weight.measureDate = date;
            weight.weight1     = (float)newWeight;
            try
            {
                db.Weights.Add(weight);
                db.SaveChanges();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.InnerException);
            }
            db.Dispose();
        }
Example #3
0
        public void AddNewFood(string foodName, int calories)
        {
            DietEntities db         = new DietEntities();
            int          nextRecNum = 0;
            var          records    = (from items in db.Foods
                                       select items);

            foreach (var item in records)
            {
                if (item.recNum > nextRecNum)
                {
                    nextRecNum = item.recNum;
                }
            }
            nextRecNum++;

            Food food = new Food();

            food.recNum   = nextRecNum;
            food.food1    = foodName;
            food.calories = calories;
            try
            {
                db.Foods.Add(food);
                db.SaveChanges();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.InnerException);
            }
            db.Dispose();
        }
Example #4
0
        public void AddPersonalData(string name, float height, float iWeight, float tWeight, string ssid)
        {
            DietEntities db = new DietEntities();

            User pd = new User();

            pd.name          = name;
            pd.height        = height;
            pd.initialWeight = iWeight;
            pd.targetWeight  = tWeight;
            pd.SSID          = ssid;
            try
            {
                db.Users.Add(pd);
                db.SaveChanges();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.InnerException);
            }
            db.Dispose();
        }
Example #5
0
        public void AddDailyFood(string foodName, double qty)
        {
            DietEntities db        = new DietEntities();
            DailyMeal    dailyMeal = new DailyMeal();

            DateTime date;

            date = DateTime.Today.Date;

            string currentDate;
            string yesterday;
            bool   sameday;
            double dailyTotal;
            int    lastRecNum;

            DateTime now = DateTime.Now;

            currentDate = now.ToString("MM/dd/yyyy");
            yesterday   = now.AddDays(-1).ToString("MM/dd/yyyy");

            int foodIndex = GetFoodIndex(foodName);

            sameday = db.DailyMeals.Any(entry => entry.dayDate.Equals(date));
            // if any record's column 'daydate' equals 'date' from this method's arguement list
            // 'entry' is a local LINQ arguement referring to a record in the table
            // see https://www.dotnetperls.com/any

            lastRecNum = db.WeeklyCalories.Count();

            if (!sameday)
            {
                // total all calories for week and enter in WeeklyCalories
                if (now.ToString("dddd") == "Monday")
                {
                    dailyTotal = SumDailyCalories();

                    WeeklyCalory wc = new WeeklyCalory();
                    wc.recNum        = lastRecNum + 1;
                    wc.weekEnding    = yesterday;
                    wc.totalCalories = (float)dailyTotal;

                    try
                    {
                        db.WeeklyCalories.Add(wc);
                        db.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine(e.InnerException);
                    }

                    // clear DailyMeals

                    // Alternate Method
                    // **************************************************
                    //string connstr = "Provider=SQLOLEDB;"
                    //    + "Data Source = .\\SQLEXPRESS;"
                    //    + "Initial Catalog =Diet;"
                    //    + "Integrated Security=SSPI";
                    //DataContext context = new DataContext(connstr);
                    //context.ExecuteCommand("TRUNCATE TABLE DailyMeals");
                    // **************************************************

                    var allRecords = (from item in db.DailyMeals
                                      select item);
                    foreach (DailyMeal item in allRecords)
                    {
                        db.DailyMeals.Remove(item);
                    }
                    try
                    {
                        db.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine(e.InnerException);
                    }
                    lastRecNum = 0;
                }
            }

            // now add the latest entry
            dailyMeal.recNum   = lastRecNum + 1;
            dailyMeal.dayDate  = date;
            dailyMeal.food     = foodIndex;
            dailyMeal.quantity = (float)qty;
            try
            {
                db.DailyMeals.Add(dailyMeal);
                db.SaveChanges();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.InnerException);
            }
            db.Dispose();
        }
Example #6
0
        public void AddDailyFoodItem(string foodName, double qty)
        {
            DietEntities db    = new DietEntities();
            Daily        daily = new Daily();

            DateTime date;

            date = DateTime.Today.Date;

            int foodIndex = GetFoodIndex(foodName);

            //string today = now.ToString("MM/dd/yyyy");
            int nextRecNum = 0;

            //var count = (from item in db.Dailies
            //             select item);
            //foreach (var item in count)
            //{
            //    if (item.recNum > nextRecNum)
            //        nextRecNum = (int)item.recNum;
            //}

            nextRecNum = int.Parse(db.Dailies
                                   .OrderByDescending(p => p.recNum)
                                   .Select(r => r.recNum)
                                   .First().ToString());


            nextRecNum++;
            daily.day_of_week = date;
            daily.recNum      = nextRecNum;
            daily.food        = foodIndex;
            daily.qty         = (float)qty;


            //var rec = (from item in db.Dailies
            //           where item.day_of_week == sqlDate
            //           select item);

            //foreach (var item in rec)
            //{
            //    daily.recNum = item.recNum;
            //    daily.day_of_week = item.day_of_week;
            //    daily = item;
            //        daily.food = foodIndex;
            //        daily.qty = (float)qty;
            //        break;
            //}



            //foreach (var item in rec)
            //{
            //    daily.recNum = item.recNum;
            //    daily.day_of_week = item.day_of_week;
            //    daily = item;
            //    if (!item.food1.HasValue)
            //    {
            //        daily.food1 = foodIndex;
            //        daily.qty1 = (float)qty;
            //        break;
            //    }
            //    if (!item.food2.HasValue)
            //    {
            //        daily.food2 = foodIndex;
            //        daily.qty2 = (float)qty;
            //        break;
            //    }
            //    if (!item.food3.HasValue)
            //    {
            //        daily.food3 = foodIndex;
            //        daily.qty3 = (float)qty;
            //        break;
            //    }
            //    if (!item.food4.HasValue)
            //    {
            //        daily.food4 = foodIndex;
            //        daily.qty4 = (float)qty;
            //        break;
            //    }
            //    if (!item.food5.HasValue)
            //    {
            //        daily.food5 = foodIndex;
            //        daily.qty5 = (float)qty;
            //        break;
            //    }
            //    if (!item.food6.HasValue)
            //    {
            //        daily.food6 = foodIndex;
            //        daily.qty6 = (float)qty;
            //        break;
            //    }
            //    if (!item.food7.HasValue)
            //    {
            //        daily.food7 = foodIndex;
            //        daily.qty7 = (float)qty;
            //        break;
            //    }
            //    if (!item.food8.HasValue)
            //    {
            //        daily.food8 = foodIndex;
            //        daily.qty8 = (float)qty;
            //        break;
            //    }
            //    if (!item.food9.HasValue)
            //    {
            //        daily.food9 = foodIndex;
            //        daily.qty9 = (float)qty;
            //        break;
            //    }
            //    if (!item.food10.HasValue)
            //    {
            //        daily.food10 = foodIndex;
            //        daily.qty10 = (float)qty;
            //        break;
            //    }
            //}

            try
            {
                db.Dailies.Add(daily);
                db.SaveChanges();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.InnerException);
            }

            //var original = db.Dailies.Find(daily.recNum);

            //if (original != null)
            //{
            //    db.Entry(original).CurrentValues.SetValues(daily);
            //    db.SaveChanges();
            //}
            //else
            //{
            //    daily = new Daily();
            //    daily.recNum = nextRecNum;
            //    daily.day_of_week = DateTime.Parse(date).Date;
            //    daily.food = foodIndex;
            //    daily.qty = (float)qty;
            //    try
            //    {
            //        db.Dailies.Add(daily);
            //        db.SaveChanges();
            //    }
            //    catch (Exception e)
            //    {
            //        Debug.WriteLine(e.InnerException);
            //    }
            //}
            db.Dispose();
            AddDailyFood(foodName, qty);
        }