Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 45 additions & 25 deletions lib/Controller/RowOCSController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use OCA\Tables\ResponseDefinitions;
use OCA\Tables\Service\FederationService;
use OCA\Tables\Service\RowObjectSerializer;
use OCA\Tables\Service\RowObjectXmlShaper;
use OCA\Tables\Service\RowService;
use OCA\Tables\Service\TableService;
use OCA\Tables\Service\ViewService;
Expand Down Expand Up @@ -53,6 +54,7 @@ public function __construct(
private readonly ViewService $viewService,
private readonly FederationService $federationService,
private readonly RowObjectSerializer $rowObjectSerializer,
private readonly RowObjectXmlShaper $rowObjectXmlShaper,
) {
parent::__construct($request, $logger, $n, $userId);
}
Expand All @@ -63,11 +65,11 @@ public function __construct(
* @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 The values of the row: keyed by column id, or with shape "object" keyed by technical name, as the object shape reads a row: a relation as the row id or the label of its target, where labels are checked before row ids and the first row with a matching label wins, so an integer is always a row id and a numeric string is tried as a label first; an @self block, its id and uuid included, is ignored
* @param 'cells'|'object'|null $shape The shape of the data and of the returned row, defaults to "cells"; the object shape is only available as JSON
* @param 'cells'|'object'|null $shape The shape of the data and of the returned row, defaults to "cells". The data is sent as JSON or form data; in an XML response a row in the object shape has an element per column, see the shape of getRow
* @return DataResponse<Http::STATUS_OK, TablesRow|TablesRowObject, array{}>|DataResponse<Http::STATUS_BAD_REQUEST, array{message: string}|TablesRowValidationError, array{}>|DataResponse<Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
*
* 200: Row returned
* 400: Invalid request parameters, an unknown shape or the object shape for a response that is not JSON (nothing is written then), or with shape "object" the values that cannot be stored, per technical name
* 400: Invalid request parameters or an unknown shape (nothing is written then), or with shape "object" the values that cannot be stored, per technical name; in XML errors has an element per technical name, by the rules of the object shape
* 403: No permissions
* 404: Not found
* 500: Internal error
Expand All @@ -77,7 +79,7 @@ public function __construct(
#[ApiRoute(verb: 'POST', url: '/api/2/{nodeCollection}/{nodeId}/rows', requirements: ['nodeCollection' => '(tables|views)', 'nodeId' => '(\d+)'])]
public function createRow(string $nodeCollection, int $nodeId, mixed $data, ?string $shape = null): DataResponse {
try {
$rowShape = $this->rowShapeFromRequest($shape);
$rowShape = RowShape::fromRequest($shape);
} catch (\InvalidArgumentException $e) {
return $this->handleBadRequestError(new BadRequestError($e->getMessage()));
}
Expand Down Expand Up @@ -122,7 +124,7 @@ public function createRow(string $nodeCollection, int $nodeId, mixed $data, ?str
}
return new DataResponse($this->shapeRow($this->rowService->create($tableId, $viewId, $newRowData), $rowShape));
} catch (RowValidationError $e) {
return $this->handleRowValidationError($e);
return $this->rowValidationError($e);
} catch (BadRequestError $e) {
return $this->handleBadRequestError($e);
} catch (NotFoundError $e) {
Expand Down Expand Up @@ -151,7 +153,7 @@ public function createRow(string $nodeCollection, int $nodeId, mixed $data, ?str
* @param string|null $search Text to look for in the text cells and the selection labels
* @param string|null $fields Comma separated technical names or column ids of the columns to return, the metadata of a row is always returned
* @param array<string, string|array<string, string>>|null $filter Filters as filter[field]=value or filter[field][operator]=value, all of which must match. A deep object in the query string (for example filter[age][is-greater-than]=30&filter[name]=jane), shown as a string because the API description cannot express nested query parameters. field is a technical name, a column id or a meta field; operator is one of the view filter operators that fit the field's type (without one: is-equal, or contains-item on multi-value columns; long and rich text need an operator such as contains); a value is compared as given and written as the column stores it: a decimal number; a row id for relations and id; YYYY-MM-DD HH:MM for date and time columns, YYYY-MM-DD for date columns, HH:MM for time columns; YYYY-MM-DD with an optional HH:MM or HH:MM:SS, after a space or a T, for createdAt and updatedAt, in UTC unless the time ends in Z or an offset such as +02:00 (written %2B02:00 in the query string, where + means a space), an exact moment, so compare those with is-greater-than and is-lower-than rather than is-equal; an option id for selections; true or false for checkboxes; a user, group or team id for usergroup columns
* @param 'cells'|'object'|null $shape The shape of the returned rows, defaults to "cells". With "object" a row is one object with a property per column, in the column order of the table or view, keyed by its technical name and null without a value: datetimes with the offset of the instance timezone, links as {title, url, providerId}, relations as the row id of the target row (null when it no longer exists), and the metadata in @self, with its times in UTC; only available as JSON
* @param 'cells'|'object'|null $shape The shape of the returned rows, defaults to "cells". With "object" a row is one object with a property per column, in the column order of the table or view, keyed by its technical name and null without a value: datetimes with the offset of the instance timezone, links as {title, url, providerId}, relations as the row id of the target row (null when it no longer exists), and the metadata in @self, with its times in UTC. In XML a row is an element holding <self/>, with the metadata as attributes and an attribute without a value left out, and an element per column named by its technical name; a name that is not an ASCII XML name (a letter or _, then letters, digits, _, . or -), or is element or self, becomes <element name="...">, which holds the value in <value> unless it is a list or an object, so there a scalar and an object with only a value property look the same (no column type gives such an object). null is an empty element with nil="true", unprefixed because xsi:nil would need a namespace declaration; an empty string or list is an empty element without it. Booleans are true and false, a list has an <element> per item and an object a child element per property, by the same rules
* @return DataResponse<Http::STATUS_OK, TablesRowPage|TablesRowObjectPage, array{}>|DataResponse<Http::STATUS_FORBIDDEN|Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
*
* 200: Rows returned
Expand Down Expand Up @@ -182,15 +184,15 @@ public function getRows(
}
try {
$query = RowListQuery::createFromRequest($page, $offset, $limit, $sort, $search, $fields, $filter);
$rowShape = $this->rowShapeFromRequest($shape);
$rowShape = RowShape::fromRequest($shape);
} catch (\InvalidArgumentException $e) {
throw new BadRequestError($e->getMessage());
}

$found = $this->rowService->findPage($query, $tableId, $viewId, $this->userId);
return new DataResponse([
'results' => $rowShape === RowShape::Object
? $this->rowObjectSerializer->serializeAll(array_values($found['rows']))
? $this->rowObjects($found['rows'])
: array_values(array_map(static fn ($row) => $row->jsonSerialize(), $found['rows'])),
'total' => $found['total'],
'limit' => $query->limit,
Expand All @@ -214,11 +216,11 @@ public function getRows(
* @param 'tables'|'views' $nodeCollection Indicates whether to get a row from a table or view
* @param int $nodeId The identifier of the targeted table or view
* @param int $rowId The identifier of the row
* @param 'cells'|'object'|null $shape The shape of the returned row, defaults to "cells". With "object" a row is one object with a property per column, in the column order of the table or view, keyed by its technical name and null without a value: datetimes with the offset of the instance timezone, links as {title, url, providerId}, relations as the row id of the target row (null when it no longer exists), and the metadata in @self, with its times in UTC; only available as JSON
* @param 'cells'|'object'|null $shape The shape of the returned row, defaults to "cells". With "object" a row is one object with a property per column, in the column order of the table or view, keyed by its technical name and null without a value: datetimes with the offset of the instance timezone, links as {title, url, providerId}, relations as the row id of the target row (null when it no longer exists), and the metadata in @self, with its times in UTC. In XML a row is an element holding <self/>, with the metadata as attributes and an attribute without a value left out, and an element per column named by its technical name; a name that is not an ASCII XML name (a letter or _, then letters, digits, _, . or -), or is element or self, becomes <element name="...">, which holds the value in <value> unless it is a list or an object, so there a scalar and an object with only a value property look the same (no column type gives such an object). null is an empty element with nil="true", unprefixed because xsi:nil would need a namespace declaration; an empty string or list is an empty element without it. Booleans are true and false, a list has an <element> per item and an object a child element per property, by the same rules
* @return DataResponse<Http::STATUS_OK, TablesRow|TablesRowObject, 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: An unknown shape or the object shape for a response that is not JSON, or the table or view is federated
* 400: An unknown shape, or the table or view is federated
* 403: No permissions
* 404: Not found
* 500: Internal error
Expand All @@ -230,7 +232,7 @@ public function getRow(string $nodeCollection, int $nodeId, int $rowId, ?string
[$tableId, $viewId] = $this->splitNode($nodeCollection, $nodeId);
try {
try {
$rowShape = $this->rowShapeFromRequest($shape);
$rowShape = RowShape::fromRequest($shape);
} catch (\InvalidArgumentException $e) {
throw new BadRequestError($e->getMessage());
}
Expand All @@ -256,11 +258,11 @@ public function getRow(string $nodeCollection, int $nodeId, int $rowId, ?string
* @param int $nodeId The identifier of the targeted table or view
* @param int $rowId The identifier of the row to update
* @param string|array<string, mixed> $data The values to change: keyed by column id, or with shape "object" keyed by technical name, as the object shape reads a row: a relation as the row id or the label of its target, where labels are checked before row ids and the first row with a matching label wins, so an integer is always a row id and a numeric string is tried as a label first; an @self block, its id and uuid included, is ignored
* @param 'cells'|'object'|null $shape The shape of the data and of the returned row, defaults to "cells"; the object shape is only available as JSON
* @param 'cells'|'object'|null $shape The shape of the data and of the returned row, defaults to "cells". The data is sent as JSON or form data; in an XML response a row in the object shape has an element per column, see the shape of getRow
* @return DataResponse<Http::STATUS_OK, TablesRow|TablesRowObject, array{}>|DataResponse<Http::STATUS_BAD_REQUEST, array{message: string}|TablesRowValidationError, array{}>|DataResponse<Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}>
*
* 200: Row updated
* 400: Invalid request parameters, an unknown shape or the object shape for a response that is not JSON (nothing is written then), or with shape "object" the values that cannot be stored, per technical name
* 400: Invalid request parameters or an unknown shape (nothing is written then), or with shape "object" the values that cannot be stored, per technical name; in XML errors has an element per technical name, by the rules of the object shape
* 403: No permissions
* 404: Not found
* 500: Internal error
Expand All @@ -270,7 +272,7 @@ public function getRow(string $nodeCollection, int $nodeId, int $rowId, ?string
#[ApiRoute(verb: 'PUT', url: '/api/2/{nodeCollection}/{nodeId}/rows/{rowId}', requirements: ['nodeCollection' => '(tables|views)', 'nodeId' => '(\d+)', 'rowId' => '(\d+)'])]
public function updateRow(string $nodeCollection, int $nodeId, int $rowId, mixed $data, ?string $shape = null): DataResponse {
try {
$rowShape = $this->rowShapeFromRequest($shape);
$rowShape = RowShape::fromRequest($shape);
} catch (\InvalidArgumentException $e) {
return $this->handleBadRequestError(new BadRequestError($e->getMessage()));
}
Expand Down Expand Up @@ -299,7 +301,7 @@ public function updateRow(string $nodeCollection, int $nodeId, int $rowId, mixed
}
return new DataResponse($this->shapeRow($this->rowService->updateSet($rowId, $viewId, $values, $this->userId, $tableId), $rowShape));
} catch (RowValidationError $e) {
return $this->handleRowValidationError($e);
return $this->rowValidationError($e);
} catch (BadRequestError $e) {
return $this->handleBadRequestError($e);
} catch (NotFoundError $e) {
Expand Down Expand Up @@ -355,25 +357,43 @@ public function deleteRow(string $nodeCollection, int $nodeId, int $rowId): Data
}

/**
* The shape a request asks for. The object shape is refused for an XML response, which
* OCS gives unless the format parameter or else the Accept header asks for JSON.
*
* @throws \InvalidArgumentException
* OCS answers in XML unless the format parameter or else the Accept header asks for JSON.
*/
private function rowShapeFromRequest(?string $shape): RowShape {
$rowShape = RowShape::fromRequest($shape);
if ($rowShape === RowShape::Object && $this->request->getFormat() !== 'json') {
throw new \InvalidArgumentException('The object shape is only available as JSON.');
}
return $rowShape;
private function isXmlResponse(): bool {
return $this->request->getFormat() !== 'json';
}

/**
* @return TablesRow|TablesRowObject
* @throws InternalError
*/
private function shapeRow(Row2 $row, RowShape $rowShape): array {
return $rowShape === RowShape::Object ? $this->rowObjectSerializer->serialize($row) : $row->jsonSerialize();
if ($rowShape === RowShape::Cells) {
return $row->jsonSerialize();
}
$object = $this->rowObjectSerializer->serialize($row);
return $this->isXmlResponse() ? $this->rowObjectXmlShaper->shapeRow($object) : $object;
}

/**
* @param Row2[] $rows
* @return list<TablesRowObject>
* @throws InternalError
*/
private function rowObjects(array $rows): array {
$objects = $this->rowObjectSerializer->serializeAll(array_values($rows));
return $this->isXmlResponse() ? $this->rowObjectXmlShaper->shapeRows($objects) : $objects;
}

/**
* @return DataResponse<Http::STATUS_BAD_REQUEST, TablesRowValidationError, array{}>
*/
private function rowValidationError(RowValidationError $e): DataResponse {
$response = $this->handleRowValidationError($e);
if ($this->isXmlResponse()) {
$response->setData($this->rowObjectXmlShaper->shapeValidationError($response->getData()));
}
return $response;
}

/**
Expand Down
Loading
Loading