public ActionResult UpdateProfile(ProfileModel model)
        {
            var userAccount = HttpContext.UserAccount();

            if (!ModelState.IsValid || userAccount == null)
            {
                if (model.Weight == null)
                {
                    model.Weight = ProfileModel.DefaultWeightData();
                }
                model.Message = "Please enter your name";
                return(View("Profile", model));
            }

            userAccount.DisplayName = model.Name;
            if (Enum.IsDefined(typeof(DistanceUnits), model.DistUnits))
            {
                var distUnits = (DistanceUnits)model.DistUnits;
                userAccount.DistanceUnits = distUnits;
                HttpContext.Session.Set("rp_UserDistanceUnits", distUnits);
            }

            MassiveDB.Current.UpdateUser(userAccount);

            var newModel = new ProfileModel(HttpContext);

            if (newModel.Weight != model.Weight)
            {
                MassiveDB.Current.CreatePref(userAccount, model.Weight.UnitWeight, model.Weight.Units);
            }

            return(RedirectToAction("profile", new { saved = true }));
        }
Esempio n. 2
0
        public ActionResult AutoCalcCalories(string date, int?route, double?distance)
        {
            WeightData weight = null;

            if (HttpContext.HasValidUserAccount(dataCache) && date != null)
            {
                var userAccount = HttpContext.UserAccount(dataCache);

                DateTime onDate;
                if (!DateTime.TryParseExact(date, "ddd, d MMM yyyy HH':'mm':'ss 'UTC'", null, DateTimeStyles.AssumeUniversal, out onDate))
                {
                    onDate = DateTime.UtcNow;
                }

                weight = ProfileModel.DefaultWeightData();
                var userPref = ((object)userAccount).UserPrefs().Latest(onDate);
                if (userPref != null)
                {
                    weight.Units = userPref.WeightUnits;
                    switch (weight.Units)
                    {
                    case "kg":
                        weight.Kg = userPref.Weight;
                        break;

                    case "lbs":
                    case "st":
                        weight.Lbs   = userPref.Weight;
                        weight.Units = "lbs";
                        break;
                    }
                    weight.UpdateFromUnits();
                    if (userPref.WeightUnits == "st")
                    {
                        weight.Units = "st";
                    }
                }
            }

            if (weight == null || weight.UnitWeight == 0 || (!route.HasValue && !distance.HasValue))
            {
                return(Json(new { Result = false }));
            }

            Distance actualDistance = null;

            if (route.HasValue && route.Value > 0)
            {
                var dbRoute = MassiveDB.Current.FindRoute(route.Value);
                if (dbRoute != null)
                {
                    actualDistance = new Distance((double)dbRoute.Distance, (DistanceUnits)dbRoute.DistanceUnits).ConvertTo(HttpContext.UserDistanceUnits(dataCache));
                }
            }

            if (distance.HasValue && distance.Value > 0)
            {
                actualDistance = new Distance(distance.Value, HttpContext.UserDistanceUnits(dataCache));
            }

            if (actualDistance == null)
            {
                return(Json(new { Result = false }));
            }

            return(Json(new { Result = true, Calories = caloriesCalc.Calculate(actualDistance, weight) }));
        }