Browse Source

Add increment and test

Maurits van der Schee 6 years ago
parent
commit
b10054e7c7

+ 1
- 1
README.md View File

@@ -114,7 +114,7 @@ These features match features in v1 (see branch "v1"):
114 114
   - [x] Pagination, seeking, sorting and column selection
115 115
   - [x] Relation detection nested results (belongsTo, hasMany and HABTM)
116 116
   - [ ] ~~Relation "transforms" (of condensed JSON) for PHP and JavaScript~~
117
-  - [ ] Atomic increment support via PATCH (for counters)
117
+  - [x] Atomic increment support via PATCH (for counters)
118 118
   - [x] Binary fields supported with base64 encoding
119 119
   - [x] Spatial/GIS fields and filters supported with WKT
120 120
   - [ ] Unstructured data support through JSON/JSONB

+ 31
- 1
src/Tqdev/PhpCrudApi/Controller/RecordController.php View File

@@ -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
 }

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

@@ -1,9 +1,9 @@
1 1
 <?php
2 2
 namespace Tqdev\PhpCrudApi\Database;
3 3
 
4
+use Tqdev\PhpCrudApi\Column\Reflection\ReflectedTable;
4 5
 use Tqdev\PhpCrudApi\Record\Condition\ColumnCondition;
5 6
 use Tqdev\PhpCrudApi\Record\Condition\Condition;
6
-use Tqdev\PhpCrudApi\Column\Reflection\ReflectedTable;
7 7
 
8 8
 class GenericDB
9 9
 {
@@ -218,6 +218,22 @@ class GenericDB
218 218
         return $stmt->rowCount();
219 219
     }
220 220
 
221
+    public function incrementSingle(ReflectedTable $table, array $columnValues, String $id)
222
+    {
223
+        if (count($columnValues) == 0) {
224
+            return 0;
225
+        }
226
+        $this->converter->convertColumnValues($table, $columnValues);
227
+        $updateColumns = $this->columns->getIncrement($table, $columnValues);
228
+        $tableName = $table->getName();
229
+        $condition = new ColumnCondition($table->getPk(), 'eq', $id);
230
+        $parameters = array_values($columnValues);
231
+        $whereClause = $this->conditions->getWhereClause($condition, $parameters);
232
+        $sql = 'UPDATE "' . $tableName . '" SET ' . $updateColumns . $whereClause;
233
+        $stmt = $this->query($sql, $parameters);
234
+        return $stmt->rowCount();
235
+    }
236
+
221 237
     private function query(String $sql, array $parameters): \PDOStatement
222 238
     {
223 239
         $stmt = $this->pdo->prepare($sql);

+ 9
- 1
src/Tqdev/PhpCrudApi/Record/RecordService.php View File

@@ -1,9 +1,9 @@
1 1
 <?php
2 2
 namespace Tqdev\PhpCrudApi\Record;
3 3
 
4
+use Tqdev\PhpCrudApi\Column\ReflectionService;
4 5
 use Tqdev\PhpCrudApi\Database\GenericDB;
5 6
 use Tqdev\PhpCrudApi\Record\Document\ListDocument;
6
-use Tqdev\PhpCrudApi\Column\ReflectionService;
7 7
 
8 8
 class RecordService
9 9
 {
@@ -86,6 +86,14 @@ class RecordService
86 86
         return $this->db->deleteSingle($table, $id);
87 87
     }
88 88
 
89
+    public function increment(String $tableName, String $id, /* object */ $record, array $params)
90
+    {
91
+        $this->sanitizeRecord($tableName, $record, $id);
92
+        $table = $this->tables->get($tableName);
93
+        $columnValues = $this->columns->getValues($table, true, $record, $params);
94
+        return $this->db->incrementSingle($table, $columnValues, $id);
95
+    }
96
+
89 97
     public function _list(String $tableName, array $params): ListDocument
90 98
     {
91 99
         $table = $this->tables->get($tableName);

Loading…
Cancel
Save