Ejemplo n.º 1
0
        public IActionResult Create([FromBody] TrainingDayViewModel viewModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (string.IsNullOrWhiteSpace(viewModel.UserId) || viewModel.Year == 0 || viewModel.WeekOfYear == 0 ||
                        viewModel.DayOfWeek < 0 || viewModel.DayOfWeek > 6 || SessionUserId != viewModel.UserId)
                    {
                        return(BadRequest());
                    }

                    //Verify trainingWeek exist
                    var trainingWeekKey = new TrainingWeekKey()
                    {
                        UserId     = viewModel.UserId,
                        Year       = viewModel.Year,
                        WeekOfYear = viewModel.WeekOfYear
                    };
                    var trainingWeekScenario = new TrainingWeekScenario()
                    {
                        ManageTrainingDay = false
                    };
                    var trainingWeek = _trainingWeeksService.GetTrainingWeek(trainingWeekKey, trainingWeekScenario);

                    if (trainingWeek == null)
                    {
                        return(BadRequest(new WebApiException(string.Format(Translation.P0_NOT_EXIST, Translation.TRAINING_WEEK))));
                    }

                    //Verify valid week of year
                    if (viewModel.WeekOfYear > 0 && viewModel.WeekOfYear <= 52)
                    {
                        var trainingDay = ControllerUtils.TransformViewModelToTrainingDay(viewModel);
                        trainingDay = _trainingDaysService.CreateTrainingDay(trainingDay);
                        if (trainingDay == null)
                        {
                            return(BadRequest(new WebApiException(string.Format(Translation.IMPOSSIBLE_TO_CREATE_P0, Translation.TRAINING_DAY))));
                        }
                        else
                        {
                            return(new OkObjectResult(trainingDay));
                        }
                    }
                    else
                    {
                        return(BadRequest());
                    }
                }
                else
                {
                    return(BadRequest(new WebApiException(ControllerUtils.GetModelStateError(ModelState))));
                }
            }
            catch (Exception exception)
            {
                return(BadRequest(new WebApiException("Error", exception)));
            }
        }
Ejemplo n.º 2
0
        internal TrainingWeek CreateTrainingWeek(TrainingWeek trainingWeek)
        {
            TrainingWeek trainingWeekResult = null;

            trainingWeekResult = _trainingWeekModule.Create(trainingWeek);

            if (trainingWeek.TrainingDays != null)
            {
                trainingWeekResult.TrainingDays = new List <TrainingDay>();
                foreach (var trainingDay in trainingWeek.TrainingDays)
                {
                    trainingWeekResult.TrainingDays.Add(_trainingDaysService.CreateTrainingDay(trainingDay));
                }
            }
            return(trainingWeekResult);
        }