public void CompleteWorkout(WorkoutDay workoutDay) { using (var db = new LiftBroContext()) { //TODO: use authenticated user User currentUser = db.Users.First(); //TOOD:unecessary attach? db.Users.Attach(currentUser); UserProgram currentUserProgram = db.UserPrograms .Include(userProgram => userProgram.Program.WorkoutDays) .Include(userProgram => userProgram.User) .FirstOrDefault(program => program.User.Username == User.Identity.Name && program.CurrentlyUsing); int nextOrder = workoutDay.Order + 1; WorkoutDay nextWorkoutDay = currentUserProgram.Program.WorkoutDays.FirstOrDefault(day => day.Order == nextOrder); //Program currentProgram = db.Programs // .Include(program => program.WorkoutDays) // .FirstOrDefault(program => // program.Id == currentUserProgram.Program.Id); if (nextWorkoutDay == null) nextWorkoutDay = currentUserProgram.Program.WorkoutDays.OrderBy(day => day.Order).First(); var attached = db.WorkoutDays.Find(nextWorkoutDay.Id); //attached.CurrentWorkoutDay = true; currentUserProgram.NextWorkout = attached; //nextWorkoutDay.CurrentWorkoutDay = true; db.CompletedWorkouts.Add(new CompletedWorkoutDay() { User = currentUser, When = DateTime.Now, Workout = db.WorkoutDays.FirstOrDefault(day => day.Id == workoutDay.Id) }); db.SaveChanges(); } }
public void Update(WorkoutDay workoutDay) { using (var db = new LiftBroContext()) { db.WorkoutDays.Attach(workoutDay); db.ChangeTracker.Entries<WorkoutDay>().First(e => e.Entity == workoutDay) .State = EntityState.Modified; db.SaveChanges(); } }