|
@@ -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);
|