mirror of
https://github.com/nextcloud/tables.git
synced 2026-01-14 03:17:18 +00:00
attach columname to response
This commit is contained in:
@ -77,7 +77,7 @@ class RowOCSController extends AOCSController {
|
||||
}
|
||||
|
||||
try {
|
||||
return new DataResponse($this->rowService->create($tableId, $viewId, $newRowData)->jsonSerialize());
|
||||
return new DataResponse($this->rowService->create($tableId, $viewId, $newRowData)->toResponseArray());
|
||||
} catch (BadRequestError $e) {
|
||||
return $this->handleBadRequestError($e);
|
||||
} catch (NotFoundError $e) {
|
||||
|
||||
@ -23,6 +23,7 @@ class Row2 implements JsonSerializable {
|
||||
private ?string $lastEditBy = null;
|
||||
private ?string $lastEditAt = null;
|
||||
private ?array $data = [];
|
||||
private array $cellMeta = [];
|
||||
private array $changedColumnIds = []; // collect column ids that have changed after $loaded = true
|
||||
|
||||
private bool $loaded = false; // set to true if model is loaded, after that changed column ids will be collected
|
||||
@ -133,6 +134,16 @@ class Row2 implements JsonSerializable {
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* add response-only metadata for a specific column
|
||||
*/
|
||||
public function addCellMeta(int $columnId, array $meta): void {
|
||||
if (!isset($this->cellMeta[$columnId])) {
|
||||
$this->cellMeta[$columnId] = [];
|
||||
}
|
||||
$this->cellMeta[$columnId] = array_merge($this->cellMeta[$columnId], $meta);
|
||||
}
|
||||
|
||||
/**
|
||||
* @psalm-return TablesRow
|
||||
*/
|
||||
@ -148,6 +159,31 @@ class Row2 implements JsonSerializable {
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* return merged response-only metadata into the data cells.
|
||||
*/
|
||||
public function toResponseArray(): array {
|
||||
$data = [];
|
||||
foreach ($this->data as $cell) {
|
||||
$colId = $cell['columnId'];
|
||||
$merged = $cell;
|
||||
if (isset($this->cellMeta[$colId])) {
|
||||
$merged = array_merge($merged, $this->cellMeta[$colId]);
|
||||
}
|
||||
$data[] = $merged;
|
||||
}
|
||||
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'tableId' => $this->tableId,
|
||||
'createdBy' => $this->createdBy,
|
||||
'createdAt' => $this->createdAt,
|
||||
'lastEditBy' => $this->lastEditBy,
|
||||
'lastEditAt' => $this->lastEditAt,
|
||||
'data' => $data,
|
||||
];
|
||||
}
|
||||
|
||||
public function toPublicRow(?array $previousValues = null): Row {
|
||||
return new Row(
|
||||
tableId: $this->tableId,
|
||||
|
||||
@ -208,6 +208,28 @@ class RowService extends SuperService {
|
||||
throw new InternalError('Cannot create row without table or view in context');
|
||||
}
|
||||
|
||||
$fullRowData = [];
|
||||
$columnNames = [];
|
||||
|
||||
foreach ($columns as $c) {
|
||||
$columnNames[$c->getId()] = $c->getTitle();
|
||||
}
|
||||
|
||||
$rows = $data instanceof RowDataInput ? iterator_to_array($data) : $data;
|
||||
|
||||
foreach ($rows as $r) {
|
||||
$colId = (int)$r['columnId'];
|
||||
if (!isset($columnNames[$colId])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$fullRowData[] = [
|
||||
'columnId' => $colId,
|
||||
'value' => $r['value'],
|
||||
'columnName' => $columnNames[$colId],
|
||||
];
|
||||
}
|
||||
|
||||
$tableId = $tableId ?? $view->getTableId();
|
||||
|
||||
$data = $data instanceof RowDataInput ? $data : RowDataInput::fromArray($data);
|
||||
@ -220,6 +242,12 @@ class RowService extends SuperService {
|
||||
$row2->setData($data);
|
||||
try {
|
||||
$insertedRow = $this->row2Mapper->insert($row2);
|
||||
// attached columnname to returned row for the response only
|
||||
foreach ($fullRowData as $meta) {
|
||||
if (isset($meta['columnId']) && array_key_exists('columnName', $meta)) {
|
||||
$insertedRow->addCellMeta((int)$meta['columnId'], ['columnName' => $meta['columnName']]);
|
||||
}
|
||||
}
|
||||
|
||||
$this->eventDispatcher->dispatchTyped(new RowAddedEvent($insertedRow));
|
||||
$this->activityManager->triggerEvent(
|
||||
@ -334,7 +362,7 @@ class RowService extends SuperService {
|
||||
if (!$column && $viewId) {
|
||||
throw new InternalError('Column with id ' . $entry['columnId'] . ' is not part of view with id ' . $viewId);
|
||||
} elseif (!$column && $tableId) {
|
||||
throw new InternalError('Column with id ' . $entry['columnId'] . ' is not part of table with id ' . $tableId);
|
||||
throw new BadRequestError('Column with id ' . $entry['columnId'] . ' is not part of table with id ' . $tableId);
|
||||
}
|
||||
|
||||
if (!$column) {
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
- SPDX-FileCopyrightText: 2021 Florian Steffens
|
||||
- SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
-->
|
||||
<phpunit bootstrap="tests/bootstrap.php" colors="true">
|
||||
<phpunit bootstrap="tests/Unit/bootstrap.php" colors="true">
|
||||
<testsuites>
|
||||
<testsuite name="unit">
|
||||
<directory>./tests/Unit</directory>
|
||||
|
||||
32
tests/unit/Db/Row2Test.php
Normal file
32
tests/unit/Db/Row2Test.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OCA\Tables\Tests\Unit\Db;
|
||||
|
||||
use OCA\Tables\Db\Row2;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class Row2Test extends TestCase {
|
||||
public function testToResponseArrayMergesMetaButJsonSerializeDoesNot(): void {
|
||||
$row = new Row2();
|
||||
$row->setTableId(1);
|
||||
|
||||
$row->setData([
|
||||
['columnId' => 57, 'value' => 'foo'],
|
||||
['columnId' => 58, 'value' => 'bar'],
|
||||
]);
|
||||
|
||||
$json = $row->jsonSerialize();
|
||||
$this->assertArrayNotHasKey('columnName', $json['data'][0]);
|
||||
|
||||
$row->addCellMeta(57, ['columnName' => 'Title 57']);
|
||||
|
||||
$resp = $row->toResponseArray();
|
||||
$this->assertArrayHasKey('columnName', $resp['data'][0]);
|
||||
$this->assertSame('Title 57', $resp['data'][0]['columnName']);
|
||||
|
||||
$json2 = $row->jsonSerialize();
|
||||
$this->assertArrayNotHasKey('columnName', $json2['data'][0]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user