Exemplo n.º 1
0
        public async Task <ActionResult <Exercise> > SaveExercise([FromBody] ExerciseViewModel exerciseViewModel)
        {
            Exercise entity;

            if (exerciseViewModel.Id == 0)
            {
                entity = await _exerciseService.CreateNewAsync();

                entity.User = await _applicationUserService.GetCurrentUserAsync(User);
            }
            else
            {
                entity = await _exerciseService.GetExerciseByIdAsync(exerciseViewModel.Id);

                if (entity == null)
                {
                    return(BadRequest());
                }
            }

            exerciseViewModel.Update(entity);
            await _exerciseService.SaveChangesAsync();

            return(Ok(new ExerciseViewModel(entity)));
        }