コード例 #1
0
        public async Task <IActionResult> WorkoutUpdate(
            [HttpTrigger(AuthorizationLevel.User, "put", Route = "workouts/{workoutId}")]
            [RequestBodyType(typeof(WorkoutBody), "The workout to update")] HttpRequest req,
            int workoutId,
            [SwaggerIgnore] ClaimsPrincipal user)
        {
            // check if user has admin rights
            if (!user.IsInRole(UserType.Admin.ToString()))
            {
                return(ForbiddenObjectResult.Create(new ErrorResponse(ErrorCode.UNAUTHORIZED_ROLE_NO_PERMISSIONS)));
            }

            // check if requested workout exists
            if (!await workoutService.Exists(workoutId))
            {
                return(new NotFoundObjectResult(new ErrorResponse(ErrorCode.WORKOUT_NOT_FOUND)));
            }

            // deserialize request
            WorkoutBody workoutBody;

            try
            {
                workoutBody = await SerializationUtil.Deserialize <WorkoutBody>(req.Body);
            }
            catch (JsonException e)
            {
                return(new BadRequestObjectResult(new ErrorResponse(400, e.Message)));
            }

            // update workout
            await workoutService.UpdateWorkout(workoutBody, workoutId);

            // get the updated workout
            WorkoutResponse updatedWorkout = await workoutService.GetWorkout(workoutId);

            return(new OkObjectResult(updatedWorkout));
        }