Skip to content

Commit b20d433

Browse files
committed
ISSUE-345: admin attributes
1 parent 62b2aea commit b20d433

4 files changed

+471
-0
lines changed
Lines changed: 366 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,366 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\RestBundle\Identity\Controller;
6+
7+
use OpenApi\Attributes as OA;
8+
use PhpList\Core\Domain\Identity\Model\AdminAttributeDefinition;
9+
use PhpList\Core\Domain\Identity\Service\AdminAttributeDefinitionManager;
10+
use PhpList\Core\Security\Authentication;
11+
use PhpList\RestBundle\Common\Controller\BaseController;
12+
use PhpList\RestBundle\Common\Service\Provider\PaginatedDataProvider;
13+
use PhpList\RestBundle\Common\Validator\RequestValidator;
14+
use PhpList\RestBundle\Identity\Request\CreateAttributeDefinitionRequest;
15+
use PhpList\RestBundle\Identity\Request\UpdateAttributeDefinitionRequest;
16+
use PhpList\RestBundle\Identity\Serializer\AdminAttributeDefinitionNormalizer;
17+
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
18+
use Symfony\Component\HttpFoundation\JsonResponse;
19+
use Symfony\Component\HttpFoundation\Request;
20+
use Symfony\Component\HttpFoundation\Response;
21+
use Symfony\Component\Routing\Attribute\Route;
22+
23+
#[Route('/administrators/attributes')]
24+
class AdminAttributeDefinitionController extends BaseController
25+
{
26+
private AdminAttributeDefinitionManager $definitionManager;
27+
private AdminAttributeDefinitionNormalizer $normalizer;
28+
private PaginatedDataProvider $paginatedDataProvider;
29+
30+
public function __construct(
31+
Authentication $authentication,
32+
RequestValidator $validator,
33+
AdminAttributeDefinitionManager $definitionManager,
34+
AdminAttributeDefinitionNormalizer $normalizer,
35+
PaginatedDataProvider $paginatedDataProvider
36+
) {
37+
parent::__construct($authentication, $validator);
38+
$this->definitionManager = $definitionManager;
39+
$this->normalizer = $normalizer;
40+
$this->paginatedDataProvider = $paginatedDataProvider;
41+
}
42+
43+
#[Route('', name: 'create_admin_attribute_definition', methods: ['POST'])]
44+
#[OA\Post(
45+
path: '/administrators/attributes',
46+
description: 'Returns created admin attribute definition.',
47+
summary: 'Create an admin attribute definition.',
48+
requestBody: new OA\RequestBody(
49+
description: 'Pass parameters to create admin attribute.',
50+
required: true,
51+
content: new OA\JsonContent(
52+
required: ['name'],
53+
properties: [
54+
new OA\Property(property: 'name', type: 'string', format: 'string', example: 'Country'),
55+
new OA\Property(property: 'type', type: 'string', example: 'checkbox'),
56+
new OA\Property(property: 'order', type: 'number', example: 12),
57+
new OA\Property(property: 'default_value', type: 'string', example: 'United States'),
58+
new OA\Property(property: 'required', type: 'boolean', example: true),
59+
new OA\Property(property: 'table_name', type: 'string', example: 'list_attributes'),
60+
]
61+
)
62+
),
63+
tags: ['admin-attributes'],
64+
parameters: [
65+
new OA\Parameter(
66+
name: 'session',
67+
description: 'Session ID obtained from authentication',
68+
in: 'header',
69+
required: true,
70+
schema: new OA\Schema(
71+
type: 'string'
72+
)
73+
)
74+
],
75+
responses: [
76+
new OA\Response(
77+
response: 201,
78+
description: 'Success',
79+
content: new OA\JsonContent(ref: '#/components/schemas/AttributeDefinition')
80+
),
81+
new OA\Response(
82+
response: 403,
83+
description: 'Failure',
84+
content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedResponse')
85+
),
86+
new OA\Response(
87+
response: 422,
88+
description: 'Failure',
89+
content: new OA\JsonContent(ref: '#/components/schemas/ValidationErrorResponse')
90+
),
91+
]
92+
)]
93+
public function create(Request $request): JsonResponse
94+
{
95+
$this->requireAuthentication($request);
96+
97+
/** @var CreateAttributeDefinitionRequest $definitionRequest */
98+
$definitionRequest = $this->validator->validate($request, CreateAttributeDefinitionRequest::class);
99+
100+
$attributeDefinition = $this->definitionManager->create($definitionRequest->getDto());
101+
$json = $this->normalizer->normalize($attributeDefinition, 'json');
102+
103+
return $this->json($json, Response::HTTP_CREATED);
104+
}
105+
106+
#[Route('/{definitionId}', name: 'update_admin_attribute_definition', methods: ['PUT'])]
107+
#[OA\Put(
108+
path: '/administrators/attributes/{definitionId}',
109+
description: 'Returns updated admin attribute definition.',
110+
summary: 'Update an admin attribute definition.',
111+
requestBody: new OA\RequestBody(
112+
description: 'Pass parameters to update admin attribute.',
113+
required: true,
114+
content: new OA\JsonContent(
115+
required: ['name'],
116+
properties: [
117+
new OA\Property(property: 'name', type: 'string', format: 'string', example: 'Country'),
118+
new OA\Property(property: 'type', type: 'string', example: 'checkbox'),
119+
new OA\Property(property: 'order', type: 'number', example: 12),
120+
new OA\Property(property: 'default_value', type: 'string', example: 'United States'),
121+
new OA\Property(property: 'required', type: 'boolean', example: true),
122+
new OA\Property(property: 'table_name', type: 'string', example: 'list_attributes'),
123+
]
124+
)
125+
),
126+
tags: ['admin-attributes'],
127+
parameters: [
128+
new OA\Parameter(
129+
name: 'definitionId',
130+
description: 'Definition ID',
131+
in: 'path',
132+
required: true,
133+
schema: new OA\Schema(type: 'string')
134+
),
135+
new OA\Parameter(
136+
name: 'session',
137+
description: 'Session ID obtained from authentication',
138+
in: 'header',
139+
required: true,
140+
schema: new OA\Schema(type: 'string')
141+
)
142+
],
143+
responses: [
144+
new OA\Response(
145+
response: 200,
146+
description: 'Success',
147+
content: new OA\JsonContent(ref: '#/components/schemas/AttributeDefinition')
148+
),
149+
new OA\Response(
150+
response: 403,
151+
description: 'Failure',
152+
content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedResponse')
153+
),
154+
new OA\Response(
155+
response: 422,
156+
description: 'Failure',
157+
content: new OA\JsonContent(ref: '#/components/schemas/ValidationErrorResponse')
158+
),
159+
]
160+
)]
161+
public function update(
162+
Request $request,
163+
#[MapEntity(mapping: ['definitionId' => 'id'])] ?AdminAttributeDefinition $attributeDefinition,
164+
): JsonResponse {
165+
$this->requireAuthentication($request);
166+
if (!$attributeDefinition) {
167+
throw $this->createNotFoundException('Attribute definition not found.');
168+
}
169+
170+
/** @var UpdateAttributeDefinitionRequest $definitionRequest */
171+
$definitionRequest = $this->validator->validate($request, UpdateAttributeDefinitionRequest::class);
172+
173+
$attributeDefinition = $this->definitionManager->update(
174+
attributeDefinition: $attributeDefinition,
175+
attributeDefinitionDto: $definitionRequest->getDto(),
176+
);
177+
$json = $this->normalizer->normalize($attributeDefinition, 'json');
178+
179+
return $this->json($json, Response::HTTP_OK);
180+
}
181+
182+
#[Route('/{definitionId}', name: 'delete_admin_attribute_definition', methods: ['DELETE'])]
183+
#[OA\Delete(
184+
path: '/administrators/attributes/{definitionId}',
185+
description: 'Deletes a single admin attribute definition.',
186+
summary: 'Deletes an attribute definition.',
187+
tags: ['admin-attributes'],
188+
parameters: [
189+
new OA\Parameter(
190+
name: 'session',
191+
description: 'Session ID',
192+
in: 'header',
193+
required: true,
194+
schema: new OA\Schema(type: 'string')
195+
),
196+
new OA\Parameter(
197+
name: 'definitionId',
198+
description: 'Definition ID',
199+
in: 'path',
200+
required: true,
201+
schema: new OA\Schema(type: 'string')
202+
)
203+
],
204+
responses: [
205+
new OA\Response(
206+
response: 200,
207+
description: 'Success'
208+
),
209+
new OA\Response(
210+
response: 403,
211+
description: 'Failure',
212+
content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedResponse')
213+
),
214+
new OA\Response(
215+
response: 404,
216+
description: 'Failure',
217+
content: new OA\JsonContent(ref: '#/components/schemas/NotFoundErrorResponse')
218+
)
219+
]
220+
)]
221+
public function delete(
222+
Request $request,
223+
#[MapEntity(mapping: ['definitionId' => 'id'])] ?AdminAttributeDefinition $attributeDefinition,
224+
): JsonResponse {
225+
$this->requireAuthentication($request);
226+
if (!$attributeDefinition) {
227+
throw $this->createNotFoundException('Attribute definition not found.');
228+
}
229+
230+
$this->definitionManager->delete($attributeDefinition);
231+
232+
return $this->json(null, Response::HTTP_NO_CONTENT);
233+
}
234+
235+
#[Route('', name: 'get_admin_attribute_definitions', methods: ['GET'])]
236+
#[OA\Get(
237+
path: '/administrators/attributes',
238+
description: 'Returns a JSON list of all admin attribute definitions.',
239+
summary: 'Gets a list of all DMIN attribute definitions.',
240+
tags: ['admin-attributes'],
241+
parameters: [
242+
new OA\Parameter(
243+
name: 'session',
244+
description: 'Session ID obtained from authentication',
245+
in: 'header',
246+
required: true,
247+
schema: new OA\Schema(
248+
type: 'string'
249+
)
250+
),
251+
new OA\Parameter(
252+
name: 'after_id',
253+
description: 'Last id (starting from 0)',
254+
in: 'query',
255+
required: false,
256+
schema: new OA\Schema(type: 'integer', default: 1, minimum: 1)
257+
),
258+
new OA\Parameter(
259+
name: 'limit',
260+
description: 'Number of results per page',
261+
in: 'query',
262+
required: false,
263+
schema: new OA\Schema(type: 'integer', default: 25, maximum: 100, minimum: 1)
264+
)
265+
],
266+
responses: [
267+
new OA\Response(
268+
response: 200,
269+
description: 'Success',
270+
content: new OA\JsonContent(
271+
properties: [
272+
new OA\Property(
273+
property: 'items',
274+
type: 'array',
275+
items: new OA\Items(ref: '#/components/schemas/AttributeDefinition')
276+
),
277+
new OA\Property(property: 'pagination', ref: '#/components/schemas/CursorPagination')
278+
],
279+
type: 'object'
280+
)
281+
),
282+
new OA\Response(
283+
response: 403,
284+
description: 'Failure',
285+
content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedResponse')
286+
)
287+
]
288+
)]
289+
public function getPaginated(Request $request): JsonResponse
290+
{
291+
$this->requireAuthentication($request);
292+
293+
return $this->json(
294+
$this->paginatedDataProvider->getPaginatedList(
295+
$request,
296+
$this->normalizer,
297+
AdminAttributeDefinition::class,
298+
),
299+
Response::HTTP_OK
300+
);
301+
}
302+
303+
#[Route('/{definitionId}', name: 'get_admin_attribute_definition', methods: ['GET'])]
304+
#[OA\Get(
305+
path: '/administrators/attributes/{definitionId}',
306+
description: 'Returns a single attribute with specified ID.',
307+
summary: 'Gets attribute with specified ID.',
308+
tags: ['admin-attributes'],
309+
parameters: [
310+
new OA\Parameter(
311+
name: 'definitionId',
312+
description: 'Definition ID',
313+
in: 'path',
314+
required: true,
315+
schema: new OA\Schema(type: 'string')
316+
),
317+
new OA\Parameter(
318+
name: 'session',
319+
description: 'Session ID obtained from authentication',
320+
in: 'header',
321+
required: true,
322+
schema: new OA\Schema(type: 'string')
323+
)
324+
],
325+
responses: [
326+
new OA\Response(
327+
response: 200,
328+
description: 'Success',
329+
content: new OA\JsonContent(ref: '#/components/schemas/AttributeDefinition')
330+
),
331+
new OA\Response(
332+
response: 403,
333+
description: 'Failure',
334+
content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedResponse')
335+
),
336+
new OA\Response(
337+
response: 404,
338+
description: 'Failure',
339+
content: new OA\JsonContent(
340+
properties: [
341+
new OA\Property(
342+
property: 'message',
343+
type: 'string',
344+
example: 'There is no attribute with that ID.'
345+
)
346+
],
347+
type: 'object'
348+
)
349+
)
350+
]
351+
)]
352+
public function getAttributeDefinition(
353+
Request $request,
354+
#[MapEntity(mapping: ['definitionId' => 'id'])] ?AdminAttributeDefinition $attributeDefinition,
355+
): JsonResponse {
356+
$this->requireAuthentication($request);
357+
if (!$attributeDefinition) {
358+
throw $this->createNotFoundException('Attribute definition not found.');
359+
}
360+
361+
return $this->json(
362+
$this->normalizer->normalize($attributeDefinition),
363+
Response::HTTP_OK
364+
);
365+
}
366+
}

0 commit comments

Comments
 (0)