Beispiel #1
0
        public void SaveWorkout(SaveWorkoutInputModel model)
        {
            var recordedSet = _workoutHistoryRepository.GetAll()
                              .Where(x => x.Workout_Fk == model.ProgramId &&
                                     x.ExerciseProgramExercise_Fk == model.ExerciseId &&
                                     x.SetNumber == model.Set)
                              .FirstOrDefault();

            if (recordedSet != null)
            {
                recordedSet.Repititions  = model.Reps;
                recordedSet.WeightUsed   = model.Weight;
                recordedSet.ModifiedBy   = Environment.UserName;
                recordedSet.ModifiedDate = DateTime.Now;

                _workoutHistoryRepository.Update(recordedSet);
            }
            else
            {
                var workout = new WorkoutHistory
                {
                    Workout_Fk = model.ProgramId,
                    ExerciseProgramExercise_Fk = model.ExerciseId,
                    SetNumber   = model.Set,
                    Repititions = model.Reps,
                    WeightUsed  = model.Weight,
                    StartDate   = DateTime.Now,
                    CreatedBy   = Environment.UserName,
                    CreateDate  = DateTime.Now
                };

                _workoutHistoryRepository.Insert(workout);
            }
        }
Beispiel #2
0
 public void DeleteWorkout()
 {
     NotifyOfPropertyChange(() => CanDeleteWorkout);
     WorkoutHistory.Delete(SelectedPastWorkout.Id);
     PastWorkouts.Remove(SelectedPastWorkout);
     NotifyOfPropertyChange(() => PastWorkouts);
 }
        private WorkoutHistory CloneWorkoutHistory(WorkoutHistory wh)
        {
            var workoutHistory = new WorkoutHistory();

            foreach (var m in wh.MuscleExercises)
            {
                var me = new MuscleExercises {
                    MuscleType = m.MuscleType
                };
                foreach (var e in m.Exercises)
                {
                    var ne = new WorkoutExercise {
                        Name = e.Name
                    };
                    foreach (var s in e.Sets)
                    {
                        ne.Sets.Add(new Set()
                        {
                            Reps = s.Reps, Rest = s.Rest
                        });
                    }

                    me.Exercises.Add(ne);
                }

                workoutHistory.MuscleExercises.Add(me);
            }

            return(workoutHistory);
        }
Beispiel #4
0
        public void LoadWorkoutHistory()
        {
            BindingList <WorkoutDetailModel> workouts = new BindingList <WorkoutDetailModel>();

            List <WorkoutDetailModel> workoutHistoryList = WorkoutHistory.Get().OrderBy(x => x.DateOfWorkout).ToList();

            PastWorkouts = new BindingList <WorkoutDetailModel>(workoutHistoryList);
        }
 public WorkoutTest()
 {
     set.Excercise.Name = "deadlift";
     set.Repetitions    = 5;
     set.Weight         = 255;
     set.StartTime      = DateTime.Now;
     set.FinishTime     = set.StartTime.AddMinutes(1);
     workoutHistory     = new WorkoutHistory();
     workout            = workoutHistory.StartNewWorkout();
 }
        private void buttonSaveWorkout_Click(object sender, EventArgs e)
        {
            bool everythingIsGreat = CheckInputValues();

            if (!everythingIsGreat)
            {
                MessageBox.Show("You done messed up");
                return;
            }

            // Run query to insert new training sesion into the database (needs a new table). Requires user id/session id
            int workoutSessionId = WorkoutHistory.InsertSessionIntoDatabase(clientId, sessionId);

            // Check the value of the session ID to see it was inserted correctly
            if (workoutSessionId == -99)
            {
                MessageBox.Show("Sorry, there was an error saving your workout.  Please check the log");
                return;
            }

            // TODO: Need to handle errors
            foreach (DataRow row in ClientDataForm.recordingExercisesTable.Rows)
            {
                int exerciseId = 0;
                int sets       = 0;
                int reps       = 0;
                int weight     = 0;
                int time       = 0;

                try
                {
                    exerciseId = Convert.ToInt32(row["ID"]);
                    sets       = Convert.ToInt32(row["Sets"]);
                    reps       = Convert.ToInt32(row["Reps"]);
                    weight     = Convert.ToInt32(row["Weight"]);
                    time       = Convert.ToInt32(row["Time"]);
                } catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

                // Get the result of inserting the data into the client_workout_history_exercises table
                int insertValue = WorkoutHistory.InsertExerciseIntoHistory(workoutSessionId, exerciseId, sets, reps, weight, time);

                if (insertValue == -99)
                {
                    MessageBox.Show("Something went wrong inserting your exercises.  Please check the log");
                }
                else
                {
                    MessageBox.Show("Successfully saved workout information");
                    this.Close();
                }
            }
        }
        public IActionResult WorkoutHistoryAjax(int workoutHistoryId)
        {
            WorkoutHistory workoutHistory = _db.WorkoutHistories.Single(x => x.Id == workoutHistoryId);

            return(new JsonResult(new
            {
                partial = this.RenderViewAsync("Program/_WorkoutHistoryPartial",
                                               new WorkoutHistoryViewModel(workoutHistory), true),
                name = workoutHistory.Workout.Name
            }));
        }
        public void AddWorkout()
        {
            WorkoutModel selectedWorkout = SelectedWorkout;

            NotifyOfPropertyChange(() => CanAddWorkout);
            try
            {
                WorkoutHistory.Add(SelectedWorkout.WorkoutName, Convert.ToDateTime(WorkoutDate));
                AddWorkoutSuccessful = "Workout Added Successfully";
            }
            catch (Exception ex)
            {
                AddWorkoutSuccessful = ex.Message;
            }
        }
        private WorkoutHistoryViewModel GetWorkoutHistoryViewModel(WorkoutHistory before, WorkoutHistory after)
        {
            var afterVm = new WorkoutHistoryViewModel(after);


            for (int i = 0; i < afterVm.MuscleExerciseViewModels.Count; i++)
            {
                for (int j = 0; j < afterVm.MuscleExerciseViewModels[i].Exercises.Count; j++)
                {
                    var afterExercise = afterVm.MuscleExerciseViewModels[i].Exercises[j];
                    if (j == before.MuscleExercises[i].Exercises.Count)
                    {
                        afterExercise.AddedExercise = true;
                        break;
                    }

                    var beforeExercise = before.MuscleExercises[i].Exercises[j];
                    for (int k = 0; k < afterExercise.SetViewModels.Count; k++)
                    {
                        var afterSet = afterExercise.SetViewModels[k];
                        if (k == beforeExercise.Sets.Count)
                        {
                            afterSet.SetAdded = true;
                            break;
                        }

                        var beforeSet = beforeExercise.Sets[k];

                        if (afterSet.Reps != beforeSet.Reps)
                        {
                            afterSet.RepsChanged = true;
                        }

                        if (afterSet.Rest != beforeSet.Rest)
                        {
                            afterSet.RestChanged = true;
                        }
                    }
                }
            }

            return(afterVm);
        }
        public IActionResult ProgressPartialAjax(int id)
        {
            var            workout                = _db.Workouts.Single(x => x.Id == id);
            var            lastWorkoutHistory     = workout.WorkoutHistories.Last();
            WorkoutHistory progressWorkoutHistory = ProgressWorkout(lastWorkoutHistory, workout);

            workout.WorkoutHistories.Add(progressWorkoutHistory);

            _db.SaveChanges();
            var workoutHistoryVm = GetWorkoutHistoryViewModel(lastWorkoutHistory, progressWorkoutHistory);

            return(new JsonResult(new
            {
                partial = this.RenderViewAsync("Program/_WorkoutHistoryPartial",
                                               workoutHistoryVm, true),
                name = workout.Name,
                count = workout.WorkoutHistories.Count
            }));
        }
        static void Main(string[] args)
        {
            WorkoutHistory workoutList = new WorkoutHistory();
            var            workout     = workoutList.StartNewWorkout();

            for (int i = 0; i < 4; i++)
            {
                workout.Sets.Add(new Set {
                    Repetitions = 10, StartTime = DateTime.Now, FinishTime = DateTime.Now.AddMinutes(1), Excercise = new Excercise {
                        Name = "Deadlift"
                    }, Weight = 225
                });
            }

            Console.WriteLine("Automatically entered workout");

            foreach (var set in workoutList.Workouts[0].Sets)
            {
                Console.WriteLine("Excercise:{0}, Weight:{1}, Reps:{2}", set.Excercise.Name, set.Weight, set.Repetitions);
            }

            Console.ReadLine();
        }
        public async Task Write(Workout workout)
        {
            var            history = Read(workout.RoutineId);
            WorkoutRoutine routine = null;

            if (history == null)
            {
                history = new WorkoutHistory(getKeyFromRoutine(workout.RoutineId));
            }
            else
            {
                routine = getMostRecentNotCompletedWorkout(history);
            }
            if (routine == null)
            {
                history.WorkoutRoutines.Add(new WorkoutRoutine());
            }

            routine.Workouts.Add(workout);
            history.key = getKeyFromRoutine(workout.RoutineId);
            history.WorkoutRoutinesJSON = JsonConvert.SerializeObject(history.WorkoutRoutines);
            await SaveAsync(history, _config);
        }
 public WorkoutHistoryViewModel(WorkoutHistory wh)
 {
     Id = wh.Id;
     MuscleExerciseViewModels = wh.MuscleExercises.Select(m => new MuscleExerciseViewModel(m)).ToList();
 }
Beispiel #14
0
        public async Task CorrectlyCreateWorkoutHistoryWithExerciseHistories()
        {
            var(connection, options) = await CreateUniqueMockDbConnectionForThisTest(null);

            try
            {
                using (var context = new FittifyContext(options))
                {
                    var listWorkouts = new List <Workout>()
                    {
                        new Workout()
                        {
                            Name = "WorkoutA", OwnerGuid = _ownerGuid
                        },
                        new Workout()
                        {
                            Name = "WorkoutB", OwnerGuid = _ownerGuid
                        },
                        new Workout()
                        {
                            Name = "WorkoutC", OwnerGuid = null
                        },
                        new Workout()
                        {
                            Name = "WorkoutD", OwnerGuid = null
                        }
                    };

                    var listExercises = new List <Exercise>()
                    {
                        new Exercise()
                        {
                            Name = "ExerciseA", OwnerGuid = _ownerGuid
                        },
                        new Exercise()
                        {
                            Name = "ExerciseB", OwnerGuid = _ownerGuid
                        },
                        new Exercise()
                        {
                            Name = "ExerciseC", OwnerGuid = null
                        },
                        new Exercise()
                        {
                            Name = "ExerciseD", OwnerGuid = null
                        },
                    };

                    var listMapExerciseWorkouts = new List <MapExerciseWorkout>();

                    foreach (var workout in listWorkouts)
                    {
                        foreach (var exercise in listExercises)
                        {
                            if (workout.OwnerGuid != null &&
                                exercise.OwnerGuid != null &&
                                workout.OwnerGuid == exercise.OwnerGuid)
                            {
                                listMapExerciseWorkouts.Add(new MapExerciseWorkout()
                                {
                                    OwnerGuid = _ownerGuid,
                                    Workout   = workout,
                                    Exercise  = exercise
                                });
                            }
                            else if (workout.OwnerGuid == null &&
                                     exercise.OwnerGuid == null &&
                                     workout.OwnerGuid == exercise.OwnerGuid)
                            {
                                listMapExerciseWorkouts.Add(new MapExerciseWorkout()
                                {
                                    OwnerGuid = null,
                                    Workout   = workout,
                                    Exercise  = exercise
                                });
                            }
                            // we don't want Guid && null or null && Guid
                        }
                    }

                    context.MapExerciseWorkout.AddRange(listMapExerciseWorkouts);
                    context.SaveChanges();
                }

                using (var context = new FittifyContext(options))
                {
                    //var Workouts = context.Workouts.ToList();
                    var newWorkoutHistory = new WorkoutHistory()
                    {
                        Workout = context.Workouts.ToList().FirstOrDefault(f => f.OwnerGuid != null)
                    };
                    var repo = new WorkoutHistoryRepository(context);
                    var newlyCreatedWorkoutHistory =
                        await repo.CreateIncludingExerciseHistories(newWorkoutHistory, _ownerGuid);

                    var serializedNewlyCreatedWorkoutHistoryReturnedFromRepo = JsonConvert.SerializeObject(newlyCreatedWorkoutHistory,
                                                                                                           new JsonSerializerSettings()
                    {
                        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                    });

                    var newlyCreatedWorkoutHistoryFromContext = context
                                                                .WorkoutHistories
                                                                .Include(i => i.ExerciseHistories)
                                                                .Include(i => i.Workout)
                                                                .FirstOrDefault(f => f.OwnerGuid != null);
                    var serializedNewlyCreatedWorkoutHistoryReturnedFromContext = JsonConvert.SerializeObject(newlyCreatedWorkoutHistory,
                                                                                                              new JsonSerializerSettings()
                    {
                        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                    });


                    Assert.AreEqual(serializedNewlyCreatedWorkoutHistoryReturnedFromRepo,
                                    serializedNewlyCreatedWorkoutHistoryReturnedFromContext);
                    Assert.AreEqual(newlyCreatedWorkoutHistory.ExerciseHistories.Count(), 2);
                    Assert.AreEqual(newlyCreatedWorkoutHistoryFromContext.ExerciseHistories.Count(), 2);

                    foreach (var exerciseHistory in newlyCreatedWorkoutHistory.ExerciseHistories)
                    {
                        Assert.AreEqual(exerciseHistory.PreviousExerciseHistory, null);
                    }
                }

                using (var context = new FittifyContext(options))
                {
                    var existingExerciseHistories =
                        context.ExerciseHistories.Where(w => w.OwnerGuid == _ownerGuid).ToList();


                    existingExerciseHistories.FirstOrDefault().WeightLiftingSets = new List <WeightLiftingSet>()
                    {
                        new WeightLiftingSet()
                        {
                            RepetitionsFull = 30
                        }
                    };

                    existingExerciseHistories.Skip(1).FirstOrDefault().CardioSets = new List <CardioSet>()
                    {
                        new CardioSet()
                        {
                            DateTimeStart = new DateTime(1989, 11, 01, 13, 10, 00),
                            DateTimeEnd   = new DateTime(1989, 11, 01, 13, 50, 00)
                        }
                    };

                    //var Workouts = context.Workouts.ToList();
                    var newWorkoutHistory = new WorkoutHistory()
                    {
                        Workout = context.Workouts.ToList().FirstOrDefault(f => f.OwnerGuid != null)
                    };
                    var repo = new WorkoutHistoryRepository(context);
                    var newlyCreatedWorkoutHistory =
                        await repo.CreateIncludingExerciseHistories(newWorkoutHistory, _ownerGuid);

                    foreach (var exerciseHistory in newlyCreatedWorkoutHistory.ExerciseHistories)
                    {
                        Assert.AreNotEqual(exerciseHistory.PreviousExerciseHistory, null);
                    }
                }
            }
            finally
            {
                connection.Close();
            }
        }
        private WorkoutHistory ProgressWorkout(WorkoutHistory wh, Workout w)
        {
            var workoutHistory = CloneWorkoutHistory(wh);
            var r = new Random();

            foreach (var workoutHistoryMuscleExercise in workoutHistory.MuscleExercises)
            {
                var exercises = workoutHistoryMuscleExercise.Exercises;

                var mechanicalRepsCount = exercises.SelectMany(x => x.Sets).Count(x => x.Reps < 10);
                var stressRepsCount     = exercises.SelectMany(x => x.Sets).Count(x => x.Reps >= 10);

                int numOfExercisesToProgress = (int)Math.Ceiling((decimal)exercises.Count / 2);

                Dictionary <int, WorkoutExercise> selectedExercises;
                VolumeType vt;
                RepsType[] neededReps;

                if (mechanicalRepsCount == stressRepsCount)
                {
                    var volumeTypes = new[] { VolumeType.MetabolicStress, VolumeType.MechanicalTension };
                    vt = volumeTypes[r.Next(volumeTypes.Length)];
                }

                else if (stressRepsCount < mechanicalRepsCount)
                {
                    vt = VolumeType.MetabolicStress;
                }
                else
                {
                    vt = VolumeType.MechanicalTension;
                }

                switch (vt)
                {
                case VolumeType.MetabolicStress:
                    selectedExercises = exercises.Where(x => x.Sets.Any(s => s.Reps < 10)).Select((e, i) => new { i, e }).ToDictionary(x => x.i, x => x.e);
                    neededReps        = new[] { RepsType.HighAdvanced, RepsType.HighInter, RepsType.HighNovice };
                    break;

                case VolumeType.MechanicalTension:

                    selectedExercises = exercises.Where(x => x.Sets.Any(s => s.Reps >= 10)).Select((e, i) => new { i, e }).ToDictionary(x => x.i, x => x.e);
                    neededReps        = new[] { RepsType.Low, RepsType.MedAdvanced, RepsType.MedInter, RepsType.MedNovice };
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
                var modifyFuncs = new ProgramService.ModifyExerciseDelegate[]
                {
                    _programService.ModifyExerciseChangeReps,
                    _programService.ModifyExerciseChangeRest,
                    _programService.ModifyExerciseChangeSet
                };

                selectedExercises = selectedExercises.OrderBy(x => r.Next()).Take(numOfExercisesToProgress).ToDictionary(x => x.Key, x => x.Value);

                foreach (var selectedExercise in selectedExercises)
                {
                    var exerciseSettings = _programService.GetRelevantExerciseData(selectedExercise.Key,
                                                                                   w.Template.TrainerLevelType, workoutHistoryMuscleExercise.MuscleType);

                    var exerciseSetting = exerciseSettings.Where(x => neededReps.Any(y => y == x.Key))
                                          .OrderBy(x => r.Next())
                                          .Take(1).Select(x => x.Value).Single();

                    modifyFuncs = modifyFuncs.OrderBy(x => r.Next()).ToArray();
                    foreach (var modifyExerciseDelegate in modifyFuncs)
                    {
                        if (modifyExerciseDelegate.Invoke(selectedExercise.Value, exerciseSetting))
                        {
                            break;
                        }
                    }
                }
            }

            return(workoutHistory);
        }
 private WorkoutRoutine getMostRecentNotCompletedWorkout(WorkoutHistory wh)
 {
     return(wh.WorkoutRoutines.OrderByDescending(x => x.DateStarted).FirstOrDefault(x => x.DateCompleted == null));
 }
        CreateUniqueMockDbConnectionForThisTest()
        {
            SqliteConnection connection = null;
            DbContextOptions <FittifyContext> options = null;

            await Task.Run(() =>
            {
                connection = new SqliteConnection("DataSource=:memory:");
                connection.Open();

                options = new DbContextOptionsBuilder <FittifyContext>()
                          .UseSqlite(connection)
                          .Options;

                var listWorkouts = new List <Workout>()
                {
                    new Workout()
                    {
                        Name = "WorkoutA", OwnerGuid = _ownerGuid
                    },
                    new Workout()
                    {
                        Name = "WorkoutB", OwnerGuid = _ownerGuid
                    },
                    new Workout()
                    {
                        Name = "WorkoutC", OwnerGuid = null
                    },
                    new Workout()
                    {
                        Name = "WorkoutD", OwnerGuid = null
                    }
                };

                var listExercises = new List <Exercise>()
                {
                    new Exercise()
                    {
                        Name = "ExerciseA", OwnerGuid = _ownerGuid
                    },
                    new Exercise()
                    {
                        Name = "ExerciseB", OwnerGuid = _ownerGuid
                    },
                    new Exercise()
                    {
                        Name = "ExerciseC", OwnerGuid = null
                    },
                    new Exercise()
                    {
                        Name = "ExerciseD", OwnerGuid = null
                    },
                };

                var listMapExerciseWorkouts = new List <MapExerciseWorkout>();

                foreach (var workout in listWorkouts)
                {
                    foreach (var exercise in listExercises)
                    {
                        if (workout.OwnerGuid != null &&
                            exercise.OwnerGuid != null &&
                            workout.OwnerGuid == exercise.OwnerGuid)
                        {
                            listMapExerciseWorkouts.Add(new MapExerciseWorkout()
                            {
                                OwnerGuid = _ownerGuid,
                                Workout   = workout,
                                Exercise  = exercise
                            });
                        }
                        else if (workout.OwnerGuid == null &&
                                 exercise.OwnerGuid == null &&
                                 workout.OwnerGuid == exercise.OwnerGuid)
                        {
                            listMapExerciseWorkouts.Add(new MapExerciseWorkout()
                            {
                                OwnerGuid = null,
                                Workout   = workout,
                                Exercise  = exercise
                            });
                        }
                        // we don't want Guid && null or null && Guid
                    }
                }

                using (var context = new FittifyContext(options))
                {
                    // Adding Workouts, Exercise, MapWorkoutExercises
                    context.Database.EnsureCreated();
                    context.AddRange(listMapExerciseWorkouts);
                    context.SaveChanges();

                    // Creating a workoutHistory with ExerciseHistories
                    var firstWorkoutHistory = new WorkoutHistory()
                    {
                        OwnerGuid         = _ownerGuid,
                        Workout           = context.Workouts.FirstOrDefault(f => f.OwnerGuid == _ownerGuid),
                        ExerciseHistories = new List <ExerciseHistory>()
                        {
                            new ExerciseHistory(),
                            new ExerciseHistory()
                        }
                    };

                    context.Add(firstWorkoutHistory);
                    context.SaveChanges();

                    // Creating a workoutHistory of a different workout, but with the same exerciseHistories
                    var secondWorkoutHistory = new WorkoutHistory()
                    {
                        OwnerGuid         = _ownerGuid,
                        Workout           = context.Workouts.FirstOrDefault(f => f.OwnerGuid == null),
                        ExerciseHistories = new List <ExerciseHistory>()
                        {
                            new ExerciseHistory()
                            {
                                PreviousExerciseHistory = context.ExerciseHistories.FirstOrDefault()
                            },
                            new ExerciseHistory()
                            {
                                PreviousExerciseHistory = context.ExerciseHistories.Skip(1).FirstOrDefault()
                            }
                        }
                    };

                    context.Add(secondWorkoutHistory);
                    context.SaveChanges();

                    ////var thirdWorkoutHistory = new WorkoutHistory()
                    ////{
                    ////    OwnerGuid = _ownerGuid,
                    ////    Workout = context.Workouts.FirstOrDefault(f => f.OwnerGuid == _ownerGuid),
                    ////    ExerciseHistories = new List<ExerciseHistory>()
                    ////    {
                    ////        new ExerciseHistory()
                    ////        {
                    ////            PreviousExerciseHistory = context.ExerciseHistories.Skip(2).FirstOrDefault()
                    ////        },
                    ////        new ExerciseHistory()
                    ////        {
                    ////            PreviousExerciseHistory = context.ExerciseHistories.Skip(3).FirstOrDefault()
                    ////        }
                    ////    }
                    ////};

                    ////context.Add(thirdWorkoutHistory);
                    ////context.SaveChanges();

                    // Connecting Workout with the three WorkoutHistories
                    var workoutHistory     = context.WorkoutHistories.FirstOrDefault(f => f.ExerciseHistories != null && f.OwnerGuid != null);
                    workoutHistory.Workout = context.Workouts.FirstOrDefault();
                    context.SaveChanges();
                }
            });

            return(connection, options);
        }