public void AddCompletedWorkout(string userId, CreateCompletedWorkoutViewModel vm) { var completedWorkout = new CompletedWorkout() { ApplicationUserId = userId, Comments = vm.Comments, DateCreated = DateTime.Now, WorkoutId = vm.WorkoutId }; completedWorkoutsRepo.Add(completedWorkout); completedExcercisesRepo.AddRange(vm.DetailedExercises.Select(de => new CompletedExercise() { ApplicationUserId = userId, DateCreated = DateTime.Now, Comments = de.Comments, ExerciseId = de.ExerciseId, CompletedWorkout = completedWorkout, Mins = de.Mins, Weight = de.Weight, Sets = de.Sets, Reps = de.Reps }).ToList()); completedWorkoutsRepo.SaveChanges(); completedExcercisesRepo.SaveChanges(); }
public async Task <IActionResult> Create(CreateWorkoutViewModel vm) { if (ModelState.IsValid) { using (exercisesRepo) using (workoutsRepo) using (weRepo) using (userManager) { var user = await userManager.GetUserAsync(HttpContext.User); var exercises = exercisesRepo.All().Where(e => e.ApplicationUserId == user.Id).ToList(); var selectedExercises = vm.Exercises.Select(e => Int32.Parse(e)); var filteredExcercises = exercises.Where(e => selectedExercises.Contains(e.ExerciseId)); Workout workout = new Workout() { WorkoutName = vm.WorkoutName, WorkoutDetails = vm.WorkoutDetails, DateCreated = DateTime.Now, ApplicationUserId = user.Id }; workoutsRepo.Add(workout); foreach (var e in filteredExcercises) { weRepo.Add(new WorkoutExercise() { Exercise = e, Workout = workout }); } workoutsRepo.SaveChanges(); weRepo.SaveChanges(); return(RedirectToAction(nameof(Index))); } } return(View(vm)); }
/// <summary> /// Create new ToDo /// </summary> /// <param name="model">Information about ToDo</param> public void CreateItem(ToDoItemViewModel model) { ToDoItem item = new ToDoItem() { Id = model.Id, Name = model.Name, Description = model.Description, Completed = model.Completed }; itemsRepo.Add(item); itemsRepo.SaveChanges(); }