예제 #1
0
        public async Task <IActionResult> ComponentCreate(
            [HttpTrigger(AuthorizationLevel.User, "post", Route = "locations/{locationId}/components")] HttpRequest req,
            int locationId,
            [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 given location exists
            if (!await locationService.Exists(locationId))
            {
                return(new NotFoundObjectResult(new ErrorResponse(ErrorCode.LOCATION_NOT_FOUND)));
            }

            // get the form data
            IFormCollection formdata = await req.ReadFormAsync();

            ComponentBody componentBody;

            try {
                componentBody = SerializationUtil.DeserializeFormData <ComponentBody>(formdata);
            }
            catch (ValidationException e) {
                return(new BadRequestObjectResult(new ErrorResponse(400, e.Message)));
            }

            // check if all fields are filled in
            if (componentBody.Name == null || componentBody.Description == null || componentBody.Image == null || componentBody.Exercises == null)
            {
                return(new BadRequestObjectResult(new ErrorResponse(ErrorCode.INVALID_REQUEST_BODY)));
            }

            // check if given exercises exist
            foreach (int exerciseId in componentBody.Exercises)
            {
                if (!await exerciseService.Exists(exerciseId))
                {
                    return(new BadRequestObjectResult(new ErrorResponse(ErrorCode.INVALID_EXERCISE_PROVIDED)));
                }
            }

            // create new component
            int componentId = await componentService.CreateComponent(componentBody, locationId);

            // get the component
            ComponentResponse createdComponent = await componentService.GetComponent(locationId, componentId);

            return(new OkObjectResult(createdComponent));
        }
예제 #2
0
        public async Task <IActionResult> WorkoutCreate(
            [HttpTrigger(AuthorizationLevel.User, "post", Route = "workouts")]
            [RequestBodyType(typeof(WorkoutBody), "The workout to create")] HttpRequest req,
            [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)));
            }

            // deserialize request
            WorkoutBody workoutBody;

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

            // check if all fields are filled in
            if (workoutBody.Name == null || workoutBody.Type == null || workoutBody.Exercises == null || workoutBody.Exercises.Count == 0)
            {
                return(new BadRequestObjectResult(new ErrorResponse(ErrorCode.INVALID_REQUEST_BODY)));
            }

            // check if given exercises exist
            foreach (int exerciseId in workoutBody.Exercises)
            {
                if (!await exerciseService.Exists(exerciseId))
                {
                    return(new BadRequestObjectResult(new ErrorResponse(ErrorCode.INVALID_EXERCISE_PROVIDED)));
                }
            }

            // create new workout
            int workoutId = await workoutService.CreateWorkout(workoutBody);

            // get the created location
            WorkoutResponse createdWorkout = await workoutService.GetWorkout(workoutId);

            return(new OkObjectResult(createdWorkout));
        }