예제 #1
0
        public ActionResult NewCheckInForm(string pageFrom)
        {
            //Used when creating a new check in

            //If no log found for that date, fetch blank log page. If existing log found, fetch existing info into page
            CheckInFormViewModel viewModel;
            var    userProfileId = Helper_Classes.UserHelpers.GetUserProfile().Id;
            string weightInputA  = "";
            string weightInputB  = "";
            var    weightUnit    = Helper_Classes.UserHelpers.GetWeightUnit();
            double weight        = 0.0;

            if (_context.UserProgressLogs.SingleOrDefault(g =>
                                                          g.UserProfileId == userProfileId && g.Date == DateTime.Today) == null)
            {
                viewModel = new CheckInFormViewModel
                {
                    UserProgressLog = new UserProgressLog()
                }
            }
            ;

            else
            {
                viewModel = new CheckInFormViewModel
                {
                    UserProgressLog = _context.UserProgressLogs.SingleOrDefault(g =>
                                                                                g.UserProfileId == userProfileId && g.Date == DateTime.Today),
                };
                weight = (double)viewModel.UserProgressLog.WeightInKg;
            }

            viewModel.BodyFat = viewModel.UserProgressLog.BodyFat;



            if (weightUnit.Equals(WeightUnits.Kg))
            {
                weightInputA = Convert.ToInt32(weight).ToString();
            }
            else if (weightUnit.Equals(WeightUnits.Lbs))
            {
                weightInputA = Convert.ToInt32(Calculators.KgsToLbs(weight)).ToString();
            }
            else if (weightUnit.Equals(WeightUnits.LbsAndStone))
            {
                weightInputA = Convert.ToInt32(Calculators.KgsToStone(weight)).ToString();
                weightInputB = Convert.ToInt32(Calculators.KgsToLbsRemainingFromStone(weight)).ToString();
            }
            viewModel.WeightInputA    = weightInputA;
            viewModel.WeightInputB    = weightInputB;
            viewModel.WeightUnit      = weightUnit;
            viewModel.RedirectionPage = pageFrom;

            viewModel.IsBodyFatCalculation =
                Helper_Classes.UserHelpers.GetCurrentGoal().CalculationBasis == CalculationBasis.BodyFat ? true : false;

            return(View(viewModel));
        }
예제 #2
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
        }
예제 #3
0
        public ActionResult AddNewProgressLog(CheckInFormViewModel formUserProgressLog)
        {
            if (!ModelState.IsValid)
            {
                foreach (ModelState modelState in ViewData.ModelState.Values)
                {
                    foreach (ModelError error in modelState.Errors)
                    {
                        System.Diagnostics.Debug.WriteLine(error);
                    }
                }
                return(View("NewCheckInForm", formUserProgressLog));
            }

            if (formUserProgressLog.Photo != null && formUserProgressLog.Photo.IsImage())
            {
                PhotoManager photoManager = new PhotoManager();


                //TODO Insert new photo into UserPhotos.Photo and assign UserPhotos.Id to Userprogresslogs.userphoto. Separation of concerns - don't have this method handle the specifics of the logic
                //byte[] uploadedFile = new byte[formUserProgressLog.Photo.InputStream.Length];

                //formUserProgressLog.Photo.InputStream.Read(uploadedFile, 0, uploadedFile.Length);

                //var newPhoto = new UserPhoto
                //{
                //    Photo = uploadedFile
                //};

                //_context.UserPhotos.Add(newPhoto);
                //_context.SaveChanges();
                formUserProgressLog.UserProgressLog.UserPhotoId = photoManager.UploadProgressPhotoToDb(formUserProgressLog.Photo);
            }



            formUserProgressLog.UserProgressLog.BodyFat = formUserProgressLog.BodyFat;
            UpdateDbWithNewCheckIn(formUserProgressLog);

            return(RedirectToAction("Index", new { controller = formUserProgressLog.RedirectionPage }));
        }
예제 #4
0
        //Used when 'editing' a check in from the tracker
        public ActionResult NewCheckInForm(int id)
        {
            //If no log found for that date, fetch blank log page. If existing log found, fetch existing info into page
            CheckInFormViewModel viewModel;
            var             userProfileId   = Helper_Classes.UserHelpers.GetUserProfile().Id;
            string          weightInputA    = "";
            string          weightInputB    = "";
            var             weightUnit      = Helper_Classes.UserHelpers.GetWeightUnit();
            UserProgressLog userProgressLog = _context.UserProgressLogs.SingleOrDefault(m => m.Id == id);
            double          weight          = userProgressLog.WeightInKg.Value;

            if (weightUnit.Equals(WeightUnits.Kg))
            {
                weightInputA = Convert.ToInt32(weight).ToString();
            }
            else if (weightUnit.Equals(WeightUnits.Lbs))
            {
                weightInputA = Convert.ToInt32(Calculators.KgsToLbs(weight)).ToString();
            }
            else if (weightUnit.Equals(WeightUnits.LbsAndStone))
            {
                weightInputA = Convert.ToInt32(Calculators.KgsToStone(weight)).ToString();
                weightInputB = Convert.ToInt32(Calculators.KgsToLbsRemainingFromStone(weight)).ToString();
            }

            viewModel = new CheckInFormViewModel
            {
                UserProgressLog = userProgressLog,
                WeightInputA    = weightInputA,
                WeightInputB    = weightInputB,
                WeightUnit      = weightUnit,
                PageTitlePrefix = "Edit",
                RedirectionPage = "Tracker"
            };

            viewModel.BodyFat = viewModel.UserProgressLog.BodyFat;
            viewModel.IsBodyFatCalculation =
                Helper_Classes.UserHelpers.GetCurrentGoal().CalculationBasis == CalculationBasis.BodyFat ? true : false;

            return(View("NewCheckInForm", viewModel));
        }
예제 #5
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" }));
        }