public function execute(Request $request, Response $response, callable $next = null) { $pathParams = $request->getAttribute('pathParams'); $queryParams = $request->getQueryParams(); try { \Assert\Assertion::keyExists($pathParams, 'name'); $collection = $pathParams['name']; $limit = $queryParams['limit']; $page = $queryParams['page']; $columns = $queryParams['columns']; $groups = $queryParams['groups']; $aggregates = $queryParams['aggregates']; $orders = $this->orders(isset($queryParams['orders']) ? $queryParams['orders'] : array()); $wheres = $this->wheres(isset($queryParams['filters']) ? $queryParams['filters'] : array()); $table = $this->boot()->db->table($collection); $paginator = $table->getPaginator($wheres, $orders, $limit, $page, $columns, $groups, $aggregates); $pages = (array) $paginator->getPages(); $pages['requesttime'] = time(); $pages['sql'] = $table->getSql(); $query = http_build_query(['limit' => $pages['itemCountPerPage'], 'page' => $pages['current'], 'columns' => $queryParams['columns']]); $hal = new Hal("/collection/{$collection}?{$query}", $pages); $query = http_build_query(['limit' => $pages['itemCountPerPage'], 'page' => $pages['next'], 'columns' => $queryParams['columns']]); $hal->addLink('next', "/collection/{$collection}?{$query}"); $query = http_build_query(['limit' => $pages['itemCountPerPage'], 'page' => $pages['previous'], 'columns' => $queryParams['columns']]); $hal->addLink('previous', "/collection/{$collection}?{$query}"); $query = http_build_query(['limit' => $pages['itemCountPerPage'], 'page' => $pages['first'], 'columns' => $queryParams['columns']]); $hal->addLink('first', "/collection/{$collection}?{$query}"); $query = http_build_query(['limit' => $pages['itemCountPerPage'], 'page' => $pages['last'], 'columns' => $queryParams['columns']]); $hal->addLink('last', "/collection/{$collection}?{$query}"); foreach ($paginator as $item) { $resource = new Hal("/collection/{$collection}/{$item->id}", $item->toArray()); $hal->addResource('items', $resource); } } catch (\Exception $ex) { $problem = new \Crell\ApiProblem\ApiProblem($ex->getMessage(), $request->getUri()->getPath()); $problem->setDetail($ex->getTraceAsString())->setStatus($ex->getCode()); return $response->withStatus(500)->withBody($this->toStream($problem->asJson())); } $response = $response->withHeader('Content-Type', 'application/json')->withBody($this->toStream($hal->asJson())); if ($next) { $response = $next($request, $response); } return $response; }
public function execute(Request $request, Response $response, callable $next = null) { try { $pathParams = $request->getAttribute('pathParams'); \Assert\Assertion::keyExists($pathParams, 'name'); $body = $request->getBody(); \Assert\Assertion::isJsonString($body); $collection = $pathParams['name']; $data = json_decode($body, true); $id = $this->boot()->db->table($collection)->insert($data); $item = $this->boot()->db->table($collection)->fetchRow("`id` = {$id}"); $hal = new Hal("/collection/{$collection}/{$id}", $item->toArray()); $response = $response->withBody($this->toStream($hal->asJson())); } catch (\Exception $ex) { $problem = new \Crell\ApiProblem\ApiProblem($ex->getMessage(), $request->getUri()->getPath()); $problem->setDetail($ex->getTraceAsString())->setStatus($ex->getCode()); return $response->withStatus(500)->withBody($this->toStream($problem->asJson())); } if ($next) { $response = $next($request, $response); } return $response; }