-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
enh(API): add OCS API to create rows #1161
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,86 @@ | ||
<?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 (26 compatibility) | ||
*/ | ||
#[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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace OCA\Tables\Helper; | ||
|
||
use InvalidArgumentException; | ||
use OCA\Tables\AppInfo\Application; | ||
|
||
class ConversionHelper { | ||
|
||
/** | ||
* @throws InvalidArgumentException | ||
*/ | ||
public static function constNodeType2String(int $nodeType): string { | ||
return match ($nodeType) { | ||
Application::NODE_TYPE_TABLE => 'table', | ||
Application::NODE_TYPE_VIEW => 'view', | ||
default => throw new InvalidArgumentException('Invalid node type'), | ||
}; | ||
} | ||
|
||
/** | ||
* @throws InvalidArgumentException | ||
*/ | ||
public static function stringNodeType2Const(string $nodeType): int { | ||
return match ($nodeType) { | ||
'table', 'tables' => Application::NODE_TYPE_TABLE, | ||
'view', 'views' => Application::NODE_TYPE_VIEW, | ||
default => throw new InvalidArgumentException('Invalid node type'), | ||
}; | ||
} | ||
} |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a use case for returning true in the assert-Methods if we throw anways in case of no permission?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise I could not chain them in the match block: https://github.com/nextcloud/tables/pull/1161/files#diff-58a7c8ba568eef76405b8471729c4b735def0967160958d5ab0105ddb7afb34eR109-R112
could turn it into switch construction instead of course. I find the match more compact and readable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah ok, that makes sense then 👍