From d653b80b9b6a4e477fda14cad5953eecb58928ff Mon Sep 17 00:00:00 2001 From: Marcin Haba Date: Thu, 22 Dec 2022 16:04:50 +0100 Subject: [PATCH] baculum: Add offset parameter to event and pool list endpoint --- gui/baculum/protected/API/Modules/EventManager.php | 8 ++++++-- gui/baculum/protected/API/Modules/PoolManager.php | 9 ++++++--- gui/baculum/protected/API/Pages/API/Events.php | 3 ++- gui/baculum/protected/API/Pages/API/Pools.php | 4 +++- gui/baculum/protected/API/openapi_baculum.json | 14 +++++++++++--- 5 files changed, 28 insertions(+), 10 deletions(-) diff --git a/gui/baculum/protected/API/Modules/EventManager.php b/gui/baculum/protected/API/Modules/EventManager.php index 491451a30..54757c8b7 100644 --- a/gui/baculum/protected/API/Modules/EventManager.php +++ b/gui/baculum/protected/API/Modules/EventManager.php @@ -38,7 +38,7 @@ class EventManager extends APIModule { * @param array $time_scope time range for events time * @param int|null $limit_val limit results value */ - public function getEvents($criteria = [], $time_scope = [], $limit_val = null) { + public function getEvents($criteria = [], $time_scope = [], $limit_val = 0, $offset_val = 0) { $sort_col = 'EventsId'; $db_params = $this->getModule('api_config')->getConfig('db'); if ($db_params['type'] === Database::PGSQL_TYPE) { @@ -49,6 +49,10 @@ class EventManager extends APIModule { if(is_int($limit_val) && $limit_val > 0) { $limit = ' LIMIT ' . $limit_val; } + $offset = ''; + if (is_int($offset_val) && $offset_val > 0) { + $offset = ' OFFSET ' . $offset_val; + } $where = Database::getWhere($criteria, true); @@ -64,7 +68,7 @@ class EventManager extends APIModule { $where['where'] = ' WHERE ' . $where['where']; } - $sql = 'SELECT Events.* FROM Events ' . $where['where'] . $order . $limit; + $sql = 'SELECT Events.* FROM Events ' . $where['where'] . $order . $limit . $offset; return EventRecord::finder()->findAllBySql($sql, $where['params']); } diff --git a/gui/baculum/protected/API/Modules/PoolManager.php b/gui/baculum/protected/API/Modules/PoolManager.php index 2a97fc3fd..0af41bf24 100644 --- a/gui/baculum/protected/API/Modules/PoolManager.php +++ b/gui/baculum/protected/API/Modules/PoolManager.php @@ -32,7 +32,7 @@ use Prado\Data\ActiveRecord\TActiveRecordCriteria; * @package Baculum API */ class PoolManager extends APIModule { - public function getPools($limit) { + public function getPools($limit_val = 0, $offset_val = 0) { $criteria = new TActiveRecordCriteria; $order = 'Name'; $db_params = $this->getModule('api_config')->getConfig('db'); @@ -40,8 +40,11 @@ class PoolManager extends APIModule { $order = strtolower($order); } $criteria->OrdersBy[$order] = 'asc'; - if(is_int($limit) && $limit > 0) { - $criteria->Limit = $limit; + if(is_int($limit_val) && $limit_val > 0) { + $criteria->Limit = $limit_val; + } + if (is_int($offset_val) && $offset_val > 0) { + $criteria->Offset = $offset_val; } return PoolRecord::finder()->findAll($criteria); } diff --git a/gui/baculum/protected/API/Pages/API/Events.php b/gui/baculum/protected/API/Pages/API/Events.php index 3561ae716..4b33828a9 100644 --- a/gui/baculum/protected/API/Pages/API/Events.php +++ b/gui/baculum/protected/API/Pages/API/Events.php @@ -34,6 +34,7 @@ class Events extends BaculumAPIServer { public function get() { $misc = $this->getModule('misc'); $limit = $this->Request->contains('limit') ? intval($this->Request['limit']) : 0; + $offset = $this->Request->contains('offset') && $misc->isValidInteger($this->Request['offset']) ? (int)$this->Request['offset'] : 0; $eventscode = $this->Request->contains('eventscode') && $misc->isValidName($this->Request['eventscode']) ? $this->Request['eventscode'] : null; $eventstype = $this->Request->contains('eventstype') && $misc->isValidName($this->Request['eventstype']) ? $this->Request['eventstype'] : null; $eventstimestart = $this->Request->contains('eventstimestart') && $misc->isValidBDate($this->Request['eventstimestart']) ? $this->Request['eventstimestart'] : null; @@ -81,7 +82,7 @@ class Events extends BaculumAPIServer { $time_scope['eventstimeend'] = $eventstimeend; } - $events = $this->getModule('event')->getEvents($params, $time_scope, $limit); + $events = $this->getModule('event')->getEvents($params, $time_scope, $limit, $offset); $this->output = $events; $this->error = EventError::ERROR_NO_ERRORS; } diff --git a/gui/baculum/protected/API/Pages/API/Pools.php b/gui/baculum/protected/API/Pages/API/Pools.php index bc6041cfc..41aa33458 100644 --- a/gui/baculum/protected/API/Pages/API/Pools.php +++ b/gui/baculum/protected/API/Pages/API/Pools.php @@ -32,8 +32,10 @@ use Baculum\Common\Modules\Errors\PoolError; */ class Pools extends BaculumAPIServer { public function get() { + $misc = $this->getModule('misc'); $limit = $this->Request->contains('limit') ? intval($this->Request['limit']) : 0; - $pools = $this->getModule('pool')->getPools($limit); + $offset = $this->Request->contains('offset') && $misc->isValidInteger($this->Request['offset']) ? (int)$this->Request['offset'] : 0; + $pools = $this->getModule('pool')->getPools($limit, $offset); $result = $this->getModule('bconsole')->bconsoleCommand( $this->director, ['.pool'], diff --git a/gui/baculum/protected/API/openapi_baculum.json b/gui/baculum/protected/API/openapi_baculum.json index 8a3dff595..23869f955 100644 --- a/gui/baculum/protected/API/openapi_baculum.json +++ b/gui/baculum/protected/API/openapi_baculum.json @@ -2763,9 +2763,14 @@ } } }, - "parameters": [{ - "$ref": "#/components/parameters/Limit" - }] + "parameters": [ + { + "$ref": "#/components/parameters/Limit" + }, + { + "$ref": "#/components/parameters/Offset" + } + ] } }, "/api/v2/pools/{poolid}": { @@ -7242,6 +7247,9 @@ { "$ref": "#/components/parameters/Limit" }, + { + "$ref": "#/components/parameters/Offset" + }, { "name": "eventscode", "in": "query", -- 2.47.3