コード例 #1
0
        public ActionResult Index()
        {
            currentUserProfile = Helper_Classes.UserHelpers.GetUserProfile();

            HomeViewModel viewModel = new HomeViewModel();


            //Home page - returns different pages based on whether logged in/profile created/goal set
            if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
            {
                if (Helper_Classes.UserHelpers.GetUserProfile() == null)
                {
                    return(View("HomeNoProfile"));
                }


                //No goal exists for current user
                if (_context.Goals.SingleOrDefault(g => g.UserProfileId == currentUserProfile.Id) == null)
                {
                    //Return view where no goal has been set and no previous goal has been saved
                    return(View("HomeNoGoal"));
                }


                //Goal exists for current user, if the ETD is less than today, return No Goal and populate the page with New Goal button and previous summary
                //if (_context.Goals.SingleOrDefault(g => g.UserProfileId == currentUserProfile.Id).EndDate < DateTime.Today)
                //{
                //    return View("HomeEndedGoal");
                //}


                var userProfile = Helper_Classes.UserHelpers.GetUserProfile();

                var userProfileId = userProfile.Id;

                var currentGoal = Helper_Classes.UserHelpers.GetCurrentGoal();

                var trackBodyFat = currentGoal.TrackBodyFat;

                var startWeight = currentGoal.StartWeightInKg;

                var today = DateTime.Today;


                var maxLogDate = Helper_Classes.UserHelpers.GetCurrentCheckIn(currentGoal.CalculationBasis.Equals(CalculationBasis.BodyFat)).Date;
                //var currentWeightDate = _context.UserProgressLogs
                //    .Where(m => m.UserProfileId == userProfileId && m.Date <= today).Select(m => m.Date).OrderByDescending(m => m.Date).First();

                var currentWeight = Helper_Classes.UserHelpers.GetCurrentWeight();

                var currentBodyFat = Helper_Classes.UserHelpers.GetCurrentBodyFat();

                var goalWeight  = _context.Goals.SingleOrDefault(m => m.UserProfileId == userProfileId).TargetWeightInKg;
                var goalBodyFat = _context.Goals.SingleOrDefault(m => m.UserProfileId == userProfileId).TargetBodyFat;


                var includeBodyFatSummary = currentGoal.CalculationBasis == CalculationBasis.BodyFat || currentGoal.TrackBodyFat ? true : false;
                viewModel.StartBodyFat          = currentGoal.StartBodyFat.ToString();
                viewModel.CurrentBodyFat        = currentBodyFat.ToString();
                viewModel.GoalBodyFat           = currentGoal.TargetBodyFat.ToString();
                viewModel.IncludeBodyFatSummary = includeBodyFatSummary;
                viewModel.TrackBodyFat          = trackBodyFat;
                viewModel.StartWeight           = GetWeightString(startWeight);
                viewModel.CurrentWeight         = GetWeightString((double)currentWeight);
                viewModel.GoalWeight            = GetWeightString(goalWeight);

                //viewModel.CurrentWeight = Convert.ToInt32(currentWeight).ToString();
                //viewModel.GoalWeight = Convert.ToInt32(goalWeight).ToString();


                //Unable to divide by zero so if no difference between start and end weights, divide by 1 instead
                var fullGoalDifference = startWeight - goalWeight == 0 ? 1: startWeight - goalWeight;


                viewModel.WeightProgressPercentage = Convert.ToInt32((startWeight - currentWeight) / (fullGoalDifference) * 100).ToString();
                viewModel.TimeRemaining            = (currentGoal.EndDate - maxLogDate).TotalDays + " days";
                //var sex = userProfile.Sex.BmrAdjustmentValue;
                var sex         = _context.UserProfiles.Include(m => m.Sex).SingleOrDefault(m => m.Id == userProfileId).Sex.BmrAdjustmentValue;
                var height      = userProfile.HeightInCm;
                var dob         = userProfile.DateOfBirth;
                var activityVal = _context.UserProfiles.Include(m => m.ActivityLevel)
                                  .SingleOrDefault(m => m.Id == userProfileId).ActivityLevel.Value;

                // Calculate the age.
                var age = today.Year - dob.Year;
                // Go back to the year the person was born in case of a leap year
                if (dob.Date > today.AddYears(-age))
                {
                    age--;
                }

                var bmr = Calculators.CalculateBmr(sex, Convert.ToInt32(height), age, Convert.ToInt32(currentWeight));


                int dailyCalories = 0;
                if (currentGoal.CalculationBasis.Equals(CalculationBasis.Weight))
                {
                    dailyCalories = Calculators.CalculateDailyCaloriesFromWeight(bmr, activityVal,
                                                                                 (double)currentWeight, goalWeight, currentGoal.StartDate, currentGoal.EndDate);
                }
                else if (currentGoal.CalculationBasis.Equals(CalculationBasis.BodyFat))
                {
                    dailyCalories = Calculators.CalculateDailyCaloriesFromBodyFat(bmr, activityVal, (double)currentWeight, (int)currentBodyFat, (int)goalBodyFat, currentGoal.StartDate, currentGoal.EndDate);
                }

                if (dailyCalories > 900)
                {
                    viewModel.Calories = dailyCalories.ToString();
                }
                else
                {
                    viewModel.Calories = "Daily calories too low. Revise Goal.";
                }


                viewModel.Macros = "Not Set";

                viewModel.GoalType = "Edit Goal";
                if (currentGoal.EndDate < DateTime.Today)
                {
                    viewModel.GoalType = "New Goal";
                }

                //Goal must be active - return view with current goal summary
                return(View("HomeWithGoal", viewModel));
            }

            return(View("HomeNoLogin"));
        }