Browse Source

improvements for #744

Maurits van der Schee 3 years ago
parent
commit
943a9a51c7

+ 17
- 2
README.md View File

@@ -554,10 +554,25 @@ contain the same number of objects as there are primary keys in the URL:
554 554
 
555 555
 This adjusts the titles of the posts. And the return values are the number of rows that are set:
556 556
 
557
-    1,1
557
+    [1,1]
558 558
 
559 559
 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.
561 576
 
562 577
 ### Spatial support
563 578
 

+ 7
- 3
src/Tqdev/PhpCrudApi/Controller/JsonResponder.php View File

@@ -39,17 +39,21 @@ class JsonResponder implements Responder
39 39
 
40 40
     public function multi($results): ResponseInterface
41 41
     {
42
-        $document = array();
42
+        $documents = array();
43
+        $errors = array();
43 44
         $success = true;
44 45
         foreach ($results as $i=>$result) {
45 46
             if ($result instanceof \Throwable) {
46
-                $document[$i] = ErrorDocument::fromException($result);
47
+                $documents[$i] = null;
48
+                $errors[$i] = ErrorDocument::fromException($result);
47 49
                 $success = false;
48 50
             } else {
49
-                $document[$i] = $result;
51
+                $documents[$i] = $result;
52
+                $errors[$i] = new ErrorDocument(new ErrorCode(0),'',null);
50 53
             }
51 54
         }
52 55
         $status = $success ? ResponseFactory::OK : ResponseFactory::FAILED_DEPENDENCY;
56
+        $document = $success ? $documents : $errors;
53 57
         $response = ResponseFactory::fromObject($status, $document);
54 58
         foreach ($results as $i=>$result) {
55 59
             if ($result instanceof \Throwable) {

+ 1
- 1
src/Tqdev/PhpCrudApi/Record/Document/ErrorDocument.php View File

@@ -43,7 +43,7 @@ class ErrorDocument implements \JsonSerializable
43 43
 
44 44
     public function jsonSerialize()
45 45
     {
46
-        return array_filter($this->serialize());
46
+        return array_filter($this->serialize(), function($v) {return $v!==null;});
47 47
     }
48 48
 
49 49
     public static function fromException(\Throwable $exception)

+ 2
- 1
src/Tqdev/PhpCrudApi/Record/ErrorCode.php View File

@@ -35,7 +35,7 @@ class ErrorCode
35 35
     const PASSWORD_TOO_SHORT = 1021;
36 36
 
37 37
     private $values = [
38
-        9999 => ["%s", ResponseFactory::INTERNAL_SERVER_ERROR],
38
+        0000 => ["Success", ResponseFactory::OK],
39 39
         1000 => ["Route '%s' not found", ResponseFactory::NOT_FOUND],
40 40
         1001 => ["Table '%s' not found", ResponseFactory::NOT_FOUND],
41 41
         1002 => ["Argument count mismatch in '%s'", ResponseFactory::UNPROCESSABLE_ENTITY],
@@ -58,6 +58,7 @@ class ErrorCode
58 58
         1019 => ["Pagination forbidden", ResponseFactory::FORBIDDEN],
59 59
         1020 => ["User '%s' already exists", ResponseFactory::CONFLICT],
60 60
         1021 => ["Password too short (<%d characters)", ResponseFactory::UNPROCESSABLE_ENTITY],
61
+        9999 => ["%s", ResponseFactory::INTERNAL_SERVER_ERROR],
61 62
     ];
62 63
 
63 64
     public function __construct(int $code)

+ 2
- 2
tests/functional/001_records/090_add_multiple_comments.log View File

@@ -23,9 +23,9 @@ POST /records/comments
23 23
 ===
24 24
 424
25 25
 Content-Type: application/json; charset=utf-8
26
-Content-Length: 54
26
+Content-Length: 83
27 27
 
28
-[9,{"code":1010,"message":"Data integrity violation"}]
28
+[{"code":0,"message":"Success"},{"code":1010,"message":"Data integrity violation"}]
29 29
 ===
30 30
 GET /records/comments?include=id&filter=post_id,eq,6
31 31
 ===

+ 2
- 2
tests/functional/001_records/091_edit_multiple_comments.log View File

@@ -23,9 +23,9 @@ PUT /records/comments/7,8
23 23
 ===
24 24
 424
25 25
 Content-Type: application/json; charset=utf-8
26
-Content-Length: 54
26
+Content-Length: 83
27 27
 
28
-[1,{"code":1010,"message":"Data integrity violation"}]
28
+[{"code":0,"message":"Success"},{"code":1010,"message":"Data integrity violation"}]
29 29
 ===
30 30
 GET /records/comments?include=message&filter=post_id,eq,6
31 31
 ===

Loading…
Cancel
Save