diff --git a/src/Tqdev/PhpCrudApi/Controller/RecordController.php b/src/Tqdev/PhpCrudApi/Controller/RecordController.php index 7c5718f..658e807 100644 --- a/src/Tqdev/PhpCrudApi/Controller/RecordController.php +++ b/src/Tqdev/PhpCrudApi/Controller/RecordController.php @@ -60,6 +60,27 @@ class RecordController } } + private function multiCall(callable $method, array $argumentLists): array + { + $result = array(); + $success = true; + $this->service->beginTransaction(); + foreach ($argumentLists as $arguments) { + try { + $result[] = call_user_func_array($method, $arguments); + } catch (\Throwable $e) { + $success = false; + $result[] = null; + } + } + if ($success) { + $this->service->commitTransaction(); + } else { + $this->service->rollBackTransaction(); + } + return $result; + } + public function create(ServerRequestInterface $request): ResponseInterface { $table = RequestUtils::getPathSegment($request, 2); @@ -75,11 +96,11 @@ class RecordController } $params = RequestUtils::getParams($request); if (is_array($record)) { - $result = array(); + $argumentLists = array(); foreach ($record as $r) { - $result[] = $this->service->create($table, $r, $params); + $argumentLists[] = array($table, $r, $params); } - return $this->responder->success($result); + return $this->responder->success($this->multiCall([$this->service, 'create'], $argumentLists)); } else { return $this->responder->success($this->service->create($table, $record, $params)); } @@ -105,11 +126,11 @@ class RecordController if (count($ids) != count($record)) { return $this->responder->error(ErrorCode::ARGUMENT_COUNT_MISMATCH, $id); } - $result = array(); + $argumentLists = array(); for ($i = 0; $i < count($ids); $i++) { - $result[] = $this->service->update($table, $ids[$i], $record[$i], $params); + $argumentLists[] = array($table, $ids[$i], $record[$i], $params); } - return $this->responder->success($result); + return $this->responder->success($this->multiCall([$this->service, 'update'], $argumentLists)); } else { if (count($ids) != 1) { return $this->responder->error(ErrorCode::ARGUMENT_COUNT_MISMATCH, $id); diff --git a/src/Tqdev/PhpCrudApi/Database/GenericDB.php b/src/Tqdev/PhpCrudApi/Database/GenericDB.php index 23369ac..5a6b292 100644 --- a/src/Tqdev/PhpCrudApi/Database/GenericDB.php +++ b/src/Tqdev/PhpCrudApi/Database/GenericDB.php @@ -160,6 +160,21 @@ class GenericDB return $this->definition; } + public function beginTransaction() /*: void*/ + { + $this->pdo->beginTransaction(); + } + + public function commitTransaction() /*: void*/ + { + $this->pdo->commit(); + } + + public function rollBackTransaction() /*: void*/ + { + $this->pdo->rollBack(); + } + private function addMiddlewareConditions(string $tableName, Condition $condition): Condition { $condition1 = VariableStore::get("authorization.conditions.$tableName"); @@ -334,7 +349,7 @@ class GenericDB $this->port, $this->database, $this->tables, - $this->username + $this->username, ])); } } diff --git a/src/Tqdev/PhpCrudApi/Record/RecordService.php b/src/Tqdev/PhpCrudApi/Record/RecordService.php index b31ab13..d9276b3 100644 --- a/src/Tqdev/PhpCrudApi/Record/RecordService.php +++ b/src/Tqdev/PhpCrudApi/Record/RecordService.php @@ -56,6 +56,21 @@ class RecordService return $this->reflection->getType($table); } + public function beginTransaction() /*: void*/ + { + $this->db->beginTransaction(); + } + + public function commitTransaction() /*: void*/ + { + $this->db->commitTransaction(); + } + + public function rollBackTransaction() /*: void*/ + { + $this->db->rollBackTransaction(); + } + public function create(string $tableName, /* object */ $record, array $params) /*: ?int*/ { $this->sanitizeRecord($tableName, $record, ''); diff --git a/tests/functional/001_records/090_add_multiple_comments.log b/tests/functional/001_records/090_add_multiple_comments.log index f62cc6f..7d8ac15 100644 --- a/tests/functional/001_records/090_add_multiple_comments.log +++ b/tests/functional/001_records/090_add_multiple_comments.log @@ -21,6 +21,16 @@ POST /records/comments [{"user_id":1,"post_id":6,"message":"multi 3","category_id":3},{"user_id":1,"post_id":0,"message":"multi 4","category_id":3}] === +200 +Content-Type: application/json; charset=utf-8 +Content-Length: 8 + +[9,null] === GET /records/comments?include=id&filter=post_id,eq,6 === +200 +Content-Type: application/json; charset=utf-8 +Content-Length: 31 + +{"records":[{"id":7},{"id":8}]} diff --git a/tests/functional/001_records/091_edit_multiple_comments.log b/tests/functional/001_records/091_edit_multiple_comments.log index 2e6cbc6..32b1395 100644 --- a/tests/functional/001_records/091_edit_multiple_comments.log +++ b/tests/functional/001_records/091_edit_multiple_comments.log @@ -23,14 +23,14 @@ PUT /records/comments/7,8 === 200 Content-Type: application/json; charset=utf-8 -Content-Length: 5 +Content-Length: 8 -[0,0] +[1,null] === GET /records/comments?include=message&filter=post_id,eq,6 === 200 Content-Type: application/json; charset=utf-8 -Content-Length: 14 +Content-Length: 57 -{"records":[]} +{"records":[{"message":"multi 1"},{"message":"multi 2"}]}