public async Task <IActionResult> Skip(TrainingStepViewModel model) { var currentSpecPlanLine = appContext.TrainingPlanSpecs.Find(model.CurrentSpecPlanId); var currentSpecLine = GetCurrentSpecLine(currentSpecPlanLine, model.HeadTrainingId); var nextSpecPlanLine = GetNextSpecLine <TrainingPlanSpec>(currentSpecPlanLine.Id); if (currentSpecLine == null) { currentSpecLine = new TrainingSpec { HeadId = model.HeadTrainingId, ExId = model.ExerciseId, Set = model.Set, Weight = model.Weight, Amount = 0, Order = model.Order }; appContext.TrainingSpecs.Add(currentSpecLine); UpdateTrainingDuration(model.HeadTrainingId, DateTime.UtcNow); await appContext.SaveChangesAsync(); } if (nextSpecPlanLine == null) { return(RedirectToAction("NextStep", new { currentSpecPlanId = currentSpecPlanLine.Id, trainingPlanHeadId = model.HeadTrainingPlanId, trainingHeadId = model.HeadTrainingId, isFinishedTraining = true })); } return(RedirectToAction("NextStep", new { currentSpecPlanId = nextSpecPlanLine.Id, trainingPlanHeadId = model.HeadTrainingPlanId, trainingHeadId = model.HeadTrainingId })); }
public IActionResult NextStep(int currentSpecPlanId = 0, int trainingPlanHeadId = 0, int trainingHeadId = 0, bool isFinishedTraining = false) { if (currentSpecPlanId == 0) { return(RedirectToAction("Finish", new { trainingHeadId })); } var currentSpecPlanLine = appContext.TrainingPlanSpecs.Include(x => x.Exercise).Where(x => x.Id.Equals(currentSpecPlanId)).FirstOrDefault(); if (currentSpecPlanLine != null) { string userId = userManager.GetUserId(HttpContext.User); TrainingSpec currentSpecLine = GetCurrentSpecLine(currentSpecPlanLine, trainingHeadId); TrainingStepViewModel model; if (currentSpecLine == null) { model = new TrainingStepViewModel { HeadTrainingPlanId = trainingPlanHeadId, HeadTrainingId = trainingHeadId, CurrentSpecPlanId = currentSpecPlanId, CurrentSpecId = 0, Set = currentSpecPlanLine.Set, Weight = currentSpecPlanLine.Weight, ExerciseId = currentSpecPlanLine.ExId, Exercise = currentSpecPlanLine.Exercise.Name, ExerciseDescription = currentSpecPlanLine.Exercise.Description, Amount = currentSpecPlanLine.Amount, Order = currentSpecPlanLine.Order, IsFinishedTraining = isFinishedTraining }; } else { model = new TrainingStepViewModel { HeadTrainingPlanId = trainingPlanHeadId, HeadTrainingId = trainingHeadId, CurrentSpecPlanId = currentSpecPlanId, CurrentSpecId = currentSpecLine.Id, Set = currentSpecLine.Set, Weight = currentSpecLine.Weight, ExerciseId = currentSpecLine.ExId, Exercise = currentSpecLine.Exercise.Name, ExerciseDescription = currentSpecLine.Exercise.Description, Amount = currentSpecLine.Amount, Order = currentSpecLine.Order, IsFinishedTraining = isFinishedTraining }; } return(View(model)); } ModelState.AddModelError("Trainig specification line not found", "Trainig specification line not found"); return(View("Error")); }
public async Task <IActionResult> NextStep(TrainingStepViewModel model) { string userId = userManager.GetUserId(HttpContext.User); var currentSpecPlanId = model.CurrentSpecPlanId; var currentSpecPlanLine = appContext.TrainingPlanSpecs.Find(currentSpecPlanId); var currentSpecId = GetCurrentSpecLine(currentSpecPlanLine, model.HeadTrainingId) != null?GetCurrentSpecLine(currentSpecPlanLine, model.HeadTrainingId).Id : 0; if (currentSpecPlanId != 0) { if (model.CurrentSpecId != 0) // такая запись в TrainingSpec есть - модифицируем { var currentSpecLine = appContext.TrainingSpecs.Find(model.CurrentSpecId); if (currentSpecLine == null) { ModelState.AddModelError("IncorrectSpecId", $"Record {nameof(model.CurrentSpecId)} with not found in DataBase"); RedirectToAction("NextStep", new { currentSpecPlanId = model.CurrentSpecPlanId, trainingPlanHeadId = model.HeadTrainingPlanId, trainingHeadId = model.HeadTrainingId }); } else { currentSpecLine.Weight = model.Weight; currentSpecLine.Amount = model.Amount; appContext.TrainingSpecs.Update(currentSpecLine); await appContext.SaveChangesAsync(); } } else // новая запись в TrainingSpec - добавляем { var currentSpecLine = new TrainingSpec { HeadId = model.HeadTrainingId, ExId = model.ExerciseId, Set = model.Set, Weight = model.Weight, Amount = model.Amount, Order = appContext.TrainingPlanSpecs.Find(currentSpecPlanId).Order }; appContext.TrainingSpecs.Add(currentSpecLine); await appContext.SaveChangesAsync(); } //Найти следующую запись в тренировочной программе. var nextSpecPlanLine = GetNextSpecLine <TrainingPlanSpec>(model.CurrentSpecPlanId); if (nextSpecPlanLine == null) { return(RedirectToAction("NextStep", new { currentSpecPlanId = model.CurrentSpecPlanId, trainingPlanHeadId = model.HeadTrainingPlanId, trainingHeadId = model.HeadTrainingId, isFinishedTraining = true })); } else { return(RedirectToAction("NextStep", new { currentSpecPlanId = nextSpecPlanLine.Id, trainingPlanHeadId = model.HeadTrainingPlanId, trainingHeadId = model.HeadTrainingId })); } } else { return(View("Error")); } }
public async Task <IActionResult> AddModifySpecLine(TrainigSpecLineChangeViewModel viewModel) { ViewBag.Tittle = viewModel.Id != 0 ? localizer["TittleModify"] : localizer["TittleAdd"]; string userId = userManager.GetUserId(HttpContext.User); if (viewModel == null) { ModelState.AddModelError("Incorrect TrainigSpecLineChangeViewModel", "Incorrect TrainigSpecLineChangeViewModel"); return(RedirectToAction("AddTrainingManually")); } var exercises = appContext.Exercises.ToList(); if (!specLineValidator.IsSpecLineHasCorrectWeight(viewModel.ExerciseId, viewModel.Weight, exercises, out string shortMessage, out string longMessage)) { ModelState.AddModelError(shortMessage, longMessage); } if (ModelState.IsValid) { var spec = new TrainingSpec { HeadId = viewModel.HeadId, Set = viewModel.Set, ExId = viewModel.ExerciseId, Weight = viewModel.Weight, Amount = viewModel.Amount, Id = viewModel.Id }; if (viewModel.Id == 0) { documentSpecHandler.FillDocument(appContext.TrainingSpecs.Where(x => x.HeadId.Equals(viewModel.HeadId)).ToList()); spec.Order = documentSpecHandler.GetLastOrder(); await appContext.TrainingSpecs.AddAsync(spec); } else { spec = appContext.TrainingSpecs.Find(viewModel.Id); spec.Set = viewModel.Set; spec.ExId = viewModel.ExerciseId; spec.Weight = viewModel.Weight; spec.Amount = viewModel.Amount; appContext.TrainingSpecs.Update(spec); } await appContext.SaveChangesAsync(); int headId = viewModel.HeadId; return(RedirectToAction("ViewTrainingSpecs", new { headId = headId })); } else { viewModel.Exercises = exercises; } return(View(viewModel)); }