internal void EditWorkout(string userID, WorkoutSessionVM workoutToEdit) { var session = new WorkoutSession { Date = workoutToEdit.Date, Distance = workoutToEdit.Distance, Duration = workoutToEdit.Duration, Type = workoutToEdit.Type, SessionUserNote = workoutToEdit.SessionUserNote, UserId = userID, SessionName = workoutToEdit.SessionName, }; foreach (var exerciseVM in workoutToEdit.Exercises) { var exercise = new Exercise { ExerciseName = exerciseVM.Name }; session.Exercise.Add(exercise); foreach (var set in exerciseVM.Sets) { exercise.Set.Add(new Set { Reps = set.Reps, UsedWeight = set.Weight, UserNote = set.UserComment }); } } ; var oldWorkoutSession = WorkoutSession .Include(z => z.Exercise) .ThenInclude(y => y.Set) .FirstOrDefault(x => x.SessionName == workoutToEdit.SessionName && x.UserId == userID && x.Date == workoutToEdit.Date); foreach (var exercise in oldWorkoutSession.Exercise) { foreach (var set in exercise.Set) { Set.Remove(set); } //SaveChanges(); Exercise.Remove(exercise); } ; //SaveChanges(); WorkoutSession.Remove(oldWorkoutSession); WorkoutSession.Add(session); SaveChanges(); }
internal void DeleteWorkout(string userID, WorkoutSessionVM workoutToDelete) { var oldWorkoutSession = WorkoutSession .Include(z => z.Exercise) .ThenInclude(y => y.Set) .FirstOrDefault(x => x.SessionName == workoutToDelete.SessionName && x.UserId == userID && x.Date == workoutToDelete.Date); //== session; foreach (var exercise in oldWorkoutSession.Exercise) { foreach (var set in exercise.Set) { Set.Remove(set); } //SaveChanges(); Exercise.Remove(exercise); } ; //SaveChanges(); WorkoutSession.Remove(oldWorkoutSession); SaveChanges(); }