2019-12-28 09:58:07 -05:00
|
|
|
<?php namespace BookStack\Api;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2020-01-01 11:33:47 -05:00
|
|
|
use Illuminate\Http\Request;
|
2019-12-28 09:58:07 -05:00
|
|
|
|
|
|
|
class ListingResponseBuilder
|
|
|
|
{
|
|
|
|
|
|
|
|
protected $query;
|
2020-01-01 11:33:47 -05:00
|
|
|
protected $request;
|
2019-12-28 09:58:07 -05:00
|
|
|
protected $fields;
|
|
|
|
|
2020-01-01 11:33:47 -05:00
|
|
|
protected $filterOperators = [
|
|
|
|
'eq' => '=',
|
|
|
|
'ne' => '!=',
|
|
|
|
'gt' => '>',
|
|
|
|
'lt' => '<',
|
|
|
|
'gte' => '>=',
|
|
|
|
'lte' => '<=',
|
|
|
|
'like' => 'like'
|
|
|
|
];
|
|
|
|
|
2019-12-28 09:58:07 -05:00
|
|
|
/**
|
|
|
|
* ListingResponseBuilder constructor.
|
|
|
|
*/
|
2020-01-01 11:33:47 -05:00
|
|
|
public function __construct(Builder $query, Request $request, array $fields)
|
2019-12-28 09:58:07 -05:00
|
|
|
{
|
|
|
|
$this->query = $query;
|
2020-01-01 11:33:47 -05:00
|
|
|
$this->request = $request;
|
2019-12-28 09:58:07 -05:00
|
|
|
$this->fields = $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the response from this builder.
|
|
|
|
*/
|
|
|
|
public function toResponse()
|
|
|
|
{
|
2020-04-25 16:37:52 -04:00
|
|
|
$this->applyFiltering($this->query);
|
|
|
|
|
2020-01-01 11:33:47 -05:00
|
|
|
$total = $this->query->count();
|
2020-04-25 11:38:11 -04:00
|
|
|
$data = $this->fetchData();
|
2019-12-28 09:58:07 -05:00
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
'data' => $data,
|
|
|
|
'total' => $total,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch the data to return in the response.
|
|
|
|
*/
|
|
|
|
protected function fetchData(): Collection
|
|
|
|
{
|
|
|
|
$this->applyCountAndOffset($this->query);
|
|
|
|
$this->applySorting($this->query);
|
|
|
|
|
|
|
|
return $this->query->get($this->fields);
|
|
|
|
}
|
|
|
|
|
2020-01-01 11:33:47 -05:00
|
|
|
/**
|
|
|
|
* Apply any filtering operations found in the request.
|
|
|
|
*/
|
|
|
|
protected function applyFiltering(Builder $query)
|
|
|
|
{
|
|
|
|
$requestFilters = $this->request->get('filter', []);
|
|
|
|
if (!is_array($requestFilters)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$queryFilters = collect($requestFilters)->map(function ($value, $key) {
|
|
|
|
return $this->requestFilterToQueryFilter($key, $value);
|
|
|
|
})->filter(function ($value) {
|
|
|
|
return !is_null($value);
|
|
|
|
})->values()->toArray();
|
|
|
|
|
|
|
|
$query->where($queryFilters);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a request filter query key/value pair into a [field, op, value] where condition.
|
|
|
|
*/
|
|
|
|
protected function requestFilterToQueryFilter($fieldKey, $value): ?array
|
|
|
|
{
|
|
|
|
$splitKey = explode(':', $fieldKey);
|
|
|
|
$field = $splitKey[0];
|
|
|
|
$filterOperator = $splitKey[1] ?? 'eq';
|
|
|
|
|
|
|
|
if (!in_array($field, $this->fields)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!in_array($filterOperator, array_keys($this->filterOperators))) {
|
|
|
|
$filterOperator = 'eq';
|
|
|
|
}
|
|
|
|
|
|
|
|
$queryOperator = $this->filterOperators[$filterOperator];
|
|
|
|
return [$field, $queryOperator, $value];
|
|
|
|
}
|
|
|
|
|
2019-12-28 09:58:07 -05:00
|
|
|
/**
|
|
|
|
* Apply sorting operations to the query from given parameters
|
|
|
|
* otherwise falling back to the first given field, ascending.
|
|
|
|
*/
|
|
|
|
protected function applySorting(Builder $query)
|
|
|
|
{
|
|
|
|
$defaultSortName = $this->fields[0];
|
|
|
|
$direction = 'asc';
|
|
|
|
|
2020-01-01 11:33:47 -05:00
|
|
|
$sort = $this->request->get('sort', '');
|
2019-12-28 09:58:07 -05:00
|
|
|
if (strpos($sort, '-') === 0) {
|
|
|
|
$direction = 'desc';
|
|
|
|
}
|
|
|
|
|
|
|
|
$sortName = ltrim($sort, '+- ');
|
|
|
|
if (!in_array($sortName, $this->fields)) {
|
|
|
|
$sortName = $defaultSortName;
|
|
|
|
}
|
|
|
|
|
|
|
|
$query->orderBy($sortName, $direction);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
protected function applyCountAndOffset(Builder $query)
|
|
|
|
{
|
2020-01-01 11:33:47 -05:00
|
|
|
$offset = max(0, $this->request->get('offset', 0));
|
2019-12-28 09:58:07 -05:00
|
|
|
$maxCount = config('api.max_item_count');
|
2020-01-01 11:33:47 -05:00
|
|
|
$count = $this->request->get('count', config('api.default_item_count'));
|
2019-12-28 09:58:07 -05:00
|
|
|
$count = max(min($maxCount, $count), 1);
|
|
|
|
|
|
|
|
$query->skip($offset)->take($count);
|
|
|
|
}
|
|
|
|
}
|