|
@@ -1,11 +1,11 @@
|
1
|
1
|
<?php
|
2
|
2
|
namespace Tqdev\PhpCrudApi\Controller;
|
3
|
3
|
|
|
4
|
+use Tqdev\PhpCrudApi\Middleware\Router\Router;
|
4
|
5
|
use Tqdev\PhpCrudApi\Record\ErrorCode;
|
5
|
6
|
use Tqdev\PhpCrudApi\Record\RecordService;
|
6
|
7
|
use Tqdev\PhpCrudApi\Request;
|
7
|
8
|
use Tqdev\PhpCrudApi\Response;
|
8
|
|
-use Tqdev\PhpCrudApi\Middleware\Router\Router;
|
9
|
9
|
|
10
|
10
|
class RecordController
|
11
|
11
|
{
|
|
@@ -130,4 +130,34 @@ class RecordController
|
130
|
130
|
}
|
131
|
131
|
}
|
132
|
132
|
|
|
133
|
+ public function increment(Request $request): Response
|
|
134
|
+ {
|
|
135
|
+ $table = $request->getPathSegment(2);
|
|
136
|
+ $id = $request->getPathSegment(3);
|
|
137
|
+ $record = $request->getBody();
|
|
138
|
+ if ($record === null) {
|
|
139
|
+ return $this->responder->error(ErrorCode::HTTP_MESSAGE_NOT_READABLE, '');
|
|
140
|
+ }
|
|
141
|
+ $params = $request->getParams();
|
|
142
|
+ if (!$this->service->exists($table)) {
|
|
143
|
+ return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $table);
|
|
144
|
+ }
|
|
145
|
+ $ids = explode(',', $id);
|
|
146
|
+ if (is_array($record)) {
|
|
147
|
+ if (count($ids) != count($record)) {
|
|
148
|
+ return $this->responder->error(ErrorCode::ARGUMENT_COUNT_MISMATCH, $id);
|
|
149
|
+ }
|
|
150
|
+ $result = array();
|
|
151
|
+ for ($i = 0; $i < count($ids); $i++) {
|
|
152
|
+ $result[] = $this->service->increment($table, $ids[$i], $record[$i], $params);
|
|
153
|
+ }
|
|
154
|
+ return $this->responder->success($result);
|
|
155
|
+ } else {
|
|
156
|
+ if (count($ids) != 1) {
|
|
157
|
+ return $this->responder->error(ErrorCode::ARGUMENT_COUNT_MISMATCH, $id);
|
|
158
|
+ }
|
|
159
|
+ return $this->responder->success($this->service->increment($table, $id, $record, $params));
|
|
160
|
+ }
|
|
161
|
+ }
|
|
162
|
+
|
133
|
163
|
}
|