Exemple #1
0
        public async Task <IActionResult> PutExercise(int id, [FromBody] ExerciseResource exerciseResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var exercise = await repository.Get(id);

            if (exercise == null)
            {
                return(NotFound());
            }

            if (exercise.UserId != GetUserId())
            {
                return(Forbid());
            }

            mapper.Map <ExerciseResource, Exercise> (exerciseResource, exercise);

            try {
                await unitOfWork.CompleteAsync();
            } catch (DbUpdateConcurrencyException) {
                if (!repository.IfExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            exercise = await repository.Get(exercise.ExerciseId);

            var result = mapper.Map <Exercise, ExerciseResource> (exercise);

            return(Ok(result));
        }