Skip to content

Commit 229b089

Browse files
committed
add parameters
1 parent 8df6b12 commit 229b089

File tree

3 files changed

+148
-3
lines changed

3 files changed

+148
-3
lines changed

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,36 @@ Output:
408408

409409
NB: You may sort on multiple fields by using multiple "order" parameters. You can not order on "joined" columns.
410410

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+
411438
### Pagination
412439

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).
414441

415442
```
416443
GET /records/categories?order=id&page=1

api.php

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3768,11 +3768,22 @@ private function setPath(String $tableName) /*: void*/
37683768
if (!$this->isOperationOnTableAllowed($operation, $tableName)) {
37693769
continue;
37703770
}
3771+
$parameters = [];
37713772
if (in_array($operation, ['list', 'create'])) {
37723773
$path = sprintf('/records/%s', $tableName);
3774+
if ($operation == 'list') {
3775+
$parameters = ['filter', 'include', 'exclude', 'order', 'size', 'page', 'join'];
3776+
}
37733777
} else {
37743778
$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");
37763787
}
37773788
if (in_array($operation, ['create', 'update', 'increment'])) {
37783789
$this->openapi->set("paths|$path|$method|requestBody|\$ref", "#/components/requestBodies/$operation-" . urlencode($tableName));
@@ -3893,6 +3904,54 @@ private function setComponentParameters() /*: void*/
38933904
$this->openapi->set("components|parameters|pk|schema|type", "string");
38943905
$this->openapi->set("components|parameters|pk|description", "primary key value");
38953906
$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);
38963955
}
38973956

38983957
private function setTag(int $index, String $tableName) /*: void*/

src/Tqdev/PhpCrudApi/OpenApi/OpenApiBuilder.php

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,22 @@ private function setPath(String $tableName) /*: void*/
115115
if (!$this->isOperationOnTableAllowed($operation, $tableName)) {
116116
continue;
117117
}
118+
$parameters = [];
118119
if (in_array($operation, ['list', 'create'])) {
119120
$path = sprintf('/records/%s', $tableName);
121+
if ($operation == 'list') {
122+
$parameters = ['filter', 'include', 'exclude', 'order', 'size', 'page', 'join'];
123+
}
120124
} else {
121125
$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");
123134
}
124135
if (in_array($operation, ['create', 'update', 'increment'])) {
125136
$this->openapi->set("paths|$path|$method|requestBody|\$ref", "#/components/requestBodies/$operation-" . urlencode($tableName));
@@ -240,6 +251,54 @@ private function setComponentParameters() /*: void*/
240251
$this->openapi->set("components|parameters|pk|schema|type", "string");
241252
$this->openapi->set("components|parameters|pk|description", "primary key value");
242253
$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);
243302
}
244303

245304
private function setTag(int $index, String $tableName) /*: void*/

0 commit comments

Comments
 (0)