Browse Source

Improve responses

Maurits van der Schee 6 years ago
parent
commit
b5fda866de
2 changed files with 118 additions and 14 deletions
  1. 59
    7
      api.php
  2. 59
    7
      src/Tqdev/PhpCrudApi/OpenApi/OpenApiBuilder.php

+ 59
- 7
api.php View File

3356
         foreach ($tableNames as $tableName) {
3356
         foreach ($tableNames as $tableName) {
3357
             $this->setPath($tableName);
3357
             $this->setPath($tableName);
3358
         }
3358
         }
3359
+        $this->openapi->set("components|responses|pk_integer|description", "inserted primary key value (integer)");
3360
+        $this->openapi->set("components|responses|pk_integer|content|application/json|schema|type", "integer");
3361
+        $this->openapi->set("components|responses|pk_integer|content|application/json|schema|format", "int64");
3362
+        $this->openapi->set("components|responses|pk_string|description", "inserted primary key value (string)");
3363
+        $this->openapi->set("components|responses|pk_string|content|application/json|schema|type", "string");
3364
+        $this->openapi->set("components|responses|pk_string|content|application/json|schema|format", "uuid");
3365
+        $this->openapi->set("components|responses|rows_affected|description", "number of rows affected (integer)");
3366
+        $this->openapi->set("components|responses|rows_affected|content|application/json|schema|type", "integer");
3367
+        $this->openapi->set("components|responses|rows_affected|content|application/json|schema|format", "int64");
3359
         foreach ($tableNames as $tableName) {
3368
         foreach ($tableNames as $tableName) {
3360
             $this->setComponentSchema($tableName);
3369
             $this->setComponentSchema($tableName);
3370
+            $this->setComponentResponse($tableName);
3371
+            $this->setComponentRequestBody($tableName);
3361
         }
3372
         }
3362
         $this->setComponentParameters();
3373
         $this->setComponentParameters();
3363
         foreach ($tableNames as $index => $tableName) {
3374
         foreach ($tableNames as $index => $tableName) {
3372
         $pk = $table->getPk();
3383
         $pk = $table->getPk();
3373
         $pkName = $pk ? $pk->getName() : '';
3384
         $pkName = $pk ? $pk->getName() : '';
3374
         foreach ($this->operations as $operation => $method) {
3385
         foreach ($this->operations as $operation => $method) {
3386
+            if (!$pkName && $operation != 'list') {
3387
+                continue;
3388
+            }
3375
             if (in_array($operation, ['list', 'create'])) {
3389
             if (in_array($operation, ['list', 'create'])) {
3376
                 $path = sprintf('/records/%s', $tableName);
3390
                 $path = sprintf('/records/%s', $tableName);
3377
             } else {
3391
             } else {
3378
-                if (!$pkName) {
3379
-                    continue;
3380
-                }
3381
                 $path = sprintf('/records/%s/{%s}', $tableName, $pkName);
3392
                 $path = sprintf('/records/%s/{%s}', $tableName, $pkName);
3382
                 $this->openapi->set("paths|$path|$method|parameters|0|\$ref", "#/components/parameters/pk");
3393
                 $this->openapi->set("paths|$path|$method|parameters|0|\$ref", "#/components/parameters/pk");
3383
             }
3394
             }
3395
+            if (in_array($operation, ['create', 'update'])) {
3396
+                $this->openapi->set("paths|$path|$method|requestBody|\$ref", "#/components/requestBodies/single_" . urlencode($tableName));
3397
+            }
3384
             $this->openapi->set("paths|$path|$method|tags|0", "$tableName");
3398
             $this->openapi->set("paths|$path|$method|tags|0", "$tableName");
3385
             $this->openapi->set("paths|$path|$method|description", "$operation $tableName");
3399
             $this->openapi->set("paths|$path|$method|description", "$operation $tableName");
3386
-            $this->openapi->set("paths|$path|$method|responses|200|description", "$operation $tableName succeeded");
3400
+            switch ($operation) {
3401
+                case 'list':
3402
+                    $this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/list_of_" . urlencode($tableName));
3403
+                    break;
3404
+                case 'create':
3405
+                    if ($pk->getType() == 'integer') {
3406
+                        $this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/pk_integer");
3407
+                    } else {
3408
+                        $this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/pk_string");
3409
+                    }
3410
+                    break;
3411
+                case 'read':
3412
+                    $this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/single_" . urlencode($tableName));
3413
+                    break;
3414
+                case 'update':
3415
+                case 'delete':
3416
+                case 'increment':
3417
+                    $this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/rows_affected");
3418
+                    break;
3419
+            }
3420
+
3387
         }
3421
         }
3388
     }
3422
     }
3389
 
3423
 
3390
     private function setComponentSchema(String $tableName) /*: void*/
3424
     private function setComponentSchema(String $tableName) /*: void*/
3391
     {
3425
     {
3392
-        $this->openapi->set("components|schemas|$tableName|type", "object");
3426
+        $this->openapi->set("components|schemas|single_$tableName|type", "object");
3393
         $table = $this->reflection->getTable($tableName);
3427
         $table = $this->reflection->getTable($tableName);
3394
         foreach ($table->columnNames() as $columnName) {
3428
         foreach ($table->columnNames() as $columnName) {
3395
             $column = $table->get($columnName);
3429
             $column = $table->get($columnName);
3396
             $properties = $this->types[$column->getType()];
3430
             $properties = $this->types[$column->getType()];
3397
             foreach ($properties as $key => $value) {
3431
             foreach ($properties as $key => $value) {
3398
-                $this->openapi->set("components|schemas|$tableName|properties|$columnName|$key", $value);
3432
+                $this->openapi->set("components|schemas|single_$tableName|properties|$columnName|$key", $value);
3399
             }
3433
             }
3400
-
3401
         }
3434
         }
3435
+        $this->openapi->set("components|schemas|list_of_$tableName|type", "object");
3436
+        $this->openapi->set("components|schemas|list_of_$tableName|properties|count|type", "integer");
3437
+        $this->openapi->set("components|schemas|list_of_$tableName|properties|count|format", "int64");
3438
+        $this->openapi->set("components|schemas|list_of_$tableName|properties|records|type", "array");
3439
+        $this->openapi->set("components|schemas|list_of_$tableName|properties|records|items|\$ref", "#/components/schemas/single_" . urlencode($tableName));
3440
+    }
3441
+
3442
+    private function setComponentResponse(String $tableName) /*: void*/
3443
+    {
3444
+        $this->openapi->set("components|responses|single_$tableName|description", "single $tableName record");
3445
+        $this->openapi->set("components|responses|single_$tableName|content|application/json|schema|\$ref", "#/components/schemas/single_" . urlencode($tableName));
3446
+        $this->openapi->set("components|responses|list_of_$tableName|description", "list of $tableName records");
3447
+        $this->openapi->set("components|responses|list_of_$tableName|content|application/json|schema|\$ref", "#/components/schemas/list_of_" . urlencode($tableName));
3448
+    }
3449
+
3450
+    private function setComponentRequestBody(String $tableName) /*: void*/
3451
+    {
3452
+        $this->openapi->set("components|requestBodies|single_$tableName|description", "single $tableName record");
3453
+        $this->openapi->set("components|requestBodies|single_$tableName|content|application/json|schema|\$ref", "#/components/schemas/single_" . urlencode($tableName));
3402
     }
3454
     }
3403
 
3455
 
3404
     private function setComponentParameters() /*: void*/
3456
     private function setComponentParameters() /*: void*/

+ 59
- 7
src/Tqdev/PhpCrudApi/OpenApi/OpenApiBuilder.php View File

45
         foreach ($tableNames as $tableName) {
45
         foreach ($tableNames as $tableName) {
46
             $this->setPath($tableName);
46
             $this->setPath($tableName);
47
         }
47
         }
48
+        $this->openapi->set("components|responses|pk_integer|description", "inserted primary key value (integer)");
49
+        $this->openapi->set("components|responses|pk_integer|content|application/json|schema|type", "integer");
50
+        $this->openapi->set("components|responses|pk_integer|content|application/json|schema|format", "int64");
51
+        $this->openapi->set("components|responses|pk_string|description", "inserted primary key value (string)");
52
+        $this->openapi->set("components|responses|pk_string|content|application/json|schema|type", "string");
53
+        $this->openapi->set("components|responses|pk_string|content|application/json|schema|format", "uuid");
54
+        $this->openapi->set("components|responses|rows_affected|description", "number of rows affected (integer)");
55
+        $this->openapi->set("components|responses|rows_affected|content|application/json|schema|type", "integer");
56
+        $this->openapi->set("components|responses|rows_affected|content|application/json|schema|format", "int64");
48
         foreach ($tableNames as $tableName) {
57
         foreach ($tableNames as $tableName) {
49
             $this->setComponentSchema($tableName);
58
             $this->setComponentSchema($tableName);
59
+            $this->setComponentResponse($tableName);
60
+            $this->setComponentRequestBody($tableName);
50
         }
61
         }
51
         $this->setComponentParameters();
62
         $this->setComponentParameters();
52
         foreach ($tableNames as $index => $tableName) {
63
         foreach ($tableNames as $index => $tableName) {
61
         $pk = $table->getPk();
72
         $pk = $table->getPk();
62
         $pkName = $pk ? $pk->getName() : '';
73
         $pkName = $pk ? $pk->getName() : '';
63
         foreach ($this->operations as $operation => $method) {
74
         foreach ($this->operations as $operation => $method) {
75
+            if (!$pkName && $operation != 'list') {
76
+                continue;
77
+            }
64
             if (in_array($operation, ['list', 'create'])) {
78
             if (in_array($operation, ['list', 'create'])) {
65
                 $path = sprintf('/records/%s', $tableName);
79
                 $path = sprintf('/records/%s', $tableName);
66
             } else {
80
             } else {
67
-                if (!$pkName) {
68
-                    continue;
69
-                }
70
                 $path = sprintf('/records/%s/{%s}', $tableName, $pkName);
81
                 $path = sprintf('/records/%s/{%s}', $tableName, $pkName);
71
                 $this->openapi->set("paths|$path|$method|parameters|0|\$ref", "#/components/parameters/pk");
82
                 $this->openapi->set("paths|$path|$method|parameters|0|\$ref", "#/components/parameters/pk");
72
             }
83
             }
84
+            if (in_array($operation, ['create', 'update'])) {
85
+                $this->openapi->set("paths|$path|$method|requestBody|\$ref", "#/components/requestBodies/single_" . urlencode($tableName));
86
+            }
73
             $this->openapi->set("paths|$path|$method|tags|0", "$tableName");
87
             $this->openapi->set("paths|$path|$method|tags|0", "$tableName");
74
             $this->openapi->set("paths|$path|$method|description", "$operation $tableName");
88
             $this->openapi->set("paths|$path|$method|description", "$operation $tableName");
75
-            $this->openapi->set("paths|$path|$method|responses|200|description", "$operation $tableName succeeded");
89
+            switch ($operation) {
90
+                case 'list':
91
+                    $this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/list_of_" . urlencode($tableName));
92
+                    break;
93
+                case 'create':
94
+                    if ($pk->getType() == 'integer') {
95
+                        $this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/pk_integer");
96
+                    } else {
97
+                        $this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/pk_string");
98
+                    }
99
+                    break;
100
+                case 'read':
101
+                    $this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/single_" . urlencode($tableName));
102
+                    break;
103
+                case 'update':
104
+                case 'delete':
105
+                case 'increment':
106
+                    $this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/rows_affected");
107
+                    break;
108
+            }
109
+
76
         }
110
         }
77
     }
111
     }
78
 
112
 
79
     private function setComponentSchema(String $tableName) /*: void*/
113
     private function setComponentSchema(String $tableName) /*: void*/
80
     {
114
     {
81
-        $this->openapi->set("components|schemas|$tableName|type", "object");
115
+        $this->openapi->set("components|schemas|single_$tableName|type", "object");
82
         $table = $this->reflection->getTable($tableName);
116
         $table = $this->reflection->getTable($tableName);
83
         foreach ($table->columnNames() as $columnName) {
117
         foreach ($table->columnNames() as $columnName) {
84
             $column = $table->get($columnName);
118
             $column = $table->get($columnName);
85
             $properties = $this->types[$column->getType()];
119
             $properties = $this->types[$column->getType()];
86
             foreach ($properties as $key => $value) {
120
             foreach ($properties as $key => $value) {
87
-                $this->openapi->set("components|schemas|$tableName|properties|$columnName|$key", $value);
121
+                $this->openapi->set("components|schemas|single_$tableName|properties|$columnName|$key", $value);
88
             }
122
             }
89
-
90
         }
123
         }
124
+        $this->openapi->set("components|schemas|list_of_$tableName|type", "object");
125
+        $this->openapi->set("components|schemas|list_of_$tableName|properties|count|type", "integer");
126
+        $this->openapi->set("components|schemas|list_of_$tableName|properties|count|format", "int64");
127
+        $this->openapi->set("components|schemas|list_of_$tableName|properties|records|type", "array");
128
+        $this->openapi->set("components|schemas|list_of_$tableName|properties|records|items|\$ref", "#/components/schemas/single_" . urlencode($tableName));
129
+    }
130
+
131
+    private function setComponentResponse(String $tableName) /*: void*/
132
+    {
133
+        $this->openapi->set("components|responses|single_$tableName|description", "single $tableName record");
134
+        $this->openapi->set("components|responses|single_$tableName|content|application/json|schema|\$ref", "#/components/schemas/single_" . urlencode($tableName));
135
+        $this->openapi->set("components|responses|list_of_$tableName|description", "list of $tableName records");
136
+        $this->openapi->set("components|responses|list_of_$tableName|content|application/json|schema|\$ref", "#/components/schemas/list_of_" . urlencode($tableName));
137
+    }
138
+
139
+    private function setComponentRequestBody(String $tableName) /*: void*/
140
+    {
141
+        $this->openapi->set("components|requestBodies|single_$tableName|description", "single $tableName record");
142
+        $this->openapi->set("components|requestBodies|single_$tableName|content|application/json|schema|\$ref", "#/components/schemas/single_" . urlencode($tableName));
91
     }
143
     }
92
 
144
 
93
     private function setComponentParameters() /*: void*/
145
     private function setComponentParameters() /*: void*/

Loading…
Cancel
Save