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));
        }
        public async Task <Workout> AddWorkoutAsync(WorkoutRequest workoutRequest)
        {
            Workout workout = new Workout();

            workout.Id        = new Guid();
            workout.Date      = workoutRequest.Date;
            workout.Results   = workoutRequest.Results;
            workout.Exercises = workoutRequest.Exercises;
            Dbcontext.workout.Add(workout);
            await Dbcontext.SaveChangesAsync();

            var exercisetoreturn = await GetWorkoutByIdAsync(workout.Id);

            return(exercisetoreturn);
        }
        public IActionResult Update(Guid id, [FromBody] WorkoutRequest request)
        {
            CleanSets(request);

            var workout = trainingRepository.GetWorkout(id);

            if (workout.UserId != CurrentUserId)
            {
                return(Unauthorized());
            }
            var exercises = CreateExercises(request.Sets).ToList();

            Mapper.Map(request, workout);
            var userWeight = measurementRepository.GetUserWeight(CurrentUserId);
            var maxs       = Calculate1RMs(workout, exercises, userWeight);

            Update1RMs(maxs);
            CalculateLoads(workout.Sets, exercises, userWeight);
            trainingRepository.UpdateWorkout(workout);

            var burnedCalories    = request.EnergyExpenditure ?? CalculateEnergyExpenditure(workout.Duration, userWeight);
            var energyExpenditure = activityRepository.GetEnergyExpenditureForWorkout(id);

            if (energyExpenditure == null)
            {
                energyExpenditure = new EnergyExpenditure
                {
                    UserId     = CurrentUserId,
                    Time       = workout.Time,
                    WorkoutId  = workout.Id,
                    EnergyKcal = burnedCalories
                };
                activityRepository.CreateEnergyExpenditure(energyExpenditure);
            }
            else
            {
                energyExpenditure.Time       = workout.Time;
                energyExpenditure.EnergyKcal = burnedCalories;
                activityRepository.UpdateEnergyExpenditure(energyExpenditure);
            }

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

            return(Ok(response));
        }
 private static void CleanSets(WorkoutRequest request)
 {
     request.Sets = request.Sets.Where(s => s.ExerciseId != null || !string.IsNullOrWhiteSpace(s.ExerciseName)).ToArray();
 }
        public async Task <IActionResult> AddWorkout([FromBody] WorkoutRequest workoutRequest)
        {
            var exerciseresult = await WorkoutService.AddWorkoutAsync(workoutRequest);

            return(Ok(exerciseresult));
        }