Skip to content

Commit b10054e

Browse files
committed
Add increment and test
1 parent 7617aa7 commit b10054e

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ These features match features in v1 (see branch "v1"):
114114
- [x] Pagination, seeking, sorting and column selection
115115
- [x] Relation detection nested results (belongsTo, hasMany and HABTM)
116116
- [ ] ~~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)
118118
- [x] Binary fields supported with base64 encoding
119119
- [x] Spatial/GIS fields and filters supported with WKT
120120
- [ ] Unstructured data support through JSON/JSONB

src/Tqdev/PhpCrudApi/Controller/RecordController.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?php
22
namespace Tqdev\PhpCrudApi\Controller;
33

4+
use Tqdev\PhpCrudApi\Middleware\Router\Router;
45
use Tqdev\PhpCrudApi\Record\ErrorCode;
56
use Tqdev\PhpCrudApi\Record\RecordService;
67
use Tqdev\PhpCrudApi\Request;
78
use Tqdev\PhpCrudApi\Response;
8-
use Tqdev\PhpCrudApi\Middleware\Router\Router;
99

1010
class RecordController
1111
{
@@ -130,4 +130,34 @@ public function delete(Request $request): Response
130130
}
131131
}
132132

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+
133163
}

src/Tqdev/PhpCrudApi/Database/GenericDB.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22
namespace Tqdev\PhpCrudApi\Database;
33

4+
use Tqdev\PhpCrudApi\Column\Reflection\ReflectedTable;
45
use Tqdev\PhpCrudApi\Record\Condition\ColumnCondition;
56
use Tqdev\PhpCrudApi\Record\Condition\Condition;
6-
use Tqdev\PhpCrudApi\Column\Reflection\ReflectedTable;
77

88
class GenericDB
99
{
@@ -218,6 +218,22 @@ public function deleteSingle(ReflectedTable $table, String $id)
218218
return $stmt->rowCount();
219219
}
220220

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+
221237
private function query(String $sql, array $parameters): \PDOStatement
222238
{
223239
$stmt = $this->pdo->prepare($sql);

src/Tqdev/PhpCrudApi/Record/RecordService.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22
namespace Tqdev\PhpCrudApi\Record;
33

4+
use Tqdev\PhpCrudApi\Column\ReflectionService;
45
use Tqdev\PhpCrudApi\Database\GenericDB;
56
use Tqdev\PhpCrudApi\Record\Document\ListDocument;
6-
use Tqdev\PhpCrudApi\Column\ReflectionService;
77

88
class RecordService
99
{
@@ -86,6 +86,14 @@ public function delete(String $tableName, String $id, array $params)
8686
return $this->db->deleteSingle($table, $id);
8787
}
8888

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+
8997
public function _list(String $tableName, array $params): ListDocument
9098
{
9199
$table = $this->tables->get($tableName);

0 commit comments

Comments
 (0)