public async Task <IActionResult> AddModifyTrainingPlanLine(TrainigSpecLineChangeViewModel viewModel)
        {
            if (viewModel != null)
            {
                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)
                {
                    int headId = viewModel.HeadId;
                    if (viewModel.HeadId == 0)
                    {
                        headId = await CreateTrainigPlanHead(viewModel.DayTraining, CreateTrainingPlanName(viewModel.DayTraining));
                    }
                    documentSpecHandler.FillDocument(appContext.TrainingPlanSpecs.Where(x => x.HeadId.Equals(headId)).ToList());
                    var planSpecLine = new TrainingPlanSpec
                    {
                        HeadId = headId,
                        ExId   = viewModel.ExerciseId,
                        Set    = viewModel.Set,
                        Weight = viewModel.Weight,
                        Amount = viewModel.Amount,
                        Order  = documentSpecHandler.GetLastOrder()
                    };

                    if (viewModel.Id == 0)
                    {
                        await appContext.TrainingPlanSpecs.AddAsync(planSpecLine);

                        await appContext.SaveChangesAsync();
                    }
                    else
                    {
                        planSpecLine.Id = viewModel.Id;
                        var changingProgramPlanSpec = appContext.TrainingPlanSpecs.Find(planSpecLine.Id);
                        if (changingProgramPlanSpec != null)
                        {
                            changingProgramPlanSpec.ExId   = planSpecLine.ExId;
                            changingProgramPlanSpec.Set    = planSpecLine.Set;
                            changingProgramPlanSpec.Weight = planSpecLine.Weight;
                            changingProgramPlanSpec.Amount = planSpecLine.Amount;
                        }
                        appContext.TrainingPlanSpecs.Update(changingProgramPlanSpec);
                        await appContext.SaveChangesAsync();
                    }
                }
                else
                {
                    viewModel.Exercises = exercises.OrderBy(x => x.Name).ToList();
                    ViewBag.TrainingDay = viewModel.DayTraining;
                    ViewData["Tittle"]  = viewModel.Id == 0 ? (string)localizer["AddingExerciseToPlan"] : (string)localizer["ModifyPlanExercise"];
                    return(View(viewModel));
                }
            }
            TempData["dayTraining"] = viewModel.DayTraining;
            return(RedirectToAction("Show"));
        }
Beispiel #2
0
 private TrainingSpec GetCurrentSpecLine(TrainingPlanSpec currentSpecPlanLine, int trainingHeadId)
 {
     return(appContext.TrainingSpecs.Where(x => x.HeadId.Equals(trainingHeadId))
            .Where(x => x.Set.Equals(currentSpecPlanLine.Set) & x.ExId.Equals(currentSpecPlanLine.ExId) & x.Order.Equals(currentSpecPlanLine.Order))
            .Include(x => x.Exercise).FirstOrDefault());
 }