Skip to content

Commit

Permalink
Update V1Api.php
Browse files Browse the repository at this point in the history
Signed-off-by: Rello <Rello@users.noreply.github.com>
  • Loading branch information
Rello authored Sep 20, 2023
1 parent 095e493 commit 5144142
Showing 1 changed file with 68 additions and 51 deletions.
119 changes: 68 additions & 51 deletions lib/Api/V1Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,76 @@
namespace OCA\Tables\Api;

use OCA\Tables\Errors\InternalError;
use OCA\Tables\Errors\NotFoundError;
use OCA\Tables\Errors\PermissionError;
use OCA\Tables\Service\ColumnService;
use OCA\Tables\Service\RowService;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;

class V1Api {
private RowService $rowService;
private ColumnService $columnService;
private ?string $userId;

public function __construct(ColumnService $columnService, RowService $rowService, ?string $userId) {
$this->columnService = $columnService;
$this->rowService = $rowService;
$this->userId = $userId;
}

/**
* @param int $tableId
* @param int|null $limit
* @param int|null $offset
* @return array
* @throws InternalError
* @throws PermissionError
*/
public function getData(int $tableId, ?int $limit, ?int $offset): array {
$columns = $this->columnService->findAllByTable($tableId);

$rows = $this->rowService->findAllByTable($tableId, $this->userId, $limit, $offset);

$data = [];

// first line contains the titles
$header = [];
foreach ($columns as $column) {
$header[] = $column->getTitle();
}
$data[] = $header;

// now add the rows
foreach ($rows as $row) {
$rowData = $row->getDataArray();
$line = [];
foreach ($columns as $column) {
$value = '';
foreach ($rowData as $datum) {
if ($datum['columnId'] === $column->getId()) {
$value = $datum['value'];
}
}
$line[] = $value;
}
$data[] = $line;
}

return $data;
}
class V1Api
{
private RowService $rowService;
private ColumnService $columnService;
private ?string $userId;

public function __construct(ColumnService $columnService, RowService $rowService, ?string $userId)
{
$this->columnService = $columnService;
$this->rowService = $rowService;
$this->userId = $userId;
}

/**
* @param int $tableId
* @param int|null $limit
* @param int|null $offset
* @param int|null $userId
* @param string|null $nodeType
* @return array
* @throws DoesNotExistException
* @throws InternalError
* @throws MultipleObjectsReturnedException
* @throws NotFoundError
* @throws PermissionError
*/
public function getData(int $tableId, ?int $limit, ?int $offset, ?int $userId, ?string $nodeType = null): array
{
if ($userId) $this->userId = $userId;
if ($nodeType === 'view') {
$columns = $this->columnService->findAllByView($tableId, $this->userId);
$rows = $this->rowService->findAllByView($tableId, $this->userId, $limit, $offset);
} else {
// if no nodeType is provided, the old table selection is used to not break anything
$columns = $this->columnService->findAllByTable($tableId, null, $this->userId);
$rows = $this->rowService->findAllByTable($tableId, $this->userId, $limit, $offset);
}

$data = [];

// first line contains the titles
$header = [];
foreach ($columns as $column) {
$header[] = $column->getTitle();
}
$data[] = $header;

// now add the rows
foreach ($rows as $row) {
$rowData = $row->getDataArray();
$line = [];
foreach ($columns as $column) {
$value = '';
foreach ($rowData as $datum) {
if ($datum['columnId'] === $column->getId()) {
$value = $datum['value'];
}
}
$line[] = $value;
}
$data[] = $line;
}

return $data;
}
}

0 comments on commit 5144142

Please sign in to comment.