Skip to content

Commit 943a9a5

Browse files
committed
improvements for #744
1 parent 8ab4027 commit 943a9a5

File tree

6 files changed

+31
-11
lines changed

6 files changed

+31
-11
lines changed

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,10 +554,25 @@ contain the same number of objects as there are primary keys in the URL:
554554

555555
This adjusts the titles of the posts. And the return values are the number of rows that are set:
556556

557-
1,1
557+
[1,1]
558558

559559
Which means that there were two update operations and each of them had set one row. Batch operations use database
560-
transactions, so they either all succeed or all fail (successful ones get roled back).
560+
transactions, so they either all succeed or all fail (successful ones get roled back). If they fail the body will
561+
contain the list of error documents. In the following response the first operation succeeded and the second operation
562+
of the batch failed due to an integrity violation:
563+
564+
[
565+
{
566+
"code": 0,
567+
"message": "Success"
568+
},
569+
{
570+
"code": 1010,
571+
"message": "Data integrity violation"
572+
}
573+
]
574+
575+
The response status code will always be 424 (failed dependency) in case of failure of one of the batch operations.
561576

562577
### Spatial support
563578

src/Tqdev/PhpCrudApi/Controller/JsonResponder.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,21 @@ public function exception($exception): ResponseInterface
3939

4040
public function multi($results): ResponseInterface
4141
{
42-
$document = array();
42+
$documents = array();
43+
$errors = array();
4344
$success = true;
4445
foreach ($results as $i=>$result) {
4546
if ($result instanceof \Throwable) {
46-
$document[$i] = ErrorDocument::fromException($result);
47+
$documents[$i] = null;
48+
$errors[$i] = ErrorDocument::fromException($result);
4749
$success = false;
4850
} else {
49-
$document[$i] = $result;
51+
$documents[$i] = $result;
52+
$errors[$i] = new ErrorDocument(new ErrorCode(0),'',null);
5053
}
5154
}
5255
$status = $success ? ResponseFactory::OK : ResponseFactory::FAILED_DEPENDENCY;
56+
$document = $success ? $documents : $errors;
5357
$response = ResponseFactory::fromObject($status, $document);
5458
foreach ($results as $i=>$result) {
5559
if ($result instanceof \Throwable) {

src/Tqdev/PhpCrudApi/Record/Document/ErrorDocument.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function serialize()
4343

4444
public function jsonSerialize()
4545
{
46-
return array_filter($this->serialize());
46+
return array_filter($this->serialize(), function($v) {return $v!==null;});
4747
}
4848

4949
public static function fromException(\Throwable $exception)

src/Tqdev/PhpCrudApi/Record/ErrorCode.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class ErrorCode
3535
const PASSWORD_TOO_SHORT = 1021;
3636

3737
private $values = [
38-
9999 => ["%s", ResponseFactory::INTERNAL_SERVER_ERROR],
38+
0000 => ["Success", ResponseFactory::OK],
3939
1000 => ["Route '%s' not found", ResponseFactory::NOT_FOUND],
4040
1001 => ["Table '%s' not found", ResponseFactory::NOT_FOUND],
4141
1002 => ["Argument count mismatch in '%s'", ResponseFactory::UNPROCESSABLE_ENTITY],
@@ -58,6 +58,7 @@ class ErrorCode
5858
1019 => ["Pagination forbidden", ResponseFactory::FORBIDDEN],
5959
1020 => ["User '%s' already exists", ResponseFactory::CONFLICT],
6060
1021 => ["Password too short (<%d characters)", ResponseFactory::UNPROCESSABLE_ENTITY],
61+
9999 => ["%s", ResponseFactory::INTERNAL_SERVER_ERROR],
6162
];
6263

6364
public function __construct(int $code)

tests/functional/001_records/090_add_multiple_comments.log

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ POST /records/comments
2323
===
2424
424
2525
Content-Type: application/json; charset=utf-8
26-
Content-Length: 54
26+
Content-Length: 83
2727

28-
[9,{"code":1010,"message":"Data integrity violation"}]
28+
[{"code":0,"message":"Success"},{"code":1010,"message":"Data integrity violation"}]
2929
===
3030
GET /records/comments?include=id&filter=post_id,eq,6
3131
===

tests/functional/001_records/091_edit_multiple_comments.log

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ PUT /records/comments/7,8
2323
===
2424
424
2525
Content-Type: application/json; charset=utf-8
26-
Content-Length: 54
26+
Content-Length: 83
2727

28-
[1,{"code":1010,"message":"Data integrity violation"}]
28+
[{"code":0,"message":"Success"},{"code":1010,"message":"Data integrity violation"}]
2929
===
3030
GET /records/comments?include=message&filter=post_id,eq,6
3131
===

0 commit comments

Comments
 (0)