mirror of
https://github.com/grocy/grocy.git
synced 2025-04-29 09:39:57 +00:00
Support descending ordering in generic API filter (closes #1167)
This commit is contained in:
parent
19802bc122
commit
fda8411ab3
@ -188,7 +188,7 @@
|
||||
- `/chores`
|
||||
- `/batteries`
|
||||
- There are 4 new (optional) query parameters to utilize that
|
||||
- `order` The field to order by
|
||||
- `order` The field to order by (use the separator `:` to specify the sort order - `asc` or `desc`, defaults to `asc` when omitted)
|
||||
- `limit` The maximum number of objects to return
|
||||
- `offset` The number of objects to skip
|
||||
- `query[]` An array of conditions, each of them is a string in the form of `<field><condition><value>`, where
|
||||
|
@ -47,14 +47,31 @@ class BaseApiController extends BaseController
|
||||
{
|
||||
$data = $this->filter($data, $query['query']);
|
||||
}
|
||||
|
||||
if (isset($query['limit']))
|
||||
{
|
||||
$data = $data->limit(intval($query['limit']), intval($query['offset'] ?? 0));
|
||||
}
|
||||
|
||||
if (isset($query['order']))
|
||||
{
|
||||
$data = $data->orderBy($query['order']);
|
||||
$parts = explode(':', $query['order']);
|
||||
|
||||
if (count($parts) == 1)
|
||||
{
|
||||
$data = $data->orderBy($parts[0]);
|
||||
}
|
||||
else
|
||||
{
|
||||
if ($parts[1] != 'asc' && $parts[1] != 'desc')
|
||||
{
|
||||
throw new \Exception('Invalid sort order ' . $parts[1]);
|
||||
}
|
||||
|
||||
$data = $data->orderBy($parts[0], $parts[1]);
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
@ -5053,7 +5053,7 @@
|
||||
"in": "query",
|
||||
"name": "order",
|
||||
"required": false,
|
||||
"description": "A valid field name by which the response should be ordered",
|
||||
"description": "A valid field name by which the response should be ordered, use the separator `:` to specify the sort order (`asc` or `desc`, defaults to `asc` when omitted)",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user