private int FindHeadIdByDay(DayOfWeek dayTraining, string userId)
        {
            var head   = appContext.TrainingPlanHeads.Where(x => x.UserId.Equals(userId) & x.TrainingDays.Equals(trainigDayConverter.DaysToByte(dayTraining))).FirstOrDefault();
            int headId = head != null ? head.Id : 0;

            return(headId);
        }
Exemplo n.º 2
0
        public IActionResult Start()
        {
            DayOfWeek currentDay   = DateTime.UtcNow.DayOfWeek;
            string    userId       = userManager.GetUserId(HttpContext.User);
            var       trainingPlan = appContext.TrainingPlanHeads
                                     .Where(x => x.TrainingDays.Equals(dayConverter.DaysToByte(currentDay)))
                                     ?.ToList().OrderBy(x => x.Date)
                                     .LastOrDefault();

            if (trainingPlan == null)
            {
                ModelState.AddModelError(sharedErrorMessageLocalizer["ProgramPlanNotFoundShort"], sharedErrorMessageLocalizer["ProgramPlanNotFoundLong"]);
                return(View("Error"));
            }
            ;

            if (appContext.TrainingHeads.Where(x => x.TrainingPlanHeadId.Equals(trainingPlan.Id)).FirstOrDefault() != null)
            {
                ModelState.AddModelError(sharedErrorMessageLocalizer["TrainingAlreadyExistsShort"], sharedErrorMessageLocalizer["TrainingAlreadyExistsLong"]);
                return(View("Error"));
            }

            List <TrainingPlanSpec> planSpec = appContext.TrainingPlanSpecs.Include(x => x.Exercise)
                                               .Where(x => x.HeadId.Equals(trainingPlan.Id))
                                               .OrderBy(x => x.Order)
                                               .ToList();
            TrainigPlanViewModel model = new TrainigPlanViewModel
            {
                CurrentTrainingDay = currentDay,
                Id          = trainingPlan.Id,
                Date        = trainingPlan.Date.ToLocalTime(),
                Description = trainingPlan.Description,
                Name        = trainingPlan.Name,
                Lines       = planSpec,
            };

            return(View(model));
        }
        public IActionResult GetPlanSpecJson(DayOfWeek?day = null)
        {
            if (day != null)
            {
                var userId   = userManager.GetUserId(HttpContext.User);
                var specPlan = appContext.TrainingPlanHeads.Where(x => x.TrainingDays.Equals(dayConverter.DaysToByte(day.Value)) & x.UserId.Equals(userId))
                               .Join(appContext.TrainingPlanSpecs.Include(x => x.Exercise),
                                     head => head.Id,
                                     spec => spec.HeadId,
                                     (head, spec) => new { spec.Id, Exercise = spec.Exercise.Name, spec.Set, spec.Weight, spec.Amount, spec.Order });

                return(Json(specPlan));
            }
            else
            {
                throw new ArgumentNullException(nameof(day));
            }
        }