Skip to content

Commit 4cbac93

Browse files
matiasperrone-exosmarcet
authored andcommitted
fix: Reuse already defined schema and add Paginated Schema
1 parent 41031c1 commit 4cbac93

3 files changed

Lines changed: 45 additions & 57 deletions

File tree

app/Http/Controllers/Apis/CountriesApiController.php

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,71 +11,49 @@
1111
* See the License for the specific language governing permissions and
1212
* limitations under the License.
1313
**/
14+
1415
use App\Models\Foundation\Main\CountryCodes;
1516
use Illuminate\Http\Response;
16-
use Illuminate\Support\Facades\Log;
17-
use models\exceptions\EntityNotFoundException;
18-
use models\exceptions\ValidationException;
1917
use OpenApi\Attributes as OA;
2018
use utils\PagingResponse;
21-
use Illuminate\Support\Facades\Request;
2219

2320
/**
2421
* Class CountriesApiController
2522
* @package App\Http\Controllers
2623
*/
2724
final class CountriesApiController extends JsonController
2825
{
26+
use RequestProcessor;
27+
2928
#[OA\Get(
3029
path: "/api/public/v1/countries",
3130
description: "Get all countries with ISO codes",
3231
summary: 'Get all countries',
3332
operationId: 'getAllCountries',
34-
tags: ['Country'],
35-
parameters: [
36-
new OA\Parameter(
37-
name: 'expand',
38-
in: 'query',
39-
required: false,
40-
description: 'Parameter for expanding related entity properties through serialization. Note: Has no effect on this endpoint since countries are returned as simple arrays, not complex entities. Always returns iso_code and name regardless of this parameter.',
41-
schema: new OA\Schema(type: 'string', example: '')
42-
),
43-
],
33+
tags: ['Countries'],
4434
responses: [
4535
new OA\Response(
4636
response: 200,
4737
description: 'Success - Returns paginated list of countries',
48-
content: new OA\JsonContent(
49-
properties: [
50-
'total' => new OA\Property(property: 'total', type: 'integer', example: 195),
51-
'per_page' => new OA\Property(property: 'per_page', type: 'integer', example: 195),
52-
'current_page' => new OA\Property(property: 'current_page', type: 'integer', example: 1),
53-
'last_page' => new OA\Property(property: 'last_page', type: 'integer', example: 1),
54-
'data' => new OA\Property(
55-
property: 'data',
56-
type: 'array',
57-
items: new OA\Items(ref: "#/components/schemas/ISOElementSchema")
58-
)
59-
],
60-
type: 'object'
61-
)
38+
content: new OA\JsonContent(ref: '#/components/schemas/PaginatedISOCountryElementResponseSchema'),
6239
),
6340
new OA\Response(response: Response::HTTP_PRECONDITION_FAILED, description: "Validation Error"),
6441
new OA\Response(response: Response::HTTP_NOT_FOUND, description: "Not Found"),
6542
new OA\Response(response: Response::HTTP_INTERNAL_SERVER_ERROR, description: "Server Error")
6643
]
6744
)]
68-
public function getAll(){
69-
try {
45+
public function getAll()
46+
{
47+
return $this->processRequest(function () {
7048
$countries = [];
71-
foreach(CountryCodes::$iso_3166_countryCodes as $iso_code => $name){
49+
foreach (CountryCodes::$iso_3166_countryCodes as $iso_code => $name) {
7250
$countries[] = [
7351
'iso_code' => $iso_code,
7452
'name' => $name,
7553
];
7654
}
7755

78-
$response = new PagingResponse
56+
$response = new PagingResponse
7957
(
8058
count($countries),
8159
count($countries),
@@ -84,20 +62,8 @@ public function getAll(){
8462
$countries
8563
);
8664

87-
return $this->ok($response->toArray($expand = Request::input('expand','')));
88-
}
89-
catch (ValidationException $ex1) {
90-
Log::warning($ex1);
91-
return $this->error412(array($ex1->getMessage()));
92-
}
93-
catch(EntityNotFoundException $ex2)
94-
{
95-
Log::warning($ex2);
96-
return $this->error404(array('message'=> $ex2->getMessage()));
97-
}
98-
catch (\Exception $ex) {
99-
Log::error($ex);
100-
return $this->error500($ex);
101-
}
65+
return $this->ok($response->toArray());
66+
});
67+
10268
}
10369
}

app/Swagger/Countries.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php namespace App\Swagger\schemas;
2+
3+
use OpenApi\Attributes as OA;
4+
5+
#[OA\Schema(
6+
schema: 'ISOCountryElementSchema',
7+
type: 'object',
8+
properties: [
9+
'iso_code' => new OA\Property(property: 'iso_code', type: 'string', example: 'US'),
10+
'name' => new OA\Property(property: 'name', type: 'string', example: 'United States')
11+
]
12+
)]
13+
class ISOElementSchema {};
14+
15+
#[OA\Schema(
16+
schema: 'PaginatedISOCountryElementResponseSchema',
17+
type: 'object',
18+
allOf: [
19+
new OA\Schema(ref: '#/components/schemas/PaginateDataSchemaResponse'),
20+
new OA\Schema(
21+
type: 'object',
22+
properties: [
23+
new OA\Property(
24+
property: 'data',
25+
type: 'array',
26+
items: new OA\Items(ref: "#/components/schemas/ISOCountryElementSchema")
27+
)
28+
]
29+
)
30+
]
31+
)]
32+
class PaginatedISOElementResponseSchema {};

app/Swagger/schemas.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -350,13 +350,3 @@ class RSVPUpdateRequestSchema_{
350350
]
351351
)]
352352
class RSVPAdminAddRequestSchema {}
353-
354-
#[OA\Schema(
355-
schema: 'ISOElementSchema',
356-
type: 'object',
357-
properties: [
358-
'iso_code' => new OA\Property(property: 'iso_code', type: 'string', example: 'US'),
359-
'name' => new OA\Property(property: 'name', type: 'string', example: 'United States')
360-
]
361-
)]
362-
class ISOElementSchema {};

0 commit comments

Comments
 (0)