Browse Source

add parameters

Maurits van der Schee 6 years ago
parent
commit
229b08990c
3 changed files with 148 additions and 3 deletions
  1. 28
    1
      README.md
  2. 60
    1
      api.php
  3. 60
    1
      src/Tqdev/PhpCrudApi/OpenApi/OpenApiBuilder.php

+ 28
- 1
README.md View File

@@ -408,9 +408,36 @@ Output:
408 408
 
409 409
 NB: You may sort on multiple fields by using multiple "order" parameters. You can not order on "joined" columns.
410 410
 
411
+### Limit size
412
+
413
+The "size" parameter limits the number of returned records. This can be used for top N lists together with the "order" parameter (use descending order).
414
+
415
+```
416
+GET /records/categories?order=id&size=2
417
+```
418
+
419
+Output:
420
+
421
+```
422
+    {
423
+        "records":[
424
+            {
425
+                "id": 1
426
+                "name": "Internet"
427
+            },
428
+            {
429
+                "id": 3
430
+                "name": "Web development"
431
+            }
432
+        ]
433
+    }
434
+```
435
+
436
+NB: If you also want to know to the total number of records you may want to use the "page" parameter.
437
+
411 438
 ### Pagination
412 439
 
413
-The "page" parameter holds the requested page. The default page size is 20, but can be adjusted (e.g. to 50):
440
+The "page" parameter holds the requested page. The default page size is 20, but can be adjusted (e.g. to 50).
414 441
 
415 442
 ```
416 443
 GET /records/categories?order=id&page=1

+ 60
- 1
api.php View File

@@ -3768,11 +3768,22 @@ class OpenApiBuilder
3768 3768
             if (!$this->isOperationOnTableAllowed($operation, $tableName)) {
3769 3769
                 continue;
3770 3770
             }
3771
+            $parameters = [];
3771 3772
             if (in_array($operation, ['list', 'create'])) {
3772 3773
                 $path = sprintf('/records/%s', $tableName);
3774
+                if ($operation == 'list') {
3775
+                    $parameters = ['filter', 'include', 'exclude', 'order', 'size', 'page', 'join'];
3776
+                }
3773 3777
             } else {
3774 3778
                 $path = sprintf('/records/%s/{%s}', $tableName, $pkName);
3775
-                $this->openapi->set("paths|$path|$method|parameters|0|\$ref", "#/components/parameters/pk");
3779
+                if ($operation == 'read') {
3780
+                    $parameters = ['pk', 'include', 'exclude', 'join'];
3781
+                } else {
3782
+                    $parameters = ['pk'];
3783
+                }
3784
+            }
3785
+            foreach ($parameters as $p => $parameter) {
3786
+                $this->openapi->set("paths|$path|$method|parameters|$p|\$ref", "#/components/parameters/$parameter");
3776 3787
             }
3777 3788
             if (in_array($operation, ['create', 'update', 'increment'])) {
3778 3789
                 $this->openapi->set("paths|$path|$method|requestBody|\$ref", "#/components/requestBodies/$operation-" . urlencode($tableName));
@@ -3893,6 +3904,54 @@ class OpenApiBuilder
3893 3904
         $this->openapi->set("components|parameters|pk|schema|type", "string");
3894 3905
         $this->openapi->set("components|parameters|pk|description", "primary key value");
3895 3906
         $this->openapi->set("components|parameters|pk|required", true);
3907
+
3908
+        $this->openapi->set("components|parameters|filter|name", "filter");
3909
+        $this->openapi->set("components|parameters|filter|in", "query");
3910
+        $this->openapi->set("components|parameters|filter|schema|type", "array");
3911
+        $this->openapi->set("components|parameters|filter|schema|items|type", "string");
3912
+        $this->openapi->set("components|parameters|filter|description", "Filters to be applied. Each filter consists of a column, an operator and a value (comma separated). Example: id,eq,1");
3913
+        $this->openapi->set("components|parameters|filter|required", false);
3914
+        $this->openapi->set("components|parameters|filter|explode", false);
3915
+        
3916
+        $this->openapi->set("components|parameters|include|name", "include");
3917
+        $this->openapi->set("components|parameters|include|in", "query");
3918
+        $this->openapi->set("components|parameters|include|schema|type", "string");
3919
+        $this->openapi->set("components|parameters|include|description", "Columns you want to include in the output (comma separated). Example: posts.*,categories.name");
3920
+        $this->openapi->set("components|parameters|include|required", false);
3921
+        
3922
+        $this->openapi->set("components|parameters|exclude|name", "exclude");
3923
+        $this->openapi->set("components|parameters|exclude|in", "query");
3924
+        $this->openapi->set("components|parameters|exclude|schema|type", "string");
3925
+        $this->openapi->set("components|parameters|exclude|description", "Columns you want to exclude from the output (comma separated). Example: posts.content");
3926
+        $this->openapi->set("components|parameters|exclude|required", false);
3927
+        
3928
+        $this->openapi->set("components|parameters|order|name", "order");
3929
+        $this->openapi->set("components|parameters|order|in", "query");
3930
+        $this->openapi->set("components|parameters|order|schema|type", "array");
3931
+        $this->openapi->set("components|parameters|order|schema|items|type", "string");
3932
+        $this->openapi->set("components|parameters|order|description", "Column you want to sort on and the sort direction (comma separated). Example: id,desc");
3933
+        $this->openapi->set("components|parameters|order|required", false);
3934
+        $this->openapi->set("components|parameters|order|explode", false);
3935
+
3936
+        $this->openapi->set("components|parameters|size|name", "size");
3937
+        $this->openapi->set("components|parameters|size|in", "query");
3938
+        $this->openapi->set("components|parameters|size|schema|type", "string");
3939
+        $this->openapi->set("components|parameters|size|description", "Maximum number of results (for top lists). Example: 10");
3940
+        $this->openapi->set("components|parameters|size|required", false);
3941
+
3942
+        $this->openapi->set("components|parameters|page|name", "page");
3943
+        $this->openapi->set("components|parameters|page|in", "query");
3944
+        $this->openapi->set("components|parameters|page|schema|type", "string");
3945
+        $this->openapi->set("components|parameters|page|description", "Page number and page size (comma separated). Example: 1,10");
3946
+        $this->openapi->set("components|parameters|page|required", false);
3947
+
3948
+        $this->openapi->set("components|parameters|join|name", "join");
3949
+        $this->openapi->set("components|parameters|join|in", "query");
3950
+        $this->openapi->set("components|parameters|join|schema|type", "array");
3951
+        $this->openapi->set("components|parameters|join|schema|items|type", "string");
3952
+        $this->openapi->set("components|parameters|join|description", "Paths (comma separated) to related entities that you want to include. Example: comments,users");
3953
+        $this->openapi->set("components|parameters|join|required", false);
3954
+        $this->openapi->set("components|parameters|join|explode", false);
3896 3955
     }
3897 3956
 
3898 3957
     private function setTag(int $index, String $tableName) /*: void*/

+ 60
- 1
src/Tqdev/PhpCrudApi/OpenApi/OpenApiBuilder.php View File

@@ -115,11 +115,22 @@ class OpenApiBuilder
115 115
             if (!$this->isOperationOnTableAllowed($operation, $tableName)) {
116 116
                 continue;
117 117
             }
118
+            $parameters = [];
118 119
             if (in_array($operation, ['list', 'create'])) {
119 120
                 $path = sprintf('/records/%s', $tableName);
121
+                if ($operation == 'list') {
122
+                    $parameters = ['filter', 'include', 'exclude', 'order', 'size', 'page', 'join'];
123
+                }
120 124
             } else {
121 125
                 $path = sprintf('/records/%s/{%s}', $tableName, $pkName);
122
-                $this->openapi->set("paths|$path|$method|parameters|0|\$ref", "#/components/parameters/pk");
126
+                if ($operation == 'read') {
127
+                    $parameters = ['pk', 'include', 'exclude', 'join'];
128
+                } else {
129
+                    $parameters = ['pk'];
130
+                }
131
+            }
132
+            foreach ($parameters as $p => $parameter) {
133
+                $this->openapi->set("paths|$path|$method|parameters|$p|\$ref", "#/components/parameters/$parameter");
123 134
             }
124 135
             if (in_array($operation, ['create', 'update', 'increment'])) {
125 136
                 $this->openapi->set("paths|$path|$method|requestBody|\$ref", "#/components/requestBodies/$operation-" . urlencode($tableName));
@@ -240,6 +251,54 @@ class OpenApiBuilder
240 251
         $this->openapi->set("components|parameters|pk|schema|type", "string");
241 252
         $this->openapi->set("components|parameters|pk|description", "primary key value");
242 253
         $this->openapi->set("components|parameters|pk|required", true);
254
+
255
+        $this->openapi->set("components|parameters|filter|name", "filter");
256
+        $this->openapi->set("components|parameters|filter|in", "query");
257
+        $this->openapi->set("components|parameters|filter|schema|type", "array");
258
+        $this->openapi->set("components|parameters|filter|schema|items|type", "string");
259
+        $this->openapi->set("components|parameters|filter|description", "Filters to be applied. Each filter consists of a column, an operator and a value (comma separated). Example: id,eq,1");
260
+        $this->openapi->set("components|parameters|filter|required", false);
261
+        $this->openapi->set("components|parameters|filter|explode", false);
262
+        
263
+        $this->openapi->set("components|parameters|include|name", "include");
264
+        $this->openapi->set("components|parameters|include|in", "query");
265
+        $this->openapi->set("components|parameters|include|schema|type", "string");
266
+        $this->openapi->set("components|parameters|include|description", "Columns you want to include in the output (comma separated). Example: posts.*,categories.name");
267
+        $this->openapi->set("components|parameters|include|required", false);
268
+        
269
+        $this->openapi->set("components|parameters|exclude|name", "exclude");
270
+        $this->openapi->set("components|parameters|exclude|in", "query");
271
+        $this->openapi->set("components|parameters|exclude|schema|type", "string");
272
+        $this->openapi->set("components|parameters|exclude|description", "Columns you want to exclude from the output (comma separated). Example: posts.content");
273
+        $this->openapi->set("components|parameters|exclude|required", false);
274
+        
275
+        $this->openapi->set("components|parameters|order|name", "order");
276
+        $this->openapi->set("components|parameters|order|in", "query");
277
+        $this->openapi->set("components|parameters|order|schema|type", "array");
278
+        $this->openapi->set("components|parameters|order|schema|items|type", "string");
279
+        $this->openapi->set("components|parameters|order|description", "Column you want to sort on and the sort direction (comma separated). Example: id,desc");
280
+        $this->openapi->set("components|parameters|order|required", false);
281
+        $this->openapi->set("components|parameters|order|explode", false);
282
+
283
+        $this->openapi->set("components|parameters|size|name", "size");
284
+        $this->openapi->set("components|parameters|size|in", "query");
285
+        $this->openapi->set("components|parameters|size|schema|type", "string");
286
+        $this->openapi->set("components|parameters|size|description", "Maximum number of results (for top lists). Example: 10");
287
+        $this->openapi->set("components|parameters|size|required", false);
288
+
289
+        $this->openapi->set("components|parameters|page|name", "page");
290
+        $this->openapi->set("components|parameters|page|in", "query");
291
+        $this->openapi->set("components|parameters|page|schema|type", "string");
292
+        $this->openapi->set("components|parameters|page|description", "Page number and page size (comma separated). Example: 1,10");
293
+        $this->openapi->set("components|parameters|page|required", false);
294
+
295
+        $this->openapi->set("components|parameters|join|name", "join");
296
+        $this->openapi->set("components|parameters|join|in", "query");
297
+        $this->openapi->set("components|parameters|join|schema|type", "array");
298
+        $this->openapi->set("components|parameters|join|schema|items|type", "string");
299
+        $this->openapi->set("components|parameters|join|description", "Paths (comma separated) to related entities that you want to include. Example: comments,users");
300
+        $this->openapi->set("components|parameters|join|required", false);
301
+        $this->openapi->set("components|parameters|join|explode", false);
243 302
     }
244 303
 
245 304
     private function setTag(int $index, String $tableName) /*: void*/

Loading…
Cancel
Save