public async Task <IActionResult> AddExerciseToWorkout(WorkoutViewModel model, string returnUrl = null) { List <Exercise> ExercisesList = getExercisesFromAPI(); ModelState.Remove("WorkoutName"); ModelState.Remove("WorkoutDescription"); ModelState.Remove("StartDate"); ModelState.Remove("EndDate"); ModelState.Remove("CustomName"); ModelState.Remove("CustomDescription"); ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { string userID = _userManager.GetUserId(User); int programID = model.ProgramID; int exerciseID = model.ExerciseID; int day = model.Day; int amount = model.Amount; int aunit = model.Aunit; int? weight = model.Weight; int? wunit = model.Wunit; if (weight != null && wunit < 1) { TempData["ErrorMessage"] = "If you enter a weight, you need to enter the weight unit as well."; ViewBag.Exercises = ExercisesList; ViewBag.Workouts = getWorkoutsForUser(); return(View("Program", model)); } TrainingProgram tp = await _context.Workouts.FirstOrDefaultAsync(X => X.id == programID); ExerciseSets e_set = new ExerciseSets { amount = amount, aunit = aunit, weight = (weight == null ? 0 : (int)weight), wunit = (wunit == null ? 0 : (int)wunit) }; ProgramExercises p_ex = new ProgramExercises { ExerciseID = exerciseID, day = day }; p_ex.SetInfo.Add(e_set); tp.Exercices.Add(p_ex); _context.Workouts.Update(tp); await _context.SaveChangesAsync(); ViewBag.Exercises = ExercisesList; ViewBag.Workouts = getWorkoutsForUser(); TempData["SuccessMessage"] = "Congrats! Your Exercise has been added successfully."; return(View("Program", new WorkoutViewModel())); } ViewBag.Exercises = ExercisesList; ViewBag.Workouts = getWorkoutsForUser(); TempData["ErrorMessage"] = "Oops... Something hapenned..."; return(View("Program", model)); }
public async Task <IActionResult> AddExerciseToWorkout(ExercisesViewModel model, string returnUrl = null) { AddToWorkoutData dataModel = model.AddData; ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { string userID = _userManager.GetUserId(User); int programID = dataModel.ProgramID; int exerciseID = dataModel.ExerciseID; int day = dataModel.Day; int amount = dataModel.Amount; int aunit = dataModel.Aunit; int? weight = dataModel.Weight; int? wunit = dataModel.Wunit; if (weight != null && wunit < 1) { TempData["ErrorMessage"] = "If you enter a weight, you need to enter the weight unit as well."; ViewBag.Workouts = getWorkoutsForUser(); return(View("Exercises", model)); } TrainingProgram tp = await _context.Workouts.FirstOrDefaultAsync(X => X.id == programID); ExerciseSets e_set = new ExerciseSets { amount = amount, aunit = aunit, weight = (weight == null ? 0 : (int)weight), wunit = (wunit == null ? 0: (int)wunit) }; ProgramExercises p_ex = new ProgramExercises { ExerciseID = exerciseID, day = day }; p_ex.SetInfo.Add(e_set); tp.Exercices.Add(p_ex); _context.Workouts.Update(tp); await _context.SaveChangesAsync(); ViewBag.Workouts = getWorkoutsForUser(); TempData["SuccessMessage"] = "Congrats! Your exercise has been added successfully."; if (model.Search == null) { return(RedirectToAction("Exercises")); } else { return(RedirectToAction("Exercises")); } } ViewBag.Workouts = getWorkoutsForUser(); TempData["ErrorMessage"] = "Oops... Something hapenned..."; return(View("Exercises", model)); }