Skip to content

Commit

Permalink
Merge pull request #85 from Robindfuller/master
Browse files Browse the repository at this point in the history
Update ListResource.php
  • Loading branch information
nilportugues committed Aug 4, 2016
2 parents ea00a19 + 912a8ad commit 443012d
Showing 1 changed file with 1 addition and 64 deletions.
65 changes: 1 addition & 64 deletions src/NilPortugues/Laravel5/JsonApi/Actions/ListResource.php
Expand Up @@ -36,68 +36,5 @@ public function getErrorResponse(\Exception $e)

return parent::getErrorResponse($e);
}


/**
* @param callable $totalAmountCallable
* @param callable $resultsCallable
* @param string $route
* @param string $className
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function get(callable $totalAmountCallable, callable $resultsCallable, $route, $className)
{
try {
QueryObject::assert(
$this->serializer,
$this->fields,
$this->included,
$this->sorting,
$this->errorBag,
$className
);
$totalAmount = $totalAmountCallable();

if ($totalAmount > 0 && $this->page->size() > 0 && $this->page->number() > ceil($totalAmount / $this->page->size())) {
return $this->resourceNotFound(
new ErrorBag([new OufOfBoundsError($this->page->number(), $this->page->size())])
);
}

$links = $this->pagePaginationLinks(
$route,
$this->page->number(),
$this->page->size(),
$totalAmount,
$this->fields,
$this->sorting,
$this->included,
$this->filters
);


$results = $resultsCallable();

if ($results instanceof Collection) {
$results = json_encode(['data' => $results->toArray()]);
}


$paginatedResource = new PaginatedResource(
$results,
$this->page->number(),
$this->page->size(),
$totalAmount,
$links
);

$response = $this->response($paginatedResource);
} catch (Exception $e) {
$response = $this->getErrorResponse($e);
}

return $response;
}


}

0 comments on commit 443012d

Please sign in to comment.