示例#1
0
        private void UpdateDbWithNewCheckIn(CheckInFormViewModel formUserProgressLog)
        {
            currentUserProfile = Helper_Classes.UserHelpers.GetUserProfile();
            var userProfileId = currentUserProfile.Id;


            double startWeight  = 0.0;
            double targetWeight = 0.0;

            if (Helper_Classes.UserHelpers.GetWeightUnit().Equals(WeightUnits.Kg))
            {
                startWeight = Convert.ToDouble(formUserProgressLog.WeightInputA);
            }
            else if (Helper_Classes.UserHelpers.GetWeightUnit().Equals(WeightUnits.Lbs))
            {
                startWeight = Calculators.LbsToKG(Convert.ToDouble(formUserProgressLog.WeightInputA));
            }
            else if (Helper_Classes.UserHelpers.GetWeightUnit().Equals(WeightUnits.LbsAndStone))
            {
                startWeight = Calculators.StToKg(Convert.ToDouble(formUserProgressLog.WeightInputA)) +
                              Calculators.LbsToKG(Convert.ToDouble(formUserProgressLog.WeightInputB));
            }

            formUserProgressLog.UserProgressLog.WeightInKg = Convert.ToDouble(startWeight);

            //Check for log on the same date, if it doesn't exist, insert, else update
            if (_context.UserProgressLogs.SingleOrDefault(g =>
                                                          g.UserProfileId == userProfileId && g.Date == formUserProgressLog.UserProgressLog.Date) == null)
            {
                //Insert
                formUserProgressLog.UserProgressLog.UserProfileId = userProfileId;
                _context.UserProgressLogs.Add(formUserProgressLog.UserProgressLog);
            }
            else
            {
                var photoManager = new PhotoManager();

                var progressLogId = _context.UserProgressLogs.SingleOrDefault(g =>
                                                                              g.UserProfileId == userProfileId && g.Date == formUserProgressLog.UserProgressLog.Date).Id;
                formUserProgressLog.UserProgressLog.Id            = progressLogId;
                formUserProgressLog.UserProgressLog.UserProfileId = userProfileId;

                _context.Entry(_context.UserProgressLogs.
                               SingleOrDefault(g => g.UserProfileId == userProfileId && g.Date == formUserProgressLog.UserProgressLog.Date))
                .CurrentValues
                .SetValues(formUserProgressLog.UserProgressLog);
            }
            _context.SaveChanges();

            //Update
        }
示例#2
0
        public ActionResult AddNewGoal(EditGoalViewModel newGoal)
        {
            currentUserProfile = Helper_Classes.UserHelpers.GetUserProfile();
            var userProfileId = Helper_Classes.UserHelpers.GetUserProfile().Id;
            var dbGoal        = _context.Goals.SingleOrDefault(g => g.UserProfileId == userProfileId);

            newGoal.CalculationBasis = _context.Goals.SingleOrDefault(g => g.UserProfileId == userProfileId) == null
                ? new SelectList(new List <string> {
                "Weight", "Body Fat"
            }, newGoal.CalculationBasisChoice)
                : new SelectList(new List <string> {
                "Weight", "Body Fat"
            }, dbGoal.CalculationBasis);
            newGoal.Title = "Edit Goal";

            if (!ModelState.IsValid)
            {
                foreach (ModelState modelState in ViewData.ModelState.Values)
                {
                    foreach (ModelError error in modelState.Errors)
                    {
                        System.Diagnostics.Debug.WriteLine(error);
                    }
                }
                return(View("NewGoalForm", newGoal));
            }

            newGoal.Goal.StartBodyFat  = newGoal.StartBodyFat;
            newGoal.Goal.TargetBodyFat = newGoal.TargetBodyFat;
            newGoal.Goal.TrackBodyFat  = newGoal.TrackBodyFat;

            var trackBf = newGoal.Goal.TrackBodyFat;

            var calculationBasis = newGoal.CalculationBasisChoice;


            double startWeight  = 0.0;
            double targetWeight = 0.0;

            if (newGoal.AddAsCheckIn && newGoal.Goal.StartDate <= DateTime.Today)
            {
                var checkInModel = new CheckInFormViewModel
                {
                    WeightUnit      = newGoal.WeightUnit,
                    WeightInputA    = newGoal.StartWeightInputA,
                    WeightInputB    = newGoal.StartWeightInputB,
                    UserProgressLog = new UserProgressLog
                    {
                        Date    = newGoal.Goal.StartDate,
                        BodyFat = newGoal.Goal.StartBodyFat
                    }
                };
                UpdateDbWithNewCheckIn(checkInModel);
            }


            if (Helper_Classes.UserHelpers.GetWeightUnit().Equals(WeightUnits.Kg))
            {
                startWeight  = Convert.ToDouble(newGoal.StartWeightInputA);
                targetWeight = Convert.ToDouble(newGoal.TargetWeightInputA);
            }
            else if (Helper_Classes.UserHelpers.GetWeightUnit().Equals(WeightUnits.Lbs))
            {
                startWeight  = Calculators.LbsToKG(Convert.ToDouble(newGoal.StartWeightInputA));
                targetWeight = Calculators.LbsToKG(Convert.ToDouble(newGoal.TargetWeightInputA));
            }
            else if (Helper_Classes.UserHelpers.GetWeightUnit().Equals(WeightUnits.LbsAndStone))
            {
                startWeight = Calculators.StToKg(Convert.ToDouble(newGoal.StartWeightInputA)) +
                              Calculators.LbsToKG(Convert.ToDouble(newGoal.StartWeightInputB));

                targetWeight = Calculators.StToKg(Convert.ToDouble(newGoal.TargetWeightInputA)) +
                               Calculators.LbsToKG(Convert.ToDouble(newGoal.TargetWeightInputB));
            }


            newGoal.Goal.StartWeightInKg  = startWeight;
            newGoal.Goal.TargetWeightInKg = targetWeight;

            //TODO latest checkin always updates final weight on goal

            newGoal.Goal.CalculationBasis = (string)calculationBasis;

            //No Goal Id on user profile. Add it
            if (_context.Goals.SingleOrDefault(g => g.UserProfileId == userProfileId) == null)
            {
                newGoal.Goal.UserProfileId = userProfileId;
                _context.Goals.Add(newGoal.Goal);
            }
            else
            {
                //Update the existing Goal record
                _context.Entry(_context.Goals.SingleOrDefault(g => g.UserProfileId == userProfileId))
                .CurrentValues
                .SetValues(newGoal.Goal);
            }
            _context.SaveChanges();


            //Overwrite new target weight with estimated one if using body fat to calculate goal - ordering is important here as the db needs to have been updated already
            if (calculationBasis.Equals(CalculationBasis.BodyFat))
            {
                targetWeight = Calculators.CalculateEstimatedGoalWeight((double)Helper_Classes.UserHelpers.GetCurrentWeight(),
                                                                        (int)Helper_Classes.UserHelpers.GetCurrentBodyFat(), (int)newGoal.Goal.TargetBodyFat);

                newGoal.Goal.TargetWeightInKg = targetWeight;

                _context.Entry(_context.Goals.SingleOrDefault(g => g.UserProfileId == userProfileId))
                .CurrentValues
                .SetValues(newGoal.Goal);

                _context.SaveChanges();
            }



            return(RedirectToAction("Index", new { controller = "Home" }));
        }