public async Task <IActionResult> AddExFromTrainProgram(AddExercisesFromTrainingProgramViewModel viewModel)
        {
            if (viewModel != null)
            {
                var trainingProgramSpecs = appContext.TrainingProgramSpecs.Where(x => x.HeadId.Equals(viewModel.ProgramHeadId)).ToList();
                if (trainingProgramSpecs.Count != 0)
                {
                    var  userId = userManager.GetUserId(HttpContext.User);
                    int  headId = viewModel.HeadId;
                    byte SetNum = 1;
                    List <TrainingPlanSpec> lines;

                    #region Find last order number in current training plan
                    if (headId == 0)
                    {
                        headId = await CreateTrainigPlanHead(viewModel.DayTraining, CreateTrainingPlanName(viewModel.DayTraining));

                        lines = new List <TrainingPlanSpec>();
                    }
                    else
                    {
                        lines = appContext.TrainingPlanSpecs.Where(x => x.HeadId.Equals(viewModel.HeadId)).ToList();
                        if (lines.Count != 0)
                        {
                            SetNum = Convert.ToByte(lines.Max(x => x.Set) + 1);
                        }
                    }
                    documentSpecHandler.FillDocument(lines);
                    int lastOrderNum = documentSpecHandler.GetLastOrder();
                    int step         = documentSpecHandler.GetOrderStep();
                    #endregion
                    lines = new List <TrainingPlanSpec>();
                    foreach (var trainingProgramLine in trainingProgramSpecs)
                    {
                        lines.Add(new TrainingPlanSpec
                        {
                            HeadId = headId,
                            Set    = SetNum,
                            ExId   = trainingProgramLine.ExId,
                            Weight = trainingProgramLine.Weight,
                            Amount = trainingProgramLine.Amount,
                            Order  = lastOrderNum += step
                        });
                    }
                    await appContext.TrainingPlanSpecs.AddRangeAsync(lines);

                    await appContext.SaveChangesAsync();
                }
            }
            TempData["dayTraining"] = viewModel.DayTraining;
            return(RedirectToAction("Show"));
        }
        public async Task <IActionResult> AddExFromTrainPlan(AddExercisesFromPlanViewModel model)
        {
            if (model != null & model.HeadId != 0)
            {
                var  trainingPlanSpecs       = appContext.TrainingPlanSpecs.Where(x => x.HeadId.Equals(model.PlanHeadId)).ToList();
                var  userId                  = userManager.GetUserId(HttpContext.User);
                int  headId                  = model.HeadId;
                byte currentDocumentSetCount = 0;
                List <TrainingSpec> lines;
                if (trainingPlanSpecs.Count != 0)
                {
                    lines = appContext.TrainingSpecs.Where(x => x.HeadId.Equals(model.HeadId)).ToList();
                    if (lines.Count != 0)
                    {
                        currentDocumentSetCount = Convert.ToByte(lines.Max(x => x.Set));
                    }

                    documentSpecHandler.FillDocument(lines);
                    int lastOrderNum = documentSpecHandler.GetLastOrder();
                    int step         = documentSpecHandler.GetOrderStep();

                    lines = new List <TrainingSpec>();
                    foreach (var trainingPlanLine in trainingPlanSpecs)
                    {
                        lines.Add(new TrainingSpec
                        {
                            HeadId = headId,
                            Set    = (byte)(trainingPlanLine.Set + currentDocumentSetCount),
                            ExId   = trainingPlanLine.ExId,
                            Weight = trainingPlanLine.Weight,
                            Amount = trainingPlanLine.Amount,
                            Order  = lastOrderNum += step
                        });
                    }
                    await appContext.TrainingSpecs.AddRangeAsync(lines).ConfigureAwait(false);

                    await appContext.SaveChangesAsync().ConfigureAwait(false);

                    return(RedirectToAction("ViewTrainingSpecs", new { headId = headId }));
                }
            }
            return(RedirectToAction("Index", "Home"));
        }