mirror of
https://github.com/nextcloud/tables.git
synced 2026-01-14 03:17:18 +00:00
92 lines
2.9 KiB
PHP
92 lines
2.9 KiB
PHP
<?php
|
|
|
|
/**
|
|
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
|
|
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<string, 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)->toResponseArray());
|
|
} catch (BadRequestError $e) {
|
|
return $this->handleBadRequestError($e);
|
|
} catch (NotFoundError $e) {
|
|
return $this->handleNotFoundError($e);
|
|
} catch (PermissionError $e) {
|
|
return $this->handlePermissionError($e);
|
|
} catch (InternalError|\Exception $e) {
|
|
return $this->handleError($e);
|
|
}
|
|
}
|
|
}
|