protected void bAjaxPostback_Click(object sender, EventArgs e) { try { aqufitEntities entities = new aqufitEntities(); Affine.Data.Managers.IDataManager datMan = Affine.Data.Managers.LINQ.DataManager.Instance; switch (hiddenAjaxAction.Value) { case "SaveWOD": JavaScriptSerializer serializer = new JavaScriptSerializer(); Affine.Data.json.WOD wodJson = serializer.Deserialize <Affine.Data.json.WOD>(hiddenAjaxValue.Value); if (GroupSettings != null) { // if this was a group wod.. we want to bounce back to the scheduling page now... wodJson.CreatorUId = GroupSettings.Id; } else { wodJson.CreatorUId = UserSettings.Id; } long wodId = datMan.CreateWOD(wodJson); if (wodJson.GroupKey > 0 && (wodJson.GroupKey != 435 && wodJson.GroupKey != 516)) // TODO: this is a tmp switch { DateTime date = DateTime.Parse(wodJson.Date); datMan.ScheduleGroupWOD(UserSettings.Id, wodJson.GroupKey, wodId, date, date.AddDays(-1), null, ""); } if (GroupSettings != null) { Response.Redirect(ResolveUrl("~") + "group/" + GroupSettings.UserName + "?w=" + wodId, true); return; } else { string gname = wodJson.GroupKey > 0 ? wodJson.GorupName : ""; RadAjaxManager1.ResponseScripts.Add("Aqufit.Windows.WorkoutSavedDialog.open(" + wodId + ",'" + gname + "');"); } break; } } catch (Exception ex) { // TODO: better error handling // RadAjaxManager1.ResponseScripts.Add(" alert('"+ ex.StackTrace.Replace("'", "") + "');"); RadAjaxManager1.ResponseScripts.Add(" alert('" + ex.Message + "');"); } }
protected void bSaveWOD_Click(object sender, EventArgs e) { string status = string.Empty; try { JavaScriptSerializer serializer = new JavaScriptSerializer(); Affine.Data.json.WOD wodJson = serializer.Deserialize <Affine.Data.json.WOD>(atiWodJson.Value); aqufitEntities entities = new aqufitEntities(); // TODO: check if there is already a WOD of the same name long wodTypeId = Convert.ToInt64(ddlWODType.SelectedValue); int standard = UserInfo.IsSuperUser ? 1 : 0; long userKey = GroupSettings != null ? this.GroupSettings.Id : this.UserSettings.Id; WOD wod = new WOD() { Difficulty = wodJson.Difficulty, Standard = (short)standard, Name = Affine.Utils.Web.WebUtils.MakeWebSafeString(txtWorkoutName.Text), WODType = entities.WODTypes.First(t => t.Id == wodTypeId), UserSettingsKey = userKey, Description = Affine.Utils.Web.WebUtils.MakeWebSafeString(txtDescription.Text), UserName = GroupSettings != null ? this.GroupSettings.UserName : this.UserSettings.UserName, CreationDate = DateTime.Now.ToUniversalTime() }; if (wodTypeId == (int)Affine.Utils.WorkoutUtil.WodType.AMRAP) { wod.TimeSpan = atiTimeSpan.Time; } entities.AddToWODs(wod); foreach (Affine.Data.json.WODSet setJson in wodJson.WODSets) { WODSet wodSet = new WODSet() { WOD = wod }; //Affine.Data.Exercise[] exerciseArray = entities.Exercises.Where(Affine.Utils.Linq.LinqUtils.BuildContainsExpression<Affine.Data.Exercise, long>(ex => ex.Id, setJson.WODExercises.Select(ee => ee.ExcercisKey ).ToArray() )).ToArray(); foreach (Affine.Data.json.WODExercise exJson in setJson.WODExercises) { double mdist = exJson.MenDist > 0 ? Utils.UnitsUtil.unitsToSystemDefualt(exJson.MenDist, Utils.UnitsUtil.ToUnit(exJson.MenDistUnits)) : -1; double wdist = exJson.WomenDist > 0 ? Utils.UnitsUtil.unitsToSystemDefualt(exJson.WomenDist, Utils.UnitsUtil.ToUnit(exJson.WomenDistUnits)) : -1; double mweight = exJson.MenRx > 0 ? Utils.UnitsUtil.unitsToSystemDefualt(exJson.MenRx, Utils.UnitsUtil.ToUnit(exJson.MenRxUnits)) : -1; double wweight = exJson.WomenRx > 0 ? Utils.UnitsUtil.unitsToSystemDefualt(exJson.WomenRx, Utils.UnitsUtil.ToUnit(exJson.WomenRxUnits)) : -1; WODExercise wodEx = new WODExercise() { MenDist = mdist, WomenDist = wdist, WODSet = wodSet, MenRx = mweight, WomenRx = wweight, Reps = exJson.Reps, Order = exJson.Order, Notes = Affine.Utils.Web.WebUtils.MakeWebSafeString(exJson.Notes), Exercise = entities.Exercises.First(ee => ee.Id == exJson.ExcercisKey) }; } } entities.SaveChanges(); // Now we add to peoples favs // ** This no longer happens here.. it happens when the workout is "scheduled" for the group that it gets added to the list.. WOD newWOD = entities.WODs.Where(w => w.UserSettingsKey == userKey).OrderByDescending(w => w.Id).First(); if (GroupSettings == null) { // simply add for this user.. User2WODFav fav = new User2WODFav() { UserSetting = entities.UserSettings.First(us => us.Id == UserSettings.Id), WOD = newWOD }; entities.AddToUser2WODFav(fav); entities.SaveChanges(); } else { User2WODFav fav = new User2WODFav() { UserSetting = entities.UserSettings.First(us => us.Id == GroupSettings.Id), WOD = newWOD }; entities.AddToUser2WODFav(fav); entities.SaveChanges(); } if (GroupSettings != null) { // if this was a group wod.. we want to bounce back to the scheduling page now... Response.Redirect(ResolveUrl("~") + "group/" + GroupSettings.UserName + "?w=" + newWOD.Id, true); return; } string baseUrl = ResolveUrl("~"); RadAjaxManager1.ResponseScripts.Add("Aqufit.Windows.WorkoutSavedDialog.open(" + newWOD.Id + ");"); //status = "Your Workout has been saved. It will now show up in your Workout List"; } catch (Exception ex) { status = "There was a problem saving your workout: " + ex.Message + " " + ex.InnerException; } RadAjaxManager1.ResponseScripts.Add(" UpdateStatus('" + status + "');"); }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // CA - this will need to be cleaned up a lot.. but here is the iteration one version // ** People can view workouts in 4 modes (everyone, watchlist, friends, just them) // -In the case of running, cycling, swimming, and STANDARD crossfit Wods... we are going to // be storing peoples best times for (distance rnages) and standard wods... so in these cases // we can shorted the query time when we do an "everyone" query // -Watch list and friends we do the work of getting every time that person did the workout.. not just // there best times. // -Same for the (you only) view // private void LoadFlexDataForWorkout(Workout workout, Affine.Utils.ConstsUtil.GraphContext context) { Affine.Data.Managers.IDataManager dataManager = Affine.Data.Managers.LINQ.DataManager.Instance; JavaScriptSerializer serializer = new JavaScriptSerializer(); // onload we send friend, watch, and group member data that is needed to the app aqufitEntities entities = new aqufitEntities(); IList <long> friendIds = entities.UserFriends.Where(f => (f.SrcUserSettingKey == ProfileSettings.Id || f.DestUserSettingKey == ProfileSettings.Id)).Select(f => (f.SrcUserSettingKey == ProfileSettings.Id ? f.DestUserSettingKey : f.SrcUserSettingKey)).ToList(); friendIds.Add(ProfileSettings.Id); // send the persons profile through var profile = new { UserName = ProfileSettings.UserName, Id = ProfileSettings.Id, FirstName = ProfileSettings.UserFirstName, LastName = ProfileSettings.UserLastName }; hiddenProfileJson.Value = serializer.Serialize(profile); var friends = entities.UserSettings.OfType <User>().Where(LinqUtils.BuildContainsExpression <User, long>(s => s.Id, friendIds)).Select(u => new { Id = u.Id, Un = u.UserName, Fn = u.UserFirstName, Ln = u.UserLastName }).ToArray(); hiddenFriendJson.Value = serializer.Serialize(friends); // get groups long[] groupIds = entities.UserFriends.Where(f => (f.SrcUserSettingKey == ProfileSettings.Id || f.DestUserSettingKey == ProfileSettings.Id) && f.Relationship > 0).Select(f => f.SrcUserSettingKey == ProfileSettings.Id ? f.DestUserSettingKey : f.SrcUserSettingKey).ToArray(); UserSettings[] groupList = entities.UserSettings.OfType <Group>().Where(LinqUtils.BuildContainsExpression <UserSettings, long>(us => us.Id, groupIds)).OrderBy(f => f.UserName).ToArray(); var groups = entities.UserSettings.OfType <Group>().Where(LinqUtils.BuildContainsExpression <Group, long>(s => s.Id, groupIds)).Select(u => new { Id = u.Id, Un = u.UserName }).ToArray(); hiddenGroupJson.Value = serializer.Serialize(groups); // next we load the workout compare to all (friends) Affine.WebService.StreamService ss = new WebService.StreamService(); //hiddenWorkout.Value = ss.getWorkout(workout.Id); Affine.Data.json.Workout jsonWorkout = dataManager.workoutToJsonWorkout(workout); Affine.Data.json.WOD wod = null; Affine.Data.json.WorkoutData[] jsonWorkoutData = null; //var workoutJson = new { Id = workout.Id, WorkoutTypeKey = workout.WorkoutTypeKey, Title = workout.Title, Date = workout.Date.ToLocalTime().ToShortDateString(), DataSrc = workout.DataSrc }; Affine.Utils.UnitsUtil.MeasureUnit weight = base.WeightUnits; if (workout.WorkoutTypeKey == (int)Affine.Utils.WorkoutUtil.WorkoutType.CROSSFIT) { if (context == Utils.ConstsUtil.GraphContext.DEFAULT) { // default context for crossfit is the watchlist context = Utils.ConstsUtil.GraphContext.WATCHLIST; } // if this is a crossfit workout // we want to get ALL the same WoDs for you and your friends IQueryable <Workout> workoutDataQuery = context == Utils.ConstsUtil.GraphContext.EVERYONE ? entities.UserStreamSet.Include("UserSettings").OfType <Workout>().Where(w => w.WOD.Id == workout.WOD.Id).AsQueryable() : context == Utils.ConstsUtil.GraphContext.WATCHLIST ? entities.UserStreamSet.Include("UserSettings").OfType <Workout>().Where(w => w.WOD.Id == workout.WOD.Id).Where(LinqUtils.BuildContainsExpression <Workout, long>(w => w.UserSetting.Id, friendIds)).AsQueryable() : context == Utils.ConstsUtil.GraphContext.FRIENDS ? entities.UserStreamSet.Include("UserSettings").OfType <Workout>().Where(w => w.WOD.Id == workout.WOD.Id).Where(LinqUtils.BuildContainsExpression <Workout, long>(w => w.UserSetting.Id, friendIds)).AsQueryable() : entities.UserStreamSet.Include("UserSettings").OfType <Workout>().Where(w => w.WOD.Id == workout.WOD.Id && w.UserSetting.Id == ProfileSettings.Id).AsQueryable(); var workoutData = workoutDataQuery.Select(w => new { Id = w.Id, UsKey = w.UserSetting.Id, Un = w.UserSetting.UserName, T = w.Duration, S = w.Score, M = w.Max, Rx = w.RxD, D = w.Date }).ToArray(); wod = new Affine.Data.json.WOD() { Id = workout.WOD.Id, Standard = workout.WOD.Standard, Type = workout.WOD.WODType.Id, Name = workout.WOD.Name, Description = workout.WOD.Description }; if (wod.Type == (int)Affine.Utils.WorkoutUtil.WodType.TIMED) { // time in sec jsonWorkoutData = workoutData.OrderByDescending(w => w.T).Select(w => new Affine.Data.json.WorkoutData() { UId = w.Un + "_" + w.Id, Id = w.Id, S = (double)w.T, Un = w.Un, D = w.D.ToLocalTime().ToShortDateString(), Rx = (w.Rx.HasValue ? w.Rx.Value : false) }).ToArray(); } else if (wod.Type == (int)Affine.Utils.WorkoutUtil.WodType.MAX_WEIGHT) { jsonWorkoutData = workoutData.OrderByDescending(w => w.M).Select(w => new Affine.Data.json.WorkoutData() { UId = w.Un + "_" + w.Id, Id = w.Id, S = Math.Round(Affine.Utils.UnitsUtil.systemDefaultToUnits(Convert.ToDouble(w.M), weight), 2), Un = w.Un, D = w.D.ToLocalTime().ToShortDateString(), Rx = (w.Rx.HasValue ? w.Rx.Value : false) }).ToArray(); } else { jsonWorkoutData = workoutData.OrderByDescending(w => w.S).Select(w => new Affine.Data.json.WorkoutData() { UId = w.Un + "_" + w.Id, Id = w.Id, S = Math.Round(Convert.ToDouble(w.S), 2), Un = w.Un, D = w.D.ToLocalTime().ToShortDateString(), Rx = (w.Rx.HasValue ? w.Rx.Value : false) }).ToArray(); } } else { // for now this will handle "running, swimming, walking, cycling" if (context == Utils.ConstsUtil.GraphContext.DEFAULT) { if (workout.DataSrc == (short)Utils.WorkoutUtil.DataSrc.MANUAL_NO_MAP || workout.DataSrc == (short)Utils.WorkoutUtil.DataSrc.MANUAL_WITH_MAP) { context = Utils.ConstsUtil.GraphContext.EVERYONE; } else if (workout.DataSrc == (short)Utils.WorkoutUtil.DataSrc.NIKE_NO_MAP) { context = Utils.ConstsUtil.GraphContext.WATCHLIST; } // TODO: not sure about this one.... /* * else if (workout.DataSrc == (short)Utils.WorkoutUtil.DataSrc.MANUAL_WITH_MAP) * { // TODO: ?? this is a special case that we want to compare all the times of people that have logged a run for the route. * long mapRouteKey = entities.UserStreamSet.OfType<Workout>().Where(w => w.Id == workout.Id).Select(w => w.WorkoutExtendeds.FirstOrDefault().MapRoute.Id).FirstOrDefault(); * IQueryable<Workout> workoutQuery = entities.WorkoutExtendeds.Include("UserStream").Where(we => we.MapRoute != null && we.MapRoute.Id == mapRouteKey).Select(we => we.UserStream).OfType<Workout>().OrderBy(w => w.Duration).Take(5000); * workoutQuery.Select(w => w.UserSetting).ToArray(); // hydrate.. not working above so do it here. * Workout[] data = workoutQuery.ToArray(); * var wd = data.OrderByDescending(w => w.Duration).Select(w => new * { * UId = w.UserSetting.UserName + "_" + w.Id, * Id = w.Id, * S = w.Duration, * Un = w.UserSetting.UserName, * D = w.Date.ToLocalTime().ToShortDateString() * }).ToArray(); * var ret = new { WOD = new { }, WorkoutData = wd, Workout = workoutJson, Context = (int)context }; * hiddenWorkoutData.Value = serializer.Serialize(ret); * }*/ else { context = Utils.ConstsUtil.GraphContext.ME; } } if (context == Utils.ConstsUtil.GraphContext.EVERYONE) { // we want to compare achievments... IQueryable <Workout> workoutQuery = entities.Achievements.Include("UserStream").Include("UserStream.UserSetting").Where(a => a.AchievementType.WorkoutType.Id == workout.WorkoutTypeKey && workout.Distance >= a.AchievementType.DistanceRangeA && workout.Distance < a.AchievementType.DistanceRangeB).Take(5000).Select(a => a.UserStream).OfType <Workout>().AsQueryable(); workoutQuery = workoutQuery.Union(entities.UserStreamSet.OfType <Workout>().Where(w => w.Id == workout.Id)); workoutQuery.Select(w => w.UserSetting).ToArray(); // hydrate.. not working above so do it here. Workout[] data = workoutQuery.ToArray(); jsonWorkoutData = data.OrderByDescending(w => w.Duration).Select(w => new Affine.Data.json.WorkoutData() { UId = w.UserSetting.UserName + "_" + w.Id, Id = w.Id, S = w.Duration.Value, Un = w.UserSetting.UserName, D = w.Date.ToLocalTime().ToShortDateString() }).ToArray(); } else if (context == Utils.ConstsUtil.GraphContext.WATCHLIST || context == Utils.ConstsUtil.GraphContext.FRIENDS) { IQueryable <Workout> workoutQuery = entities.Achievements.Include("UserStream").Include("UserStream.UserSetting").Where(a => a.AchievementType.WorkoutType.Id == workout.WorkoutTypeKey && workout.Distance >= a.AchievementType.DistanceRangeA && workout.Distance < a.AchievementType.DistanceRangeB). Where(LinqUtils.BuildContainsExpression <Achievement, long>(a => a.UserSetting.Id, friendIds)).Take(5000).Select(a => a.UserStream).OfType <Workout>().AsQueryable(); workoutQuery = workoutQuery.Union(entities.UserStreamSet.OfType <Workout>().Where(w => w.Id == workout.Id)); workoutQuery.Select(w => w.UserSetting).ToArray(); // hydrate.. not working above so do it here. Workout[] data = workoutQuery.ToArray(); jsonWorkoutData = data.OrderByDescending(w => w.Duration).Select(w => new Affine.Data.json.WorkoutData() { UId = w.UserSetting.UserName + "_" + w.Id, Id = w.Id, S = w.Duration.Value, Un = w.UserSetting.UserName, D = w.Date.ToLocalTime().ToShortDateString() }).ToArray(); } else if (context == Utils.ConstsUtil.GraphContext.ME) // this is just you for the ranges.. since no map data { IQueryable <Workout> workoutQuery = entities.Achievements.Include("UserStream").Include("UserStream.UserSetting").Where(a => a.UserSetting.Id == ProfileSettings.Id && a.AchievementType.WorkoutType.Id == workout.WorkoutTypeKey && workout.Distance >= a.AchievementType.DistanceRangeA && workout.Distance < a.AchievementType.DistanceRangeB). Take(5000).Select(a => a.UserStream).OfType <Workout>().AsQueryable(); workoutQuery = workoutQuery.Union(entities.UserStreamSet.OfType <Workout>().Where(w => w.Id == workout.Id)); workoutQuery.Select(w => w.UserSetting).ToArray(); // hydrate.. not working above so do it here. Workout[] data = workoutQuery.ToArray(); jsonWorkoutData = data.OrderByDescending(w => w.Duration).Select(w => new Affine.Data.json.WorkoutData() { UId = w.UserSetting.UserName + "_" + w.Id, Id = w.Id, S = w.Duration.Value, Un = w.UserSetting.UserName, D = w.Date.ToLocalTime().ToShortDateString() }).ToArray(); } } var retWorkout = new { WOD = wod, WorkoutData = jsonWorkoutData, Workout = jsonWorkout, Context = (int)context }; hiddenWorkoutData.Value = serializer.Serialize(retWorkout); }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // CA - this will need to be cleaned up a lot.. but here is the iteration one version // ** People can view workouts in 4 modes (everyone, watchlist, friends, just them) // -In the case of running, cycling, swimming, and STANDARD crossfit Wods... we are going to // be storing peoples best times for (distance rnages) and standard wods... so in these cases // we can shorted the query time when we do an "everyone" query // -Watch list and friends we do the work of getting every time that person did the workout.. not just // there best times. // -Same for the (you only) view // private void LoadFlexDataForWorkout(Workout workout, Affine.Utils.ConstsUtil.GraphContext context) { Affine.Data.Managers.IDataManager dataManager = Affine.Data.Managers.LINQ.DataManager.Instance; JavaScriptSerializer serializer = new JavaScriptSerializer(); // onload we send friend, watch, and group member data that is needed to the app aqufitEntities entities = new aqufitEntities(); IList<long> friendIds = entities.UserFriends.Where(f => (f.SrcUserSettingKey == ProfileSettings.Id || f.DestUserSettingKey == ProfileSettings.Id)).Select(f => (f.SrcUserSettingKey == ProfileSettings.Id ? f.DestUserSettingKey : f.SrcUserSettingKey)).ToList(); friendIds.Add(ProfileSettings.Id); // send the persons profile through var profile = new { UserName = ProfileSettings.UserName, Id = ProfileSettings.Id, FirstName = ProfileSettings.UserFirstName, LastName= ProfileSettings.UserLastName }; hiddenProfileJson.Value = serializer.Serialize(profile); var friends = entities.UserSettings.OfType<User>().Where(LinqUtils.BuildContainsExpression<User, long>(s => s.Id, friendIds)).Select(u => new { Id = u.Id, Un = u.UserName, Fn = u.UserFirstName, Ln = u.UserLastName }).ToArray(); hiddenFriendJson.Value = serializer.Serialize(friends); // get groups long[] groupIds = entities.UserFriends.Where(f => (f.SrcUserSettingKey == ProfileSettings.Id || f.DestUserSettingKey == ProfileSettings.Id) && f.Relationship > 0).Select(f => f.SrcUserSettingKey == ProfileSettings.Id ? f.DestUserSettingKey : f.SrcUserSettingKey).ToArray(); UserSettings[] groupList = entities.UserSettings.OfType<Group>().Where(LinqUtils.BuildContainsExpression<UserSettings, long>(us => us.Id, groupIds)).OrderBy(f => f.UserName).ToArray(); var groups = entities.UserSettings.OfType<Group>().Where(LinqUtils.BuildContainsExpression<Group, long>(s => s.Id, groupIds)).Select(u => new { Id = u.Id, Un = u.UserName }).ToArray(); hiddenGroupJson.Value = serializer.Serialize(groups); // next we load the workout compare to all (friends) Affine.WebService.StreamService ss = new WebService.StreamService(); //hiddenWorkout.Value = ss.getWorkout(workout.Id); Affine.Data.json.Workout jsonWorkout = dataManager.workoutToJsonWorkout(workout); Affine.Data.json.WOD wod = null; Affine.Data.json.WorkoutData[] jsonWorkoutData = null; //var workoutJson = new { Id = workout.Id, WorkoutTypeKey = workout.WorkoutTypeKey, Title = workout.Title, Date = workout.Date.ToLocalTime().ToShortDateString(), DataSrc = workout.DataSrc }; Affine.Utils.UnitsUtil.MeasureUnit weight = base.WeightUnits; if (workout.WorkoutTypeKey == (int)Affine.Utils.WorkoutUtil.WorkoutType.CROSSFIT) { if (context == Utils.ConstsUtil.GraphContext.DEFAULT) { // default context for crossfit is the watchlist context = Utils.ConstsUtil.GraphContext.WATCHLIST; } // if this is a crossfit workout // we want to get ALL the same WoDs for you and your friends IQueryable<Workout> workoutDataQuery = context == Utils.ConstsUtil.GraphContext.EVERYONE ? entities.UserStreamSet.Include("UserSettings").OfType<Workout>().Where(w => w.WOD.Id == workout.WOD.Id).AsQueryable() : context == Utils.ConstsUtil.GraphContext.WATCHLIST ? entities.UserStreamSet.Include("UserSettings").OfType<Workout>().Where(w => w.WOD.Id == workout.WOD.Id).Where(LinqUtils.BuildContainsExpression<Workout, long>(w => w.UserSetting.Id, friendIds)).AsQueryable() : context == Utils.ConstsUtil.GraphContext.FRIENDS ? entities.UserStreamSet.Include("UserSettings").OfType<Workout>().Where(w => w.WOD.Id == workout.WOD.Id).Where(LinqUtils.BuildContainsExpression<Workout, long>(w => w.UserSetting.Id, friendIds)).AsQueryable() : entities.UserStreamSet.Include("UserSettings").OfType<Workout>().Where(w => w.WOD.Id == workout.WOD.Id && w.UserSetting.Id == ProfileSettings.Id ).AsQueryable(); var workoutData = workoutDataQuery.Select(w => new { Id = w.Id, UsKey = w.UserSetting.Id, Un = w.UserSetting.UserName, T = w.Duration, S = w.Score, M = w.Max, Rx = w.RxD, D = w.Date }).ToArray(); wod = new Affine.Data.json.WOD (){ Id = workout.WOD.Id, Standard = workout.WOD.Standard, Type = workout.WOD.WODType.Id, Name = workout.WOD.Name, Description = workout.WOD.Description }; if (wod.Type == (int)Affine.Utils.WorkoutUtil.WodType.TIMED) { // time in sec jsonWorkoutData = workoutData.OrderByDescending(w => w.T).Select(w => new Affine.Data.json.WorkoutData() { UId = w.Un + "_" + w.Id, Id = w.Id, S = (double)w.T, Un = w.Un, D = w.D.ToLocalTime().ToShortDateString(), Rx = (w.Rx.HasValue ? w.Rx.Value : false) }).ToArray(); } else if (wod.Type == (int)Affine.Utils.WorkoutUtil.WodType.MAX_WEIGHT) { jsonWorkoutData = workoutData.OrderByDescending(w => w.M).Select(w => new Affine.Data.json.WorkoutData() { UId = w.Un + "_" + w.Id, Id = w.Id, S = Math.Round(Affine.Utils.UnitsUtil.systemDefaultToUnits(Convert.ToDouble(w.M), weight), 2), Un = w.Un, D = w.D.ToLocalTime().ToShortDateString(), Rx = (w.Rx.HasValue ? w.Rx.Value : false) }).ToArray(); } else { jsonWorkoutData = workoutData.OrderByDescending(w => w.S).Select(w => new Affine.Data.json.WorkoutData() { UId = w.Un + "_" + w.Id , Id = w.Id, S = Math.Round(Convert.ToDouble(w.S), 2), Un = w.Un, D = w.D.ToLocalTime().ToShortDateString(), Rx = (w.Rx.HasValue ? w.Rx.Value : false) }).ToArray(); } } else { // for now this will handle "running, swimming, walking, cycling" if (context == Utils.ConstsUtil.GraphContext.DEFAULT) { if (workout.DataSrc == (short)Utils.WorkoutUtil.DataSrc.MANUAL_NO_MAP || workout.DataSrc == (short)Utils.WorkoutUtil.DataSrc.MANUAL_WITH_MAP) { context = Utils.ConstsUtil.GraphContext.EVERYONE; } else if (workout.DataSrc == (short)Utils.WorkoutUtil.DataSrc.NIKE_NO_MAP) { context = Utils.ConstsUtil.GraphContext.WATCHLIST; } // TODO: not sure about this one.... /* else if (workout.DataSrc == (short)Utils.WorkoutUtil.DataSrc.MANUAL_WITH_MAP) { // TODO: ?? this is a special case that we want to compare all the times of people that have logged a run for the route. long mapRouteKey = entities.UserStreamSet.OfType<Workout>().Where(w => w.Id == workout.Id).Select(w => w.WorkoutExtendeds.FirstOrDefault().MapRoute.Id).FirstOrDefault(); IQueryable<Workout> workoutQuery = entities.WorkoutExtendeds.Include("UserStream").Where(we => we.MapRoute != null && we.MapRoute.Id == mapRouteKey).Select(we => we.UserStream).OfType<Workout>().OrderBy(w => w.Duration).Take(5000); workoutQuery.Select(w => w.UserSetting).ToArray(); // hydrate.. not working above so do it here. Workout[] data = workoutQuery.ToArray(); var wd = data.OrderByDescending(w => w.Duration).Select(w => new { UId = w.UserSetting.UserName + "_" + w.Id, Id = w.Id, S = w.Duration, Un = w.UserSetting.UserName, D = w.Date.ToLocalTime().ToShortDateString() }).ToArray(); var ret = new { WOD = new { }, WorkoutData = wd, Workout = workoutJson, Context = (int)context }; hiddenWorkoutData.Value = serializer.Serialize(ret); }*/ else { context = Utils.ConstsUtil.GraphContext.ME; } } if (context == Utils.ConstsUtil.GraphContext.EVERYONE) { // we want to compare achievments... IQueryable<Workout> workoutQuery = entities.Achievements.Include("UserStream").Include("UserStream.UserSetting").Where(a => a.AchievementType.WorkoutType.Id == workout.WorkoutTypeKey && workout.Distance >= a.AchievementType.DistanceRangeA && workout.Distance < a.AchievementType.DistanceRangeB).Take(5000).Select(a => a.UserStream).OfType<Workout>().AsQueryable(); workoutQuery = workoutQuery.Union(entities.UserStreamSet.OfType<Workout>().Where(w => w.Id == workout.Id)); workoutQuery.Select(w => w.UserSetting).ToArray(); // hydrate.. not working above so do it here. Workout[] data = workoutQuery.ToArray(); jsonWorkoutData = data.OrderByDescending(w => w.Duration).Select(w => new Affine.Data.json.WorkoutData() { UId = w.UserSetting.UserName + "_" + w.Id, Id = w.Id, S = w.Duration.Value, Un = w.UserSetting.UserName, D = w.Date.ToLocalTime().ToShortDateString() }).ToArray(); } else if (context == Utils.ConstsUtil.GraphContext.WATCHLIST || context == Utils.ConstsUtil.GraphContext.FRIENDS) { IQueryable<Workout> workoutQuery = entities.Achievements.Include("UserStream").Include("UserStream.UserSetting").Where(a => a.AchievementType.WorkoutType.Id == workout.WorkoutTypeKey && workout.Distance >= a.AchievementType.DistanceRangeA && workout.Distance < a.AchievementType.DistanceRangeB). Where(LinqUtils.BuildContainsExpression<Achievement, long>(a => a.UserSetting.Id, friendIds)).Take(5000).Select(a => a.UserStream).OfType<Workout>().AsQueryable(); workoutQuery = workoutQuery.Union(entities.UserStreamSet.OfType<Workout>().Where(w => w.Id == workout.Id)); workoutQuery.Select(w => w.UserSetting).ToArray(); // hydrate.. not working above so do it here. Workout[] data = workoutQuery.ToArray(); jsonWorkoutData = data.OrderByDescending(w => w.Duration).Select(w => new Affine.Data.json.WorkoutData() { UId = w.UserSetting.UserName + "_" + w.Id, Id = w.Id, S = w.Duration.Value, Un = w.UserSetting.UserName, D = w.Date.ToLocalTime().ToShortDateString() }).ToArray(); } else if (context == Utils.ConstsUtil.GraphContext.ME) // this is just you for the ranges.. since no map data { IQueryable<Workout> workoutQuery = entities.Achievements.Include("UserStream").Include("UserStream.UserSetting").Where(a => a.UserSetting.Id == ProfileSettings.Id && a.AchievementType.WorkoutType.Id == workout.WorkoutTypeKey && workout.Distance >= a.AchievementType.DistanceRangeA && workout.Distance < a.AchievementType.DistanceRangeB). Take(5000).Select(a => a.UserStream).OfType<Workout>().AsQueryable(); workoutQuery = workoutQuery.Union(entities.UserStreamSet.OfType<Workout>().Where(w => w.Id == workout.Id)); workoutQuery.Select(w => w.UserSetting).ToArray(); // hydrate.. not working above so do it here. Workout[] data = workoutQuery.ToArray(); jsonWorkoutData = data.OrderByDescending(w => w.Duration).Select(w => new Affine.Data.json.WorkoutData() { UId = w.UserSetting.UserName + "_" + w.Id, Id = w.Id, S = w.Duration.Value, Un = w.UserSetting.UserName, D = w.Date.ToLocalTime().ToShortDateString() }).ToArray(); } } var retWorkout = new { WOD = wod, WorkoutData = jsonWorkoutData, Workout = jsonWorkout, Context = (int)context }; hiddenWorkoutData.Value = serializer.Serialize(retWorkout); }