/* Creates a new workout with some exercises, where each exercise have a number of sets.
         */
        public bool CreateWorkout(Workout w, List<Tuple<Exercise, int>> exercises, int firstActivityId)
        {
            //to avoid inconsistency, the queries will be batch committed when the input has been validated.
            SQLiteTransaction transaction = this.db.BeginTransaction();
            Dictionary<string, object> parameters = new Dictionary<string, object>() {
                { "@workoutName", w.workoutName },
                { "@workoutVersion", w.workoutVersion }};
            SQLiteCommand cmd = this.db.GetPreparedStatement("insertWorkout", parameters);
            cmd.Transaction = transaction;
            this.db.DoNonQueryPrepared(cmd);

            int i = 0;
            foreach (Tuple<Exercise, int> exercise in exercises)
            {
                Exercise e = exercise.Item1;
                int numSets = exercise.Item2;
                parameters = new Dictionary<string, object>() { { "@activityId", firstActivityId + i },
                    { "@seqNum", i }, { "@exerciseName", e.exerciseName }, { "@exerciseVersion", e.exerciseVersion },
                    { "@workoutName", w.workoutName },{ "@workoutVersion", w.workoutVersion }, { "@numSets", numSets}};

                cmd = this.db.GetPreparedStatement("insertActivity", parameters);
                cmd.Transaction = transaction;
                this.db.DoNonQueryPrepared(cmd);

                i++;
            }

            bool status = this.db.CommitTransaction(transaction);
            transaction.Dispose();
            return status;
            
        }
        /* Adds the most recent version of the workout to the date. 
         */
        public void AddWorkoutToDate(string workoutName, DateTime date)
        {
            if (DayHasWorkout(date))
            {
                MessageBox.Show("Trying to add multiple workouts to one date", "AddWorkoutToDate()",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            int workoutVersion = GetMaxWorkoutVersion(workoutName);
            if (workoutVersion < 0)
            {
                MessageBox.Show("The workout could not be found", "AddWorkoutToDate()",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            //insert the date and the sets. 
            SQLiteTransaction transaction = this.db.BeginTransaction();
            Dictionary<string, object> parameters = new Dictionary<string, object>() { { "@day", date.Day },
                { "@month", date.Month }, { "@year", date.Year }, { "@workoutName", workoutName },
                { "@workoutVersion", workoutVersion }, { "@comments", "" }};

            SQLiteCommand cmd = this.db.GetPreparedStatement("insertDate", parameters);
            cmd.Transaction = transaction;
            this.db.DoNonQueryPrepared(cmd);

            /* Get a list of all activities in the workout. If the workout has been added to a date previously, 
             * it is also convinient to pre-fill the workload fields with the workload from the last time.
             * Therefore also get a list of the most recent sets for this workout, if any.
             */
            Workout workout = new Workout(workoutName, workoutVersion);
            List<ActivityExercise> activities = GetActivitiesJoinExercises(workout);
            List<Set> mostRecentSetsInWorkout = GetMostRecentSetsInWorkout(workout);

            int i, j = 0, secs = 0, reps = 0;
            double weight = 0;
            foreach (ActivityExercise a in activities)
            {
                for (i = 0; i < a.activity.numSets; i++)
                {
                    if (mostRecentSetsInWorkout.Count != 0)
                    {
                        //if the list is not empty, then mostRecentSetsInWorkout[j] must correspond to 
                        //the set currently beeing added, because they belong to the same workout.
                        Set s = mostRecentSetsInWorkout[j];
                        weight = s.weight;
                        secs = s.secs;
                        reps = s.reps;
                        j++;
                    }//else the workload field will have the default value 0

                    parameters = new Dictionary<string, object>() { { "@day", date.Day },
                        { "@month", date.Month }, { "@year", date.Year }, { "@weight", weight }, { "@secs", secs },
                        { "@reps", reps }, { "@setSeqNum", i }, { "@activityId", a.activity.activityId }};

                    cmd = this.db.GetPreparedStatement("insertSet", parameters);
                    cmd.Transaction = transaction;
                    this.db.DoNonQueryPrepared(cmd);

                }
            }
            this.db.CommitTransaction(transaction);
            transaction.Dispose();
        }
        /* Returns a list of the activities associated with the workout. 
         * The list is sorted on seqnum.
         */
        public List<Activity> GetActivities(Workout w)
        {
            Dictionary<string, object> parameters = new Dictionary<string, object>() {
                { "@workoutName", w.workoutName },
                { "@workoutVersion", w.workoutVersion }};
            
            List<string> tables = new List<string>() { "activities" };

            return this.db.GetAll(tables, this.db.GetPreparedStatement("selectActivities", parameters),
                ActivityExtractor).Select(item => (Activity)item).ToList();
        }
        /* Returns a list of all sets on the most recent day that contained the workout w. 
         * The list will be sorted on seqNum then setSeqNum (although the activity part is not returned)
         * It will be empty if no results are found.
         */
        public List<Set> GetMostRecentSetsInWorkout(Workout w)
        {

            Dictionary<string, object> parameters = new Dictionary<string, object>() {
                { "@workoutName", w.workoutName },
                { "@workoutVersion", w.workoutVersion }};
            
            List<string> tables = new List<string>() { "sets" };

            return this.db.GetAll(tables, this.db.GetPreparedStatement(
                "selectMostRecentSetsInWorkout", parameters),
                SetExtractor).Select(item => (Set)item).ToList();
        }
Example #5
0
 public WorkoutDate(Workout workout, Date date)
 {
     this.workout = workout;
     this.date = date;
 }