public static Workout MapToWorkout(this CreateWorkoutModel workoutModel)
        {
            var workout = new Workout()
            {
                TemplateId                    = new Guid(workoutModel.Template.Id),
                NumberOfRepetitions           = workoutModel.NumberOfRepetitions,
                IsSuccessfull                 = workoutModel.Successfull,
                AverageRepetitionDuration     = workoutModel.AverageRepetitionDuration,
                AverageRepetitionAcceleration = workoutModel.AverageVelocity,
                AverageTilt                   = workoutModel.AverageTilt,
                Date     = workoutModel.Date,
                Duration = workoutModel.Duration
            };

            foreach (var value in workoutModel.AccelerationValues)
            {
                workout.AccelerationValues += value.Value + "," + value.Timestamp + ";";
            }

            foreach (var value in workoutModel.VelocityValues)
            {
                workout.VelocityValues += value.Value + "," + value.Timestamp + ";";
            }

            foreach (var value in workoutModel.TiltValues)
            {
                workout.TiltValues += value.Value + "," + value.Timestamp + ";";
            }

            return(workout);
        }
Beispiel #2
0
        public async Task <IActionResult> CreateWorkout([FromBody] CreateWorkoutModel workoutModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var workout = workoutModel.MapToWorkout();
                    workout.User = await _context.Users.FirstOrDefaultAsync(x => x.UserName.Equals(User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value));

                    workout.UserId = workout.User.Id;
                    _context.Workouts.Add(workout);
                    await _context.SaveChangesAsync();

                    workoutModel.Id = workout.Id.ToString();
                    return(new OkObjectResult(workoutModel));
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return(new BadRequestObjectResult(ModelState.Values.Select(x => x.Errors)));
        }