mirror of
https://github.com/grocy/grocy.git
synced 2025-08-13 17:27:23 +00:00
Support descending ordering in generic API filter (closes #1167)
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user