Example #1
0
        public TrainingCycle GenerateNewCycleForFiveThreeOneRoutine(App.DTO.WorkoutRoutine baseRoutine,
                                                                    NewFiveThreeOneCycleInfo cycleInfo)
        {
            // TODO: Fix this entire ugly workaround of a method

            var oldRoutineMaxCycleNumber = baseRoutine.TrainingCycles.Max(cycle => cycle.CycleNumber);
            var oldCycle =
                baseRoutine.TrainingCycles.FirstOrDefault(cycle => cycle.CycleNumber == oldRoutineMaxCycleNumber);
            var oldCycleEndingDate = oldCycle.EndingDate;

            if (oldCycleEndingDate == null)
            {
                throw new ApplicationException("Cannot generate a new training cycle: old cycle's ending date is null");
            }

            var newCycleStartingDate = ((DateTime)oldCycleEndingDate).AddDays(1);

            var routineInfo = new NewFiveThreeOneRoutineInfo
            {
                BaseRoutine  = baseRoutine,
                CycleInfo    = cycleInfo,
                StartingDate = newCycleStartingDate
            };

            var generator      = new FiveThreeOneRoutineGenerator(routineInfo);
            var generatedCycle = generator.GenerateNewTrainingCycle(baseRoutine);

            return(generatedCycle);
        }
        public async Task <ActionResult <IEnumerable <TrainingCycle> > > AddNewCycleActiveRoutine(
            NewFiveThreeOneCycleInfo cycleInfo)
        {
            if (await _bll.WorkoutRoutines.UserWithIdHasActiveRoutineAsync(User.UserId()))
            {
                var baseRoutineId = (await _bll.WorkoutRoutines.ActiveRoutineForUserWithIdAsync(User.UserId())).Id;
                var baseRoutine   = await _bll.WorkoutRoutines.FindFullRoutineWithIdAsync(baseRoutineId);

                var newCycle = _bll.WorkoutRoutines.GenerateNewCycleForFiveThreeOneRoutine(baseRoutine, cycleInfo);

                _bll.TrainingCycles.Add(newCycle);
                await _bll.SaveChangesAsync();

                var cycle = await _bll.TrainingCycles.GetFullActiveCycleForUserWithIdAsync(User.UserId());

                return(Ok(TrainingCycleMapper.MapBLLEntityToPublicDTO(cycle)));
            }

            return(NotFound(new { message = "User does not have an active workout routine" }));
        }