public IActionResult Edit(EditWorkoutViewModel viewModel) { if (!ModelState.IsValid) { return(this.View(viewModel)); } var getExpression = new WorkoutWithDaysByWorkoutNameExpression(viewModel.WorkoutId, viewModel.Name); var workout = _repository.GetBy <WorkoutRoutine>(getExpression); var removeExpression = new WorkoutDaysByWorkoutRoutine(viewModel.WorkoutId); _repository.DeleteAllBy <WorkoutDay>(removeExpression); var workoutDays = SetUpInitialWorkoutDays(workout.Id, viewModel.Days, viewModel.ExerciseName); workout.Name = viewModel.Name; workout.IsPublic = viewModel.IsPublic; workout.WorkoutDays = workoutDays; _repository.Update <WorkoutRoutine>(workout); return(Redirect($"/Users/ViewProfile?username={this.User.Identity.Name}")); }
public async Task <IActionResult> Edit(EditWorkoutViewModel input) { if (!this.ModelState.IsValid) { return(this.View(input)); } await this.workoutsService.EditAsync(input); return(this.RedirectToAction("All")); }
// GET: Workouts/Edit/5 public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } using (workoutsRepo) using (exercisesRepo) using (userManager) { var workout = workoutsRepo .All() .Where(w => w.WorkoutId == id) .Include(w => w.WorkoutExercises) .ThenInclude(we => we.Exercise) .FirstOrDefault(); var exercises = workout.WorkoutExercises.Select(we => we.Exercise).ToList(); var exercisesId = exercises.Select(e => e.ExerciseId).ToList(); if (workout == null) { return(NotFound()); } var user = await userManager.GetUserAsync(HttpContext.User); var allUserExercises = exercisesRepo.All().Where(e => e.ApplicationUserId == user.Id).Select(e => new SelectListItem() { Text = e.ExerciseName, Value = e.ExerciseId.ToString() }).ToList(); foreach (var e in allUserExercises) { if (exercisesId.Contains(Int32.Parse(e.Value))) { e.Selected = true; } } ViewBag.AllExercises = allUserExercises; EditWorkoutViewModel vm = new EditWorkoutViewModel() { WorkoutId = workout.WorkoutId, WorkoutName = workout.WorkoutName, WorkoutDetails = workout.WorkoutDetails, Exercises = exercises.Select(e => e.ExerciseId.ToString()).ToList() }; return(View(vm)); } }
public async Task EditAsync(EditWorkoutViewModel input) { var workout = this.GetWorkoutById(input.Id); var position = this.positionsService.GetPositionByName(input.PositionName); workout.Name = input.Name; workout.PositionId = position.Id; workout.Description = input.Description; workout.VideoUrl = input.VideoUrl; this.workoutsRepository.Update(workout); await this.workoutsRepository.SaveChangesAsync(); }
public EditWorkoutViewModel GetWorkoutForEdit(string id) { var workout = this.GetWorkoutById(id); if (workout != null) { var position = this.positionsService.GetPositionById(workout.PositionId); var model = new EditWorkoutViewModel() { Id = workout.Id, Name = workout.Name, PositionName = position.Name, Description = workout.Description, VideoUrl = workout.VideoUrl, CoachId = workout.CoachId, }; return(model); } throw new InvalidOperationException(GlobalConstants.InvalidOperationExceptionInWorkoutForEditSearch); }
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)); }
public async Task EditWorkoutShouldUpdateCorrectly() { var workouts = new List <Workout> { new Workout { Id = "workoutId2312312312312", Name = "Workout Five", Description = "Some kind of randommm description again", Position = new Position { Id = "PositionFive", Name = PositionName.PowerForward, Description = "Power Forward position", Playstyle = "You play like a PF", }, PositionId = "PositionFive", VideoUrl = "test youtube link 5", Picture = new Picture { Id = "5pic", Url = "test 5url" }, PictureId = "5pic", ImageUrl = "5testimg", AddedByCoach = new Coach { Id = "c5", Name = "Coach5", Description = "desc5", Experience = 9, Phone = "34435212", Email = "*****@*****.**", User = new ApplicationUser { Id = "coachuser5" }, UserId = "coachuser5", Picture = new Picture { Id = "cpic5", Url = "test xurl5" }, PictureId = "cpic5", }, CoachId = "c5", }, }; this.positionsRepository.Setup(r => r.AllAsNoTracking()).Returns(() => new List <Position> { new Position { Id = "PositionFive", Name = PositionName.PowerForward, Description = "Power Forward position", Playstyle = "You play like a PF", }, }.AsQueryable()); var positionsService = new PositionsService(this.positionsRepository.Object); var coachesService = new CoachesService(this.coachRepository.Object, this.moqCloudinaryService.Object); this.workoutsRepository.Setup(r => r.AllAsNoTracking()).Returns(() => workouts.AsQueryable()); var service = new WorkoutsService(this.moqCloudinaryService.Object, this.workoutsRepository.Object, positionsService, coachesService); var model = new EditWorkoutViewModel { Id = "workoutId2312312312312", Name = "Changed name", Description = "Some kind of randommm description again", PositionName = PositionName.PowerForward, VideoUrl = "test youtube link CHANGED", CoachId = "c5", }; await service.EditAsync(model); var workout = service.GetWorkoutById("workoutId2312312312312"); Assert.Equal("Changed name", workout.Name); Assert.Equal("test youtube link CHANGED", workout.VideoUrl); this.workoutsRepository.Verify(x => x.AllAsNoTracking(), Times.AtMost(2)); }