]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
baculum: Add offset parameter to filesets endpoint
authorMarcin Haba <marcin.haba@bacula.pl>
Tue, 4 Apr 2023 07:49:50 +0000 (09:49 +0200)
committerMarcin Haba <marcin.haba@bacula.pl>
Thu, 20 Apr 2023 10:00:26 +0000 (12:00 +0200)
Changes:
 - New offset parameter
 - Rework the filesets endpoint
 - Fix limit parameter

gui/baculum/protected/API/Modules/FileSetManager.php
gui/baculum/protected/API/Pages/API/FileSets.php

index 9b61dbc6a015b56b70d6ea1ead5fca813f09b512..9c3c24b7c9d2efe88db9bac44233b46d8174a552 100644 (file)
@@ -22,7 +22,7 @@
 
 namespace Baculum\API\Modules;
 
-use Prado\Data\ActiveRecord\TActiveRecordCriteria;
+use PDO;
 
 /**
  * FileSet manager module.
@@ -33,12 +33,23 @@ use Prado\Data\ActiveRecord\TActiveRecordCriteria;
  */
 class FileSetManager extends APIModule {
 
-       public function getFileSets($limit) {
-               $criteria = new TActiveRecordCriteria;
-               if(is_int($limit) && $limit > 0) {
-                       $criteria->Limit = $limit;
+       public function getFileSets($criteria, $limit_val = 0, $offset_val = 0) {
+               $limit = '';
+               if (is_int($limit_val) && $limit_val > 0) {
+                       $limit = ' LIMIT ' . $limit_val;
                }
-               return FileSetRecord::finder()->findAll($criteria);
+               $offset = '';
+               if (is_int($offset_val) && $offset_val > 0) {
+                       $offset = ' OFFSET ' . $offset_val;
+               }
+               $where = Database::getWhere($criteria);
+
+               $sql = 'SELECT FileSet.* 
+FROM FileSet '
+. $where['where'] . $limit . $offset;
+
+               $statement = Database::runQuery($sql, $where['params']);
+               return $statement->fetchAll(PDO::FETCH_OBJ);
        }
 
        public function getFileSetById($id) {
index 4e2ab538e30633828409e654d784794ece5bb2d2..55cc9c0791934cea42f6aba40a28de50e4c7c813 100644 (file)
@@ -33,30 +33,35 @@ use Baculum\Common\Modules\Errors\FileSetError;
 class FileSets extends BaculumAPIServer {
 
        public function get() {
-               $limit = $this->Request->contains('limit') ? intval($this->Request['limit']) : 0;
-               $filesets = $this->getModule('fileset')->getFileSets($limit);
+               $misc = $this->getModule('misc');
+               $limit = $this->Request->contains('limit') && $misc->isValidInteger($this->Request['limit']) ? (int)$this->Request['limit'] : 0;
+               $offset = $this->Request->contains('offset') && $misc->isValidInteger($this->Request['offset']) ? (int)$this->Request['offset'] : 0;
                $result = $this->getModule('bconsole')->bconsoleCommand(
                        $this->director,
-                       array('.fileset')
+                       ['.fileset']
                );
                if ($result->exitcode === 0) {
                        array_shift($result->output);
-                       if (is_array($filesets)) {
-                               $fs = array();
-                               for ($i = 0; $i < count($filesets); $i++) {
-                                       if(in_array($filesets[$i]->fileset, $result->output)) {
-                                               $fs[] = $filesets[$i];
-                                       }
-                               }
-                               $this->output = $fs;
+                       $vals = array_filter($result->output);
+                       if (count($vals) == 0) {
+                               // no $vals criteria means that user has no fileset resource assigned.
+                               $this->output = [];
                                $this->error = FileSetError::ERROR_NO_ERRORS;
-                       } else {
-                               $this->output = FileSetError::MSG_ERROR_FILESET_DOES_NOT_EXISTS;
-                               $this->error = FileSetError::ERROR_FILESET_DOES_NOT_EXISTS;
+                               return;
                        }
+
+                       $params['FileSet.FileSet'] = [];
+                       $params['FileSet.FileSet'][] = [
+                               'operator' => 'IN',
+                               'vals' => $vals
+                       ];
+
+                       $filesets = $this->getModule('fileset')->getFileSets($params, $limit, $offset);
+                       $this->output = $filesets;
+                       $this->error = FileSetError::ERROR_NO_ERRORS;
                } else {
-                       $this->output = $result->output;
-                       $this->error = $result->exitcode;
+                       $this->output = FileSetError::MSG_ERROR_WRONG_EXITCODE . 'ErrorCode=' . $result->exitcode . ' Output=' . implode(PHP_EOL, $result->output);
+                       $this->error = FileSetError::ERROR_WRONG_EXITCODE;
                }
        }
 }