Maurits van der Schee 3 years ago
parent
commit
79028e714a

+ 27
- 6
src/Tqdev/PhpCrudApi/Controller/RecordController.php View File

@@ -60,6 +60,27 @@ class RecordController
60 60
         }
61 61
     }
62 62
 
63
+    private function multiCall(callable $method, array $argumentLists): array
64
+    {
65
+        $result = array();
66
+        $success = true;
67
+        $this->service->beginTransaction();
68
+        foreach ($argumentLists as $arguments) {
69
+            try {
70
+                $result[] = call_user_func_array($method, $arguments);
71
+            } catch (\Throwable $e) {
72
+                $success = false;
73
+                $result[] = null;
74
+            }
75
+        }
76
+        if ($success) {
77
+            $this->service->commitTransaction();
78
+        } else {
79
+            $this->service->rollBackTransaction();
80
+        }
81
+        return $result;
82
+    }
83
+
63 84
     public function create(ServerRequestInterface $request): ResponseInterface
64 85
     {
65 86
         $table = RequestUtils::getPathSegment($request, 2);
@@ -75,11 +96,11 @@ class RecordController
75 96
         }
76 97
         $params = RequestUtils::getParams($request);
77 98
         if (is_array($record)) {
78
-            $result = array();
99
+            $argumentLists = array();
79 100
             foreach ($record as $r) {
80
-                $result[] = $this->service->create($table, $r, $params);
101
+                $argumentLists[] = array($table, $r, $params);
81 102
             }
82
-            return $this->responder->success($result);
103
+            return $this->responder->success($this->multiCall([$this->service, 'create'], $argumentLists));
83 104
         } else {
84 105
             return $this->responder->success($this->service->create($table, $record, $params));
85 106
         }
@@ -105,11 +126,11 @@ class RecordController
105 126
             if (count($ids) != count($record)) {
106 127
                 return $this->responder->error(ErrorCode::ARGUMENT_COUNT_MISMATCH, $id);
107 128
             }
108
-            $result = array();
129
+            $argumentLists = array();
109 130
             for ($i = 0; $i < count($ids); $i++) {
110
-                $result[] = $this->service->update($table, $ids[$i], $record[$i], $params);
131
+                $argumentLists[] = array($table, $ids[$i], $record[$i], $params);
111 132
             }
112
-            return $this->responder->success($result);
133
+            return $this->responder->success($this->multiCall([$this->service, 'update'], $argumentLists));
113 134
         } else {
114 135
             if (count($ids) != 1) {
115 136
                 return $this->responder->error(ErrorCode::ARGUMENT_COUNT_MISMATCH, $id);

+ 16
- 1
src/Tqdev/PhpCrudApi/Database/GenericDB.php View File

@@ -160,6 +160,21 @@ class GenericDB
160 160
         return $this->definition;
161 161
     }
162 162
 
163
+    public function beginTransaction() /*: void*/
164
+    {
165
+        $this->pdo->beginTransaction();
166
+    }
167
+
168
+    public function commitTransaction() /*: void*/
169
+    {
170
+        $this->pdo->commit();
171
+    }
172
+
173
+    public function rollBackTransaction() /*: void*/
174
+    {
175
+        $this->pdo->rollBack();
176
+    }
177
+
163 178
     private function addMiddlewareConditions(string $tableName, Condition $condition): Condition
164 179
     {
165 180
         $condition1 = VariableStore::get("authorization.conditions.$tableName");
@@ -334,7 +349,7 @@ class GenericDB
334 349
             $this->port,
335 350
             $this->database,
336 351
             $this->tables,
337
-            $this->username
352
+            $this->username,
338 353
         ]));
339 354
     }
340 355
 }

+ 15
- 0
src/Tqdev/PhpCrudApi/Record/RecordService.php View File

@@ -56,6 +56,21 @@ class RecordService
56 56
         return $this->reflection->getType($table);
57 57
     }
58 58
 
59
+    public function beginTransaction() /*: void*/
60
+    {
61
+        $this->db->beginTransaction();
62
+    }
63
+
64
+    public function commitTransaction() /*: void*/
65
+    {
66
+        $this->db->commitTransaction();
67
+    }
68
+
69
+    public function rollBackTransaction() /*: void*/
70
+    {
71
+        $this->db->rollBackTransaction();
72
+    }
73
+
59 74
     public function create(string $tableName, /* object */ $record, array $params) /*: ?int*/
60 75
     {
61 76
         $this->sanitizeRecord($tableName, $record, '');

+ 10
- 0
tests/functional/001_records/090_add_multiple_comments.log View File

@@ -21,6 +21,16 @@ POST /records/comments
21 21
 
22 22
 [{"user_id":1,"post_id":6,"message":"multi 3","category_id":3},{"user_id":1,"post_id":0,"message":"multi 4","category_id":3}]
23 23
 ===
24
+200
25
+Content-Type: application/json; charset=utf-8
26
+Content-Length: 8
27
+
28
+[9,null]
24 29
 ===
25 30
 GET /records/comments?include=id&filter=post_id,eq,6
26 31
 ===
32
+200
33
+Content-Type: application/json; charset=utf-8
34
+Content-Length: 31
35
+
36
+{"records":[{"id":7},{"id":8}]}

+ 4
- 4
tests/functional/001_records/091_edit_multiple_comments.log View File

@@ -23,14 +23,14 @@ PUT /records/comments/7,8
23 23
 ===
24 24
 200
25 25
 Content-Type: application/json; charset=utf-8
26
-Content-Length: 5
26
+Content-Length: 8
27 27
 
28
-[0,0]
28
+[1,null]
29 29
 ===
30 30
 GET /records/comments?include=message&filter=post_id,eq,6
31 31
 ===
32 32
 200
33 33
 Content-Type: application/json; charset=utf-8
34
-Content-Length: 14
34
+Content-Length: 57
35 35
 
36
-{"records":[]}
36
+{"records":[{"message":"multi 1"},{"message":"multi 2"}]}

Loading…
Cancel
Save