Bladeren bron

improvements for #744

Maurits van der Schee 3 jaren geleden
bovenliggende
commit
943a9a51c7

+ 17
- 2
README.md Bestand weergeven

554
 
554
 
555
 This adjusts the titles of the posts. And the return values are the number of rows that are set:
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
 Which means that there were two update operations and each of them had set one row. Batch operations use database
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
 ### Spatial support
577
 ### Spatial support
563
 
578
 

+ 7
- 3
src/Tqdev/PhpCrudApi/Controller/JsonResponder.php Bestand weergeven

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

+ 1
- 1
src/Tqdev/PhpCrudApi/Record/Document/ErrorDocument.php Bestand weergeven

43
 
43
 
44
     public function jsonSerialize()
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
     public static function fromException(\Throwable $exception)
49
     public static function fromException(\Throwable $exception)

+ 2
- 1
src/Tqdev/PhpCrudApi/Record/ErrorCode.php Bestand weergeven

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

+ 2
- 2
tests/functional/001_records/090_add_multiple_comments.log Bestand weergeven

23
 ===
23
 ===
24
 424
24
 424
25
 Content-Type: application/json; charset=utf-8
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
 GET /records/comments?include=id&filter=post_id,eq,6
30
 GET /records/comments?include=id&filter=post_id,eq,6
31
 ===
31
 ===

+ 2
- 2
tests/functional/001_records/091_edit_multiple_comments.log Bestand weergeven

23
 ===
23
 ===
24
 424
24
 424
25
 Content-Type: application/json; charset=utf-8
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
 GET /records/comments?include=message&filter=post_id,eq,6
30
 GET /records/comments?include=message&filter=post_id,eq,6
31
 ===
31
 ===

Loading…
Annuleren
Opslaan