예제 #1
0
        public void UpdateRoutine(RoutineDetails routine)
        {
            var workoutIds = routine.Workouts.Select(w => w.Id).ToArray();

            using (var conn = CreateConnection())
                using (var tran = conn.BeginTransaction())
                {
                    try
                    {
                        conn.Execute("DELETE FROM RoutineExercise WHERE RoutineWorkoutId IN (SELECT Id FROM RoutineWorkout WHERE RoutineId=@Id)", new { routine.Id }, tran);
                        conn.Execute("DELETE FROM RoutineWorkout WHERE RoutineId=@Id", new { routine.Id }, tran);

                        for (var i = 0; i < routine.Workouts.Length; i++)
                        {
                            var workout = routine.Workouts[i];
                            if (workout.Id == Guid.Empty)
                            {
                                workout.Id = Guid.NewGuid();
                            }
                            conn.Execute("INSERT INTO RoutineWorkout(Id,RoutineId,[Index],Name,Frequency) VALUES(@Id,@RoutineId,@Index,@Name,@Frequency)", new
                            {
                                workout.Id,
                                RoutineId = routine.Id,
                                Index     = i,
                                workout.Name,
                                workout.Frequency
                            }, tran);
                        }

                        conn.Execute("UPDATE Routine SET Name=@Name WHERE Id=@Id", routine, tran);

                        conn.Execute("INSERT INTO RoutineExercise(Id,RoutineWorkoutId,[Index],ExerciseId,Sets,Reps,LoadFrom,LoadTo) VALUES(newid(),@RoutineWorkoutId,@Index,@ExerciseId,@Sets,@Reps,@LoadFrom,@LoadTo)", routine.Workouts.SelectMany(w => w.Exercises.Select((e, i) => new
                        {
                            RoutineWorkoutId = w.Id,
                            Index            = i,
                            e.ExerciseId,
                            e.Sets,
                            e.Reps,
                            e.LoadFrom,
                            e.LoadTo
                        })), tran);

                        tran.Commit();
                    }
                    catch
                    {
                        for (var i = 0; i < routine.Workouts.Length; i++)
                        {
                            routine.Workouts[i].Id = workoutIds[i];
                        }
                        tran.Rollback();
                        throw;
                    }
                }
        }
예제 #2
0
        public void CreateRoutine(RoutineDetails routine)
        {
            routine.Id      = Guid.NewGuid();
            routine.Created = DateTimeOffset.Now;
            foreach (var workout in routine.Workouts)
            {
                workout.Id = Guid.NewGuid();
            }
            using (var conn = CreateConnection())
                using (var tran = conn.BeginTransaction())
                {
                    try
                    {
                        conn.Execute("INSERT INTO Routine(Id, UserId, Name,Created,Active) VALUES(@Id, @UserId, @Name,@Created,@Active)", routine, tran);
                        conn.Execute("INSERT INTO RoutineWorkout(Id,RoutineId,[Index],Name,Frequency) VALUES(@Id,@RoutineId,@Index,@Name,@Frequency)", routine.Workouts.Select((w, i) => new
                        {
                            w.Id,
                            RoutineId = routine.Id,
                            Index     = i,
                            w.Name,
                            w.Frequency
                        }), tran);
                        conn.Execute("INSERT INTO RoutineExercise(Id,RoutineWorkoutId,[Index],ExerciseId,Sets,Reps,LoadFrom,LoadTo) VALUES(newid(),@RoutineWorkoutId,@Index,@ExerciseId,@Sets,@Reps,@LoadFrom,@LoadTo)", routine.Workouts.SelectMany(w => w.Exercises.Select((e, i) => new
                        {
                            RoutineWorkoutId = w.Id,
                            Index            = i,
                            e.ExerciseId,
                            e.Sets,
                            e.Reps,
                            e.LoadFrom,
                            e.LoadTo
                        })), tran);

                        tran.Commit();
                    }
                    catch
                    {
                        routine.Id = Guid.Empty;
                        foreach (var workout in routine.Workouts)
                        {
                            workout.Id = Guid.Empty;
                        }
                        tran.Rollback();
                        throw;
                    }
                }
        }
예제 #3
0
 public void RestoreRoutine(Guid id, out RoutineDetails routine)
 {
     throw new NotImplementedException();
 }