6
6
7
7
use OpenApi \Attributes as OA ;
8
8
use PhpList \Core \Domain \Model \Identity \Administrator ;
9
- use PhpList \RestBundle \ Controller \ Traits \ AuthenticationTrait ;
9
+ use PhpList \Core \ Security \ Authentication ;
10
10
use PhpList \RestBundle \Entity \Request \CreateAdministratorRequest ;
11
11
use PhpList \RestBundle \Entity \Request \UpdateAdministratorRequest ;
12
12
use PhpList \RestBundle \Serializer \AdministratorNormalizer ;
13
13
use PhpList \RestBundle \Service \Manager \AdministratorManager ;
14
+ use PhpList \RestBundle \Service \Provider \AdministratorProvider ;
14
15
use PhpList \RestBundle \Validator \RequestValidator ;
15
16
use Symfony \Bridge \Doctrine \Attribute \MapEntity ;
16
- use Symfony \Bundle \FrameworkBundle \Controller \AbstractController ;
17
17
use Symfony \Component \HttpFoundation \JsonResponse ;
18
18
use Symfony \Component \HttpFoundation \Request ;
19
19
use Symfony \Component \HttpFoundation \Response ;
20
+ use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
20
21
use Symfony \Component \Routing \Attribute \Route ;
21
22
22
23
/**
23
24
* This controller provides CRUD operations for Administrator entities.
24
25
*/
25
26
#[Route('/administrators ' )]
26
- class AdministratorController extends AbstractController
27
+ class AdministratorController extends BaseController
27
28
{
28
- use AuthenticationTrait;
29
-
30
29
private AdministratorManager $ administratorManager ;
30
+ private AdministratorNormalizer $ normalizer ;
31
+ private AdministratorProvider $ administratorProvider ;
31
32
32
- public function __construct (AdministratorManager $ administratorManager )
33
- {
33
+ public function __construct (
34
+ Authentication $ authentication ,
35
+ AdministratorManager $ administratorManager ,
36
+ AdministratorNormalizer $ normalizer ,
37
+ RequestValidator $ validator ,
38
+ AdministratorProvider $ administratorProvider
39
+ ) {
40
+ parent ::__construct ($ authentication , $ validator );
34
41
$ this ->administratorManager = $ administratorManager ;
42
+ $ this ->normalizer = $ normalizer ;
43
+ $ this ->administratorProvider = $ administratorProvider ;
44
+ }
45
+
46
+ #[Route('' , name: 'get_administrators ' , methods: ['GET ' ])]
47
+ #[OA \Post(
48
+ path: '/administrators ' ,
49
+ description: 'Get list of administrators. ' ,
50
+ summary: 'Get Administrators ' ,
51
+ tags: ['administrators ' ],
52
+ parameters: [
53
+ new OA \Parameter (
54
+ name: 'session ' ,
55
+ description: 'Session ID obtained from authentication ' ,
56
+ in: 'header ' ,
57
+ required: true ,
58
+ schema: new OA \Schema (
59
+ type: 'string '
60
+ )
61
+ ),
62
+ new OA \Parameter (
63
+ name: 'after_id ' ,
64
+ description: 'Last id (starting from 0) ' ,
65
+ in: 'query ' ,
66
+ required: false ,
67
+ schema: new OA \Schema (type: 'integer ' , default: 1 , minimum: 1 )
68
+ ),
69
+ new OA \Parameter (
70
+ name: 'limit ' ,
71
+ description: 'Number of results per page ' ,
72
+ in: 'query ' ,
73
+ required: false ,
74
+ schema: new OA \Schema (type: 'integer ' , default: 25 , maximum: 100 , minimum: 1 )
75
+ )
76
+ ],
77
+ responses: [
78
+ new OA \Response (
79
+ response: 200 ,
80
+ description: 'Success ' ,
81
+ content: new OA \JsonContent (
82
+ properties: [
83
+ new OA \Property (
84
+ property: 'items ' ,
85
+ type: 'array ' ,
86
+ items: new OA \Items (ref: '#/components/schemas/Administrator ' )
87
+ ),
88
+ new OA \Property (property: 'pagination ' , ref: '#/components/schemas/CursorPagination ' )
89
+ ],
90
+ type: 'object '
91
+ )
92
+ ),
93
+ new OA \Response (
94
+ response: 400 ,
95
+ description: 'Invalid input '
96
+ )
97
+ ]
98
+ )]
99
+ public function getAdministrators (Request $ request ): JsonResponse
100
+ {
101
+ $ this ->requireAuthentication ($ request );
102
+
103
+ return new JsonResponse (
104
+ $ this ->administratorProvider ->getPaginatedList ($ request ),
105
+ Response::HTTP_OK
106
+ );
35
107
}
36
108
37
109
#[Route('' , name: 'create_administrator ' , methods: ['POST ' ])]
@@ -49,7 +121,7 @@ public function __construct(AdministratorManager $administratorManager)
49
121
new OA \Response (
50
122
response: 201 ,
51
123
description: 'Administrator created successfully ' ,
52
- content: new OA \JsonContent (ref: '#/components/schemas/CreateAdministratorRequest ' )
124
+ content: new OA \JsonContent (ref: '#/components/schemas/Administrator ' )
53
125
),
54
126
new OA \Response (
55
127
response: 400 ,
@@ -62,11 +134,11 @@ public function createAdministrator(
62
134
RequestValidator $ validator ,
63
135
AdministratorNormalizer $ normalizer
64
136
): JsonResponse {
137
+ $ this ->requireAuthentication ($ request );
138
+
65
139
/** @var CreateAdministratorRequest $dto */
66
140
$ dto = $ validator ->validate ($ request , CreateAdministratorRequest::class);
67
-
68
141
$ administrator = $ this ->administratorManager ->createAdministrator ($ dto );
69
-
70
142
$ json = $ normalizer ->normalize ($ administrator , 'json ' );
71
143
72
144
return new JsonResponse ($ json , Response::HTTP_CREATED );
@@ -100,16 +172,17 @@ public function createAdministrator(
100
172
]
101
173
)]
102
174
public function getAdministrator (
175
+ Request $ request ,
103
176
#[MapEntity(mapping: ['administratorId ' => 'id ' ])] ?Administrator $ administrator ,
104
- AdministratorNormalizer $ normalizer
105
177
): JsonResponse {
178
+ $ this ->requireAuthentication ($ request );
179
+
106
180
if (!$ administrator ) {
107
- return new JsonResponse ([ ' message ' => ' Administrator not found. '], Response:: HTTP_NOT_FOUND );
181
+ throw new NotFoundHttpException ( ' Administrator not found. ' );
108
182
}
183
+ $ json = $ this ->normalizer ->normalize ($ administrator , 'json ' );
109
184
110
- $ json = $ normalizer ->normalize ($ administrator , 'json ' );
111
-
112
- return new JsonResponse ($ json );
185
+ return new JsonResponse ($ json , Response::HTTP_OK );
113
186
}
114
187
115
188
#[Route('/{administratorId} ' , name: 'update_administrator ' , methods: ['PUT ' ])]
@@ -146,15 +219,14 @@ public function getAdministrator(
146
219
public function updateAdministrator (
147
220
Request $ request ,
148
221
#[MapEntity(mapping: ['administratorId ' => 'id ' ])] ?Administrator $ administrator ,
149
- RequestValidator $ validator
150
222
): JsonResponse {
223
+ $ this ->requireAuthentication ($ request );
224
+
151
225
if (!$ administrator ) {
152
- return new JsonResponse ([ ' message ' => ' Administrator not found. '], Response:: HTTP_NOT_FOUND );
226
+ throw new NotFoundHttpException ( ' Administrator not found. ' );
153
227
}
154
-
155
228
/** @var UpdateAdministratorRequest $dto */
156
- $ dto = $ validator ->validate ($ request , UpdateAdministratorRequest::class);
157
-
229
+ $ dto = $ this ->validator ->validate ($ request , UpdateAdministratorRequest::class);
158
230
$ this ->administratorManager ->updateAdministrator ($ administrator , $ dto );
159
231
160
232
return new JsonResponse (null , Response::HTTP_OK );
@@ -187,12 +259,14 @@ public function updateAdministrator(
187
259
]
188
260
)]
189
261
public function deleteAdministrator (
262
+ Request $ request ,
190
263
#[MapEntity(mapping: ['administratorId ' => 'id ' ])] ?Administrator $ administrator
191
264
): JsonResponse {
265
+ $ this ->requireAuthentication ($ request );
266
+
192
267
if (!$ administrator ) {
193
- return new JsonResponse ([ ' message ' => ' Administrator not found. '], Response:: HTTP_NOT_FOUND );
268
+ throw new NotFoundHttpException ( ' Administrator not found. ' );
194
269
}
195
-
196
270
$ this ->administratorManager ->deleteAdministrator ($ administrator );
197
271
198
272
return new JsonResponse (null , Response::HTTP_NO_CONTENT );
0 commit comments