-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enh(API): add OCS API to create rows
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
- Loading branch information
Showing
10 changed files
with
904 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
namespace OCA\Tables\Controller; | ||
|
||
use OCA\Tables\AppInfo\Application; | ||
use OCA\Tables\Errors\BadRequestError; | ||
use OCA\Tables\Errors\InternalError; | ||
use OCA\Tables\Errors\NotFoundError; | ||
use OCA\Tables\Errors\PermissionError; | ||
use OCA\Tables\Helper\ConversionHelper; | ||
use OCA\Tables\Middleware\Attribute\RequirePermission; | ||
use OCA\Tables\Model\RowDataInput; | ||
use OCA\Tables\ResponseDefinitions; | ||
use OCA\Tables\Service\RowService; | ||
use OCP\AppFramework\Http; | ||
use OCP\AppFramework\Http\Attribute\NoAdminRequired; | ||
use OCP\AppFramework\Http\DataResponse; | ||
use OCP\IL10N; | ||
use OCP\IRequest; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* @psalm-import-type TablesRow from ResponseDefinitions | ||
*/ | ||
class RowOCSController extends AOCSController { | ||
|
||
public function __construct( | ||
IRequest $request, | ||
LoggerInterface $logger, | ||
IL10N $n, | ||
string $userId, | ||
protected RowService $rowService, | ||
) { | ||
parent::__construct($request, $logger, $n, $userId); | ||
} | ||
|
||
/** | ||
* [api v2] Create a new row in a table or a view | ||
* | ||
* @param 'tables'|'views' $nodeCollection Indicates whether to create a row on a table or view | ||
* @param int $nodeId The identifier of the targeted table or view | ||
* @param string|array{columnId: int, value: mixed} $data An array containing the column identifiers and their values | ||
* @return DataResponse<Http::STATUS_OK, TablesRow, array{}>|DataResponse<Http::STATUS_FORBIDDEN|Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}> | ||
* | ||
* 200: Row returned | ||
* 400: Invalid request parameters | ||
* 403: No permissions | ||
* 404: Not found | ||
* 500: Internal error | ||
*/ | ||
#[NoAdminRequired] | ||
#[RequirePermission(permission: Application::PERMISSION_CREATE, typeParam: 'nodeCollection')] | ||
public function createRow(string $nodeCollection, int $nodeId, mixed $data): DataResponse { | ||
if (is_string($data)) { | ||
$data = json_decode($data, true); | ||
} | ||
if (!is_array($data)) { | ||
return $this->handleBadRequestError(new BadRequestError('Cannot create row: data input is invalid.')); | ||
} | ||
|
||
$iNodeType = ConversionHelper::stringNodeType2Const($nodeCollection); | ||
$tableId = $viewId = null; | ||
if ($iNodeType === Application::NODE_TYPE_TABLE) { | ||
$tableId = $nodeId; | ||
} elseif ($iNodeType === Application::NODE_TYPE_VIEW) { | ||
$viewId = $nodeId; | ||
} | ||
|
||
$newRowData = new RowDataInput(); | ||
foreach ($data as $key => $value) { | ||
$newRowData->add((int)$key, $value); | ||
} | ||
|
||
try { | ||
return new DataResponse($this->rowService->create($tableId, $viewId, $newRowData)->jsonSerialize()); | ||
} catch (NotFoundError $e) { | ||
return $this->handleNotFoundError($e); | ||
} catch (PermissionError $e) { | ||
return $this->handlePermissionError($e); | ||
} catch (InternalError|\Exception $e) { | ||
return $this->handleError($e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace OCA\Tables\Model; | ||
|
||
class Permissions { | ||
public function __construct( | ||
public bool $read = false, | ||
public bool $create = false, | ||
public bool $update = false, | ||
public bool $delete = false, | ||
public bool $manage = false, | ||
public bool $manageTable = false, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace OCA\Tables\Model; | ||
|
||
use ArrayAccess; | ||
|
||
/** | ||
* @template-implements ArrayAccess<mixed, array{'columnId': int, 'value': mixed}> | ||
*/ | ||
class RowDataInput implements ArrayAccess { | ||
protected const DATA_KEY = 'columnId'; | ||
protected const DATA_VAL = 'value'; | ||
/** @psalm-var array<array{'columnId': int, 'value': mixed}> */ | ||
protected array $data = []; | ||
|
||
public function add(int $columnId, string $value): self { | ||
$this->data[] = [self::DATA_KEY => $columnId, self::DATA_VAL => $value]; | ||
return $this; | ||
} | ||
|
||
public function offsetExists(mixed $offset): bool { | ||
foreach ($this->data as $data) { | ||
if ($data[self::DATA_KEY] === $offset[self::DATA_KEY]) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public function offsetGet(mixed $offset): mixed { | ||
return $this->data[$offset]; | ||
} | ||
|
||
public function offsetSet(mixed $offset, mixed $value): void { | ||
$this->data[$offset] = $value; | ||
} | ||
|
||
public function offsetUnset(mixed $offset): void { | ||
if (isset($this->data[$offset])) { | ||
unset($this->data[$offset]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.