public async Task <bool> Add(TrainingForAddDto trainingForAddDto, int userId) { var user = await _repoUser.GetUser(userId); var trainingToCreate = Training.Create(trainingForAddDto.Name.Trim(), trainingForAddDto.Date, user); foreach (var trainingExerciseDto in trainingForAddDto.Exercises) { var exercise = await _repoExercise.GetByName(trainingExerciseDto.Exercise.Name, user.Id); if (exercise == null) { _repoExercise.Add(Exercise.Create(trainingExerciseDto.Exercise.Name, user)); await _repoExercise.SaveAll(); exercise = await _repoExercise.GetByName(trainingExerciseDto.Exercise.Name, user.Id); } var exerciseToCreate = TrainingExercise.Create(exercise, user); foreach (var set in trainingExerciseDto.Sets) { var unit = await _repoUnit.GetByCode(set.Unit); var setToCreate = TrainingExerciseSet.Create(set.Reps, set.Weight, unit, user); exerciseToCreate.Sets.Add(setToCreate); } trainingToCreate.Exercises.Add(exerciseToCreate); } _repoTraining.Add(trainingToCreate); return(await _repoTraining.SaveAll()); }
public async Task <IActionResult> Add(TrainingForAddDto trainingForAddDto) { try { var userId = GetUserId(); var result = await _service.Add(trainingForAddDto, userId); return(StatusCode(201)); } catch (Exception e) { _logger.LogError(e.Message); return(BadRequest("Add training error.")); } }