Example #1
0
 /**
  * Handles batch process.
  *
  * @param RestRequest $restRequest
  * @return array|bool Returns false on deserialization error.
  */
 public function handle(RestRequest $restRequest)
 {
     $data = $restRequest->getData();
     if ($data === null) {
         return false;
     }
     $out = [];
     $indexes = [];
     $proxy = RestRequestProxy::initialize($restRequest);
     $currentMethod = $this->getRouter()->getContext()->getMethod();
     foreach ($data as $action) {
         $action = $this->getResolver()->resolve($action);
         $this->getRouter()->getContext()->setMethod($action['method']);
         try {
             $options = $this->getRouter()->match('/api/' . $restRequest->getVersion() . '/' . $action['path']);
         } catch (ResourceNotFoundException $e) {
             $out[] = ['status_code' => Response::HTTP_BAD_REQUEST, 'message' => 'Could not resolve path!', 'error' => $e->getMessage()];
             continue;
         }
         list($id, $method) = explode(':', $options['_controller'], 2);
         $this->prepareProxy($proxy, $action['body'], $options);
         $out[] = call_user_func_array([$this->getController($id), $method], [$proxy, $options['id']]);
         $indexes[$proxy->getRepository()->getManager()->getConnection()->getIndexName()] = null;
     }
     if (!empty($indexes)) {
         $proxy->getRepository()->getManager()->getConnection()->getClient()->indices()->flush(['index' => implode(',', array_keys($indexes))]);
     }
     $this->getRouter()->getContext()->setMethod($currentMethod);
     return $out;
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function validate(RestRequest $restRequest)
 {
     $this->setRepository($restRequest->getRepository());
     $this->setAllowExtraFields($restRequest->isAllowedExtraFields());
     $data = $restRequest->getData();
     $this->getForm(true)->submit($data);
     return $this->getForm()->isValid() ? $data : false;
 }
 /**
  * Handles batch process.
  *
  * @param RestRequest $restRequest
  * @return array|bool Returns false on deserialization error.
  */
 public function handle(RestRequest $restRequest)
 {
     $data = $restRequest->getData();
     if ($data === null) {
         return false;
     }
     $out = [];
     foreach ($data as $batch) {
         #TODO insert to the repository bulk and commit/refresh
     }
     return $out;
 }
Example #4
0
 /**
  * {@inheritdoc}
  */
 public function putAction(RestRequest $restRequest, $id = null)
 {
     if ($id === null) {
         return $this->renderRest(['message' => $this->trans('response.error.id')], Response::HTTP_BAD_REQUEST);
     }
     $data = $restRequest->getData();
     $validator = $this->get('ongr_api.rest.validator');
     $data = $validator->validate($restRequest);
     if ($data === false) {
         return $this->renderRest(['message' => $this->trans('response.error.validation'), 'errors' => $validator->getErrors()], Response::HTTP_NOT_ACCEPTABLE);
     }
     $repository = $restRequest->getRepository();
     $types = $repository->getTypes();
     $data['_id'] = $id;
     $repository->getManager()->getConnection()->bulk('index', reset($types), $data);
     $repository->getManager()->getConnection()->commit(!$this->isBatch());
     return $this->renderRest(null, Response::HTTP_NO_CONTENT, ['Location' => $this->generateRestUrl($restRequest->getRequest(), $id)]);
 }