mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-02-08 04:40:45 +08:00
Tweaked ListingResponseBuilder to help avoid future issues
- Updated so none of the method mutate the query throughout the function so that the query can be handled in a sane way, Since we were already encountering issues due to internal method call order.
This commit is contained in:
parent
a17b82bdde
commit
2ec4ad1181
|
@ -36,10 +36,10 @@ class ListingResponseBuilder
|
||||||
*/
|
*/
|
||||||
public function toResponse()
|
public function toResponse()
|
||||||
{
|
{
|
||||||
$this->applyFiltering($this->query);
|
$filteredQuery = $this->filterQuery($this->query);
|
||||||
|
|
||||||
$total = $this->query->count();
|
$total = $filteredQuery->count();
|
||||||
$data = $this->fetchData();
|
$data = $this->fetchData($filteredQuery);
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'data' => $data,
|
'data' => $data,
|
||||||
|
@ -50,22 +50,22 @@ class ListingResponseBuilder
|
||||||
/**
|
/**
|
||||||
* Fetch the data to return in the response.
|
* Fetch the data to return in the response.
|
||||||
*/
|
*/
|
||||||
protected function fetchData(): Collection
|
protected function fetchData(Builder $query): Collection
|
||||||
{
|
{
|
||||||
$this->applyCountAndOffset($this->query);
|
$query = $this->countAndOffsetQuery($query);
|
||||||
$this->applySorting($this->query);
|
$query = $this->sortQuery($query);
|
||||||
|
return $query->get($this->fields);
|
||||||
return $this->query->get($this->fields);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Apply any filtering operations found in the request.
|
* Apply any filtering operations found in the request.
|
||||||
*/
|
*/
|
||||||
protected function applyFiltering(Builder $query)
|
protected function filterQuery(Builder $query): Builder
|
||||||
{
|
{
|
||||||
|
$query = clone $query;
|
||||||
$requestFilters = $this->request->get('filter', []);
|
$requestFilters = $this->request->get('filter', []);
|
||||||
if (!is_array($requestFilters)) {
|
if (!is_array($requestFilters)) {
|
||||||
return;
|
return $query;
|
||||||
}
|
}
|
||||||
|
|
||||||
$queryFilters = collect($requestFilters)->map(function ($value, $key) {
|
$queryFilters = collect($requestFilters)->map(function ($value, $key) {
|
||||||
|
@ -74,7 +74,7 @@ class ListingResponseBuilder
|
||||||
return !is_null($value);
|
return !is_null($value);
|
||||||
})->values()->toArray();
|
})->values()->toArray();
|
||||||
|
|
||||||
$query->where($queryFilters);
|
return $query->where($queryFilters);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -102,8 +102,9 @@ class ListingResponseBuilder
|
||||||
* Apply sorting operations to the query from given parameters
|
* Apply sorting operations to the query from given parameters
|
||||||
* otherwise falling back to the first given field, ascending.
|
* otherwise falling back to the first given field, ascending.
|
||||||
*/
|
*/
|
||||||
protected function applySorting(Builder $query)
|
protected function sortQuery(Builder $query): Builder
|
||||||
{
|
{
|
||||||
|
$query = clone $query;
|
||||||
$defaultSortName = $this->fields[0];
|
$defaultSortName = $this->fields[0];
|
||||||
$direction = 'asc';
|
$direction = 'asc';
|
||||||
|
|
||||||
|
@ -117,20 +118,21 @@ class ListingResponseBuilder
|
||||||
$sortName = $defaultSortName;
|
$sortName = $defaultSortName;
|
||||||
}
|
}
|
||||||
|
|
||||||
$query->orderBy($sortName, $direction);
|
return $query->orderBy($sortName, $direction);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Apply count and offset for paging, based on params from the request while falling
|
* Apply count and offset for paging, based on params from the request while falling
|
||||||
* back to system defined default, taking the max limit into account.
|
* back to system defined default, taking the max limit into account.
|
||||||
*/
|
*/
|
||||||
protected function applyCountAndOffset(Builder $query)
|
protected function countAndOffsetQuery(Builder $query): Builder
|
||||||
{
|
{
|
||||||
|
$query = clone $query;
|
||||||
$offset = max(0, $this->request->get('offset', 0));
|
$offset = max(0, $this->request->get('offset', 0));
|
||||||
$maxCount = config('api.max_item_count');
|
$maxCount = config('api.max_item_count');
|
||||||
$count = $this->request->get('count', config('api.default_item_count'));
|
$count = $this->request->get('count', config('api.default_item_count'));
|
||||||
$count = max(min($maxCount, $count), 1);
|
$count = max(min($maxCount, $count), 1);
|
||||||
|
|
||||||
$query->skip($offset)->take($count);
|
return $query->skip($offset)->take($count);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user