public IActionResult Create([FromBody] WorkoutRequest request)
        {
            CleanSets(request);

            var exercises = CreateExercises(request.Sets).ToList();
            var workout   = Mapper.Map <WorkoutDetails>(request);

            workout.UserId = CurrentUserId;
            var userWeight = measurementRepository.GetUserWeight(CurrentUserId);
            var maxs       = Calculate1RMs(workout, exercises, userWeight);

            Update1RMs(maxs);
            CalculateLoads(workout.Sets, exercises, userWeight);
            trainingRepository.CreateWorkout(workout);
            var burnedCalories    = request.EnergyExpenditure ?? CalculateEnergyExpenditure(workout.Duration, userWeight);
            var energyExpenditure = new EnergyExpenditure
            {
                UserId     = CurrentUserId,
                Time       = workout.Time,
                WorkoutId  = workout.Id,
                EnergyKcal = burnedCalories
            };

            activityRepository.CreateEnergyExpenditure(energyExpenditure);
            var response = Mapper.Map <WorkoutDetailsResponse>(workout);

            return(Ok(response));
        }
Example #2
0
        public IActionResult Update1RM(Guid id, [FromBody] decimal max)
        {
            var exercise = trainingRepository.GetExercise(id, CurrentUserId, DateTimeOffset.MinValue);

            if (exercise.UserId != CurrentUserId)
            {
                return(Unauthorized());
            }

            var oneRepMap = new OneRepMax
            {
                ExerciseId = id,
                UserId     = CurrentUserId,
                Time       = DateTimeOffset.Now,
                Max        = max
            };

            if (exercise.PercentageBW.HasValue)
            {
                var userWeight = measurementRepository.GetUserWeight(CurrentUserId);
                if (userWeight.HasValue)
                {
                    oneRepMap.MaxInclBW = oneRepMap.Max + (exercise.PercentageBW / 100m) * userWeight;
                }
            }
            else
            {
                oneRepMap.MaxInclBW = oneRepMap.MaxBW;
            }

            trainingRepository.SaveOneRepMaxs(new[] { oneRepMap });

            return(Ok());
        }
Example #3
0
        private void CalculateEnergy(EnergyExpenditure expenditure)
        {
            var userWeight = measurementRepository.GetUserWeight(CurrentUserId);

            if (!userWeight.HasValue)
            {
                return;
            }
            if (expenditure.ActivityId.HasValue && expenditure.Duration.HasValue)
            {
                var activity = activityRepository.GetActivity(expenditure.ActivityId.Value);
                expenditure.EnergyKcal = activity.EnergyExpenditure * userWeight.Value * (decimal)expenditure.Duration.Value.TotalMinutes;
            }
        }