public function apply(Request $request, ParamConverter $configuration) { $class = $configuration->getClass(); try { $object = $this->serializer->deserialize($request->getContent(), $class, $request->get('_format')); } catch (RuntimeException $e) { throw new NotFoundHttpException(sprintf('Could not deserialise request content to object of type "%s"', $class)); } $errors = $this->validator->validate($object); ValidatorHelper::handleValidationException($this->serializer, $request->get('_format'), $errors); /*if (count($errors) > 0) { $errorDTO = array(); foreach ($errors as $error) { $errorDTO[$error->getPropertyPath()] = $error->getMessage(); } $json = $this->serializer->serialize(["ValidationFailed", $errorDTO], $request->get('_format')); throw new ValidationException($errorDTO); }*/ // set the object as the request attribute with the given name // (this will later be an argument for the action) $request->attributes->set($configuration->getName(), $object); }
/** * @return \FOS\RestBundle\View\View * * @param PostUserRequest $userDTO * * * @Post("/users") * * @Security("has_role('ROLE_MANAGER')") * * @ApiDoc( * description="Creates a new user", * input={ * "class"="cs221\APIBundle\DataTransferObject\PostUserRequest", * }, * output={ * "class"="cs221\APIBundle\DataTransferObject\GetUserResponse", * } * ) */ public function postUsersAction(PostUserRequest $userDTO) { $userRepository = $this->container->get('cs221_api.user.repository'); $serializer = $this->get('serializer'); if ($userRepository->get($userDTO->email) != null) { $serializer = $this->get('serializer'); $violation = ValidatorHelper::generateValidationError("email", sprintf('The user with the email \'%s\' already exists', $userDTO->email)); ValidatorHelper::handleValidationException($serializer, "json", new ConstraintViolationList(array($violation))); return; //throw new HttpException(500, sprintf('The user with the email \'%s\' already exists', $userDTO->email)); } $entity = new UserEntity($userDTO->email, $userDTO->fullName, $userDTO->isActive, $userDTO->isManager); $encoderService = $this->get('security.encoder_factory'); $encoder = $encoderService->getEncoder($entity); $hash = $encoder->encodePassword($userDTO->password, $entity->getSalt()); $entity->setPassword($hash); $entity = $userRepository->create($entity); return $this->entityToGetUserResponse($entity); }
/** * @return \FOS\RestBundle\View\View * * @param PostTaskRequest $TaskDTO * * * @Put("/tasks/{id}") * * @ApiDoc( * description="Updates a Task", * requirements={ * { * "name"="id", * "dataType"="integer", * "requirement"="\d+", * "description"="Identifier of task to update" * } * }, * input={ * "class"="cs221\APIBundle\DataTransferObject\PutTaskRequest", * }, * output={ * "class"="cs221\APIBundle\DataTransferObject\GetTaskResponse", * } * ) */ public function putTaskAction($id, PutTaskRequest $TaskDTO) { $taskRepository = $this->container->get('cs221_api.task.repository'); $userRepository = $this->container->get('cs221_api.user.repository'); $serializer = $this->get('serializer'); $user = $userRepository->get($TaskDTO->memberEmail); $violations = TaskController::validateTask($TaskDTO, $user); if (count($violations) > 0) { ValidatorHelper::handleValidationException($serializer, "json", new ConstraintViolationList($violations)); return; } $entity = $taskRepository->get($id); $oldElements = $entity->getElements(); //$entity->clearElements(); //$entity = $taskRepository->update($entity, $oldElements); //return $entity; $entity->setId($id); $entity->setAssignedUser($user); $entity->setStartDate($TaskDTO->startDate); $entity->setEndDate($TaskDTO->endDate); $entity->setTitle($TaskDTO->title); $entity->setStatus($TaskDTO->isCompleted ? 2 : 0); $entity->addElements($TaskDTO->elements); $entity = $taskRepository->update($entity, $oldElements); return $this->entityToGetTaskResponse($entity); }