/** * 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; }
/** * {@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; }
/** * Initializes proxy rest request. * * @param RestRequest $restRequest * * @return RestRequestProxy */ public static function initialize(RestRequest $restRequest) { return new self($restRequest->getRequest(), $restRequest->getSerializer()); }
/** * {@inheritdoc} */ public function deleteAction(RestRequest $restRequest, $id = null) { if ($id === null) { return $this->renderRest(['message' => $this->trans('response.error.id')], Response::HTTP_BAD_REQUEST); } $connection = $restRequest->getRepository()->getManager()->getConnection(); $types = $restRequest->getRepository()->getTypes(); try { if ($this->isBatch()) { $types = $restRequest->getRepository()->getTypes(); $connection->bulk('delete', reset($types), ['_id' => $id]); $connection->commit(false); } else { $connection->delete(['id' => $id, 'type' => $types, 'index' => $connection->getIndexName()]); } } catch (Missing404Exception $e) { return $this->renderRest(['message' => $this->trans('response.error.not_found', ['%id%' => $id])], Response::HTTP_NOT_FOUND); } return $this->renderRest(null, Response::HTTP_NO_CONTENT); }