// GET: Workouts public async Task <IActionResult> Index() { using (workoutsRepo) using (userManager) { var user = await userManager.GetUserAsync(HttpContext.User); var workouts = workoutsRepo.All(); return(View(await workouts.Where(w => w.ApplicationUserId == user.Id).ToListAsync())); } }
// GET: Workouts/Create public async Task <IActionResult> Create() { using (exercisesRepo) using (userManager) { var user = await userManager.GetUserAsync(HttpContext.User); ViewBag.AllExerciseNames = exercisesRepo.All().Where(e => e.ApplicationUserId == user.Id).Select(e => new SelectListItem() { Text = e.ExerciseName, Value = e.ExerciseId.ToString() }).ToList(); } return(View()); }
public List <DetailedWorkoutViewModel> GetUserWorkoutHistory(string userId) { var workouts = completedWorkoutsRepo.All() .Where(cw => cw.ApplicationUserId == userId) .Include(cw => cw.Workout) .Include(cw => cw.CompletedExercises) .ThenInclude(ce => ce.Exercise); List <DetailedWorkoutViewModel> detailedWorkouts = workouts.Select(w => new DetailedWorkoutViewModel() { WorkoutId = w.WorkoutId, WorkoutName = w.Workout.WorkoutName, DateCreated = w.DateCreated, Comments = w.Comments, DetailedExercises = w.CompletedExercises.Select(ce => new DetailedExerciseViewModel() { ExerciseId = ce.Exercise.ExerciseId, ExerciseName = ce.Exercise.ExerciseName, TargetMins = ce.Exercise.TargetMins, TargetSets = ce.Exercise.TargetSets, TargetReps = ce.Exercise.TargetReps, TargetWeight = ce.Exercise.TargetWeight, Sets = ce.Sets, Reps = ce.Reps, Weight = ce.Weight, Mins = ce.Mins, Comments = ce.Comments }).ToList() }).ToList(); return(detailedWorkouts); }
public IEnumerable <DetailedExerciseViewModel> GetWorkoutExercises(string userId, int workoutId) { var workout = workoutsRepo .All() .Where(w => w.WorkoutId == workoutId && w.ApplicationUserId == userId) .Include(w => w.WorkoutExercises) .ThenInclude(we => we.Exercise) .FirstOrDefault(); return(workout.WorkoutExercises.Select(we => new DetailedExerciseViewModel() { ExerciseId = we.Exercise.ExerciseId, TargetMins = we.Exercise.TargetMins, TargetSets = we.Exercise.TargetSets, TargetReps = we.Exercise.TargetReps, TargetWeight = we.Exercise.TargetWeight, ExerciseName = we.Exercise.ExerciseName })); }
public async Task <IActionResult> Edit(int id, EditWorkoutViewModel vm) { if (id != vm.WorkoutId) { return(NotFound()); } if (ModelState.IsValid) { try { var user = await userManager.GetUserAsync(HttpContext.User); var exercises = exercisesRepo.All().Where(e => e.ApplicationUserId == user.Id); Workout workout = workoutsRepo.GetById(id); var selectedExercises = vm.Exercises.Select(e => Int32.Parse(e)); workout.WorkoutName = vm.WorkoutName; workout.WorkoutDetails = vm.WorkoutDetails; var filteredExercises = exercises.Where(e => selectedExercises.Contains(e.ExerciseId)).ToList(); var filteredExercisesIds = filteredExercises.Select(fe => fe.ExerciseId).ToList(); workoutsRepo.Update(workout); //get what is in db var oldExercises = weRepo.All().Where(we => we.WorkoutId == id).ToList(); var oldExercisesIds = oldExercises.Select(ce => ce.ExerciseId).ToList(); var oldWE = new WorkoutExercise(); //remove not selected foreach (var eId in oldExercisesIds) { if (!filteredExercisesIds.Contains(eId)) { oldWE = oldExercises.Where(oe => oe.ExerciseId == eId).FirstOrDefault(); weRepo.Delete(oldWE); } } //add selected not in db foreach (var e in filteredExercises) { if (!oldExercisesIds.Contains(e.ExerciseId)) { weRepo.Add(new WorkoutExercise() { Exercise = e, Workout = workout }); } } workoutsRepo.SaveChanges(); weRepo.SaveChanges(); } catch (DbUpdateConcurrencyException) { var exists = workoutsRepo.GetById(id);; if (exists == null) { return(NotFound()); } else { throw; } } finally { workoutsRepo.Dispose(); exercisesRepo.Dispose(); weRepo.Dispose(); userManager.Dispose(); } return(RedirectToAction(nameof(Index))); } return(View(vm)); }