Пример #1
0
        public IActionResult CreateDay(DayDto dayDto)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("CreateDay", "bdbusr", new { id = dayDto.UserId }));
            }

            var day = _utilities.CreateDayEntity(dayDto.Name, dayDto.Date, dayDto.UserId);

            if (!_workoutRepository.Save() || day == null)
            {
                return(RedirectToAction("CreateDay", "bdbusr", new { id = dayDto.UserId }));
            }

            foreach (var e in dayDto.ExerciseNames)
            {
                if (e != "")
                {
                    var exercise = _workoutRepository.GetExerciseByName(e);
                    if (exercise == null)
                    {
                        exercise = _utilities.CreateExerciseEntity(e);
                        if (!_workoutRepository.Save())
                        {
                            return(RedirectToAction("CreateDay", "bdbusr", new { id = dayDto.UserId }));
                        }
                    }

                    var dayExercise = _utilities.CreateDayExerciseEntity(day.Id, exercise.Id);
                    if (!_workoutRepository.Save() || dayExercise == null)
                    {
                        return(RedirectToAction("CreateDay", "bdbusr", new { id = dayDto.UserId }));
                    }

                    var workload = _utilities.CreateWorkloadEntity(day.Id, exercise.Id);
                }
            }

            _workoutRepository.AddDayForUser(dayDto.UserId, day);
            if (!_workoutRepository.Save())
            {
                return(RedirectToAction("CreateDay", "bdbusr", new { id = dayDto.UserId }));
            }

            return(RedirectToAction("ViewDay", "bdbusr", new { id = day.Id }));
        }