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"));
        }
        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));
        }
Пример #3
0
        public async Task <IActionResult> AddModifyTrainingSpecLine(TrainigSpecLineChangeViewModel viewModel)
        {
            ViewBag.Tittle = viewModel.Id == 0?localizer["TittleAdd"]:localizer["TittleModify"];

            if (viewModel == null)
            {
                ModelState.AddModelError("Incorrect TrainigSpecLineChangeViewModel", "Incorrect TrainigSpecLineChangeViewModel");
                return(RedirectToAction("List"));
            }

            var exercises = appContext.Exercises.ToList();

            if (!specValidator.IsSpecLineHasCorrectWeight(viewModel.ExerciseId, viewModel.Weight, exercises, out string shortMessage, out string longMessage))
            {
                ModelState.AddModelError(shortMessage, longMessage);
            }
            if (ModelState.IsValid)
            {
                if (viewModel.Id == 0)
                {
                    TrainingProgramSpec programSpec = new TrainingProgramSpec
                    {
                        HeadId = viewModel.HeadId,
                        ExId   = viewModel.ExerciseId,
                        Set    = viewModel.Set,
                        Weight = viewModel.Weight,
                        Amount = viewModel.Amount
                    };

                    await appContext.TrainingProgramSpecs.AddAsync(programSpec);

                    await appContext.SaveChangesAsync();
                }
                else
                {
                    TrainingProgramSpec programSpec = await appContext.TrainingProgramSpecs.FindAsync(viewModel.Id);

                    programSpec.ExId   = viewModel.ExerciseId;
                    programSpec.Set    = viewModel.Set;
                    programSpec.Weight = viewModel.Weight;
                    programSpec.Amount = viewModel.Amount;
                    appContext.Update(programSpec);
                    await appContext.SaveChangesAsync();
                }

                return(View("TrainingSpecList", Getmodel(viewModel.HeadId)));
            }
            viewModel.Exercises = exercises;
            return(View(viewModel));
        }