Skip to content

Commit 0a65716

Browse files
committed
ISSUE-345: admin controller
1 parent 9601c11 commit 0a65716

File tree

6 files changed

+408
-0
lines changed

6 files changed

+408
-0
lines changed
+200
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\RestBundle\Controller;
6+
7+
use OpenApi\Attributes as OA;
8+
use PhpList\Core\Domain\Model\Identity\Administrator;
9+
use PhpList\RestBundle\Controller\Traits\AuthenticationTrait;
10+
use PhpList\RestBundle\Entity\Request\CreateAdministratorRequest;
11+
use PhpList\RestBundle\Entity\Request\UpdateAdministratorRequest;
12+
use PhpList\RestBundle\Serializer\AdministratorNormalizer;
13+
use PhpList\RestBundle\Service\Manager\AdministratorManager;
14+
use PhpList\RestBundle\Validator\RequestValidator;
15+
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
16+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
17+
use Symfony\Component\HttpFoundation\JsonResponse;
18+
use Symfony\Component\HttpFoundation\Request;
19+
use Symfony\Component\HttpFoundation\Response;
20+
use Symfony\Component\Routing\Attribute\Route;
21+
22+
/**
23+
* This controller provides CRUD operations for Administrator entities.
24+
*/
25+
#[Route('/administrators')]
26+
class AdministratorController extends AbstractController
27+
{
28+
use AuthenticationTrait;
29+
30+
private AdministratorManager $administratorManager;
31+
32+
public function __construct(AdministratorManager $administratorManager)
33+
{
34+
$this->administratorManager = $administratorManager;
35+
}
36+
37+
#[Route('', name: 'create_administrator', methods: ['POST'])]
38+
#[OA\Post(
39+
path: '/administrators',
40+
description: 'Create a new administrator.',
41+
summary: 'Create Administrator',
42+
requestBody: new OA\RequestBody(
43+
description: 'Administrator data',
44+
required: true,
45+
content: new OA\JsonContent(ref: '#/components/schemas/CreateAdministratorRequest')
46+
),
47+
tags: ['administrators'],
48+
responses: [
49+
new OA\Response(
50+
response: 201,
51+
description: 'Administrator created successfully',
52+
content: new OA\JsonContent(ref: '#/components/schemas/CreateAdministratorRequest')
53+
),
54+
new OA\Response(
55+
response: 400,
56+
description: 'Invalid input'
57+
)
58+
]
59+
)]
60+
public function createAdministrator(
61+
Request $request,
62+
RequestValidator $validator,
63+
AdministratorNormalizer $normalizer
64+
): JsonResponse {
65+
/** @var CreateAdministratorRequest $dto */
66+
$dto = $validator->validate($request, CreateAdministratorRequest::class);
67+
68+
$administrator = $this->administratorManager->createAdministrator($dto);
69+
70+
$json = $normalizer->normalize($administrator, 'json');
71+
72+
return new JsonResponse($json, Response::HTTP_CREATED);
73+
}
74+
75+
#[Route('/{administratorId}', name: 'get_administrator', methods: ['GET'])]
76+
#[OA\Get(
77+
path: '/administrators/{administratorId}',
78+
description: 'Get administrator by ID.',
79+
summary: 'Get Administrator',
80+
tags: ['administrators'],
81+
parameters: [
82+
new OA\Parameter(
83+
name: 'administratorId',
84+
description: 'Administrator ID',
85+
in: 'path',
86+
required: true,
87+
schema: new OA\Schema(type: 'integer')
88+
)
89+
],
90+
responses: [
91+
new OA\Response(
92+
response: 200,
93+
description: 'Administrator found',
94+
content: new OA\JsonContent(ref: '#/components/schemas/Administrator')
95+
),
96+
new OA\Response(
97+
response: 404,
98+
description: 'Administrator not found'
99+
)
100+
]
101+
)]
102+
public function getAdministrator(
103+
#[MapEntity(mapping: ['administratorId' => 'id'])] ?Administrator $administrator,
104+
AdministratorNormalizer $normalizer
105+
): JsonResponse {
106+
if (!$administrator) {
107+
return new JsonResponse(['message' => 'Administrator not found.'], Response::HTTP_NOT_FOUND);
108+
}
109+
110+
$json = $normalizer->normalize($administrator, 'json');
111+
112+
return new JsonResponse($json);
113+
}
114+
115+
#[Route('/{administratorId}', name: 'update_administrator', methods: ['PUT'])]
116+
#[OA\Put(
117+
path: '/administrators/{administratorId}',
118+
description: 'Update an administrator.',
119+
summary: 'Update Administrator',
120+
requestBody: new OA\RequestBody(
121+
description: 'Administrator update data',
122+
required: true,
123+
content: new OA\JsonContent(ref: '#/components/schemas/UpdateAdministratorRequest')
124+
),
125+
tags: ['administrators'],
126+
parameters: [
127+
new OA\Parameter(
128+
name: 'administratorId',
129+
description: 'Administrator ID',
130+
in: 'path',
131+
required: true,
132+
schema: new OA\Schema(type: 'integer')
133+
)
134+
],
135+
responses: [
136+
new OA\Response(
137+
response: 200,
138+
description: 'Administrator updated successfully'
139+
),
140+
new OA\Response(
141+
response: 404,
142+
description: 'Administrator not found'
143+
)
144+
]
145+
)]
146+
public function updateAdministrator(
147+
Request $request,
148+
#[MapEntity(mapping: ['administratorId' => 'id'])] ?Administrator $administrator,
149+
RequestValidator $validator
150+
): JsonResponse {
151+
if (!$administrator) {
152+
return new JsonResponse(['message' => 'Administrator not found.'], Response::HTTP_NOT_FOUND);
153+
}
154+
155+
/** @var UpdateAdministratorRequest $dto */
156+
$dto = $validator->validate($request, UpdateAdministratorRequest::class);
157+
158+
$this->administratorManager->updateAdministrator($administrator, $dto);
159+
160+
return new JsonResponse(null, Response::HTTP_OK);
161+
}
162+
163+
#[Route('/{administratorId}', name: 'delete_administrator', methods: ['DELETE'])]
164+
#[OA\Delete(
165+
path: '/administrators/{administratorId}',
166+
description: 'Delete an administrator.',
167+
summary: 'Delete Administrator',
168+
tags: ['administrators'],
169+
parameters: [
170+
new OA\Parameter(
171+
name: 'administratorId',
172+
description: 'Administrator ID',
173+
in: 'path',
174+
required: true,
175+
schema: new OA\Schema(type: 'integer')
176+
)
177+
],
178+
responses: [
179+
new OA\Response(
180+
response: 204,
181+
description: 'Administrator deleted successfully'
182+
),
183+
new OA\Response(
184+
response: 404,
185+
description: 'Administrator not found'
186+
)
187+
]
188+
)]
189+
public function deleteAdministrator(
190+
#[MapEntity(mapping: ['administratorId' => 'id'])] ?Administrator $administrator
191+
): JsonResponse {
192+
if (!$administrator) {
193+
return new JsonResponse(['message' => 'Administrator not found.'], Response::HTTP_NOT_FOUND);
194+
}
195+
196+
$this->administratorManager->deleteAdministrator($administrator);
197+
198+
return new JsonResponse(null, Response::HTTP_NO_CONTENT);
199+
}
200+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\RestBundle\Entity\Request;
6+
7+
use Symfony\Component\Validator\Constraints as Assert;
8+
9+
class CreateAdministratorRequest
10+
{
11+
#[Assert\NotBlank]
12+
#[Assert\Length(min: 3, max: 255)]
13+
public string $loginName;
14+
15+
#[Assert\NotBlank]
16+
#[Assert\Length(min: 6, max: 255)]
17+
public string $password;
18+
19+
#[Assert\NotBlank]
20+
#[Assert\Email]
21+
public string $email;
22+
23+
#[Assert\NotNull]
24+
#[Assert\Type('bool')]
25+
public bool $superUser = false;
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\RestBundle\Entity\Request;
6+
7+
use Symfony\Component\Validator\Constraints as Assert;
8+
9+
class UpdateAdministratorRequest
10+
{
11+
#[Assert\Length(min: 3, max: 255)]
12+
public ?string $loginName = null;
13+
14+
#[Assert\Length(min: 6, max: 255)]
15+
public ?string $password = null;
16+
17+
#[Assert\Email]
18+
public ?string $email = null;
19+
20+
#[Assert\Type('bool')]
21+
public ?bool $superAdmin = null;
22+
}

src/OpenApi/SwaggerSchemasRequestDto.php

+65
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,71 @@ enum: ['html', 'text', 'invite'],
7777
],
7878
type: 'object'
7979
)]
80+
#[OA\Schema(
81+
schema: 'CreateAdministratorRequest',
82+
required: ['login_name', 'password', 'email', 'super_user'],
83+
properties: [
84+
new OA\Property(
85+
property: 'login_name',
86+
type: 'string',
87+
maxLength: 255,
88+
minLength: 3,
89+
example: 'admin'
90+
),
91+
new OA\Property(
92+
property: 'password',
93+
type: 'string',
94+
format: 'password',
95+
maxLength: 255,
96+
minLength: 6,
97+
example: 'StrongP@ssw0rd'
98+
),
99+
new OA\Property(
100+
property: 'email',
101+
type: 'string',
102+
format: 'email',
103+
example: '[email protected]'
104+
),
105+
new OA\Property(
106+
property: 'super_user',
107+
type: 'boolean',
108+
example: false
109+
),
110+
],
111+
type: 'object'
112+
)]
113+
#[OA\Schema(
114+
schema: 'UpdateAdministratorRequest',
115+
properties: [
116+
new OA\Property(
117+
property: 'login_name',
118+
type: 'string',
119+
maxLength: 255,
120+
minLength: 3,
121+
example: 'admin'
122+
),
123+
new OA\Property(
124+
property: 'password',
125+
type: 'string',
126+
format: 'password',
127+
maxLength: 255,
128+
minLength: 6,
129+
example: 'StrongP@ssw0rd'
130+
),
131+
new OA\Property(
132+
property: 'email',
133+
type: 'string',
134+
format: 'email',
135+
example: '[email protected]'
136+
),
137+
new OA\Property(
138+
property: 'super_user',
139+
type: 'boolean',
140+
example: false
141+
),
142+
],
143+
type: 'object'
144+
)]
80145
class SwaggerSchemasRequestDto
81146
{
82147
}
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\RestBundle\Serializer;
6+
7+
use InvalidArgumentException;
8+
use PhpList\Core\Domain\Model\Identity\Administrator;
9+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
10+
11+
class AdministratorNormalizer implements NormalizerInterface
12+
{
13+
public function normalize($object, string $format = null, array $context = []): array
14+
{
15+
if (!$object instanceof Administrator) {
16+
throw new InvalidArgumentException('Expected an Administrator object.');
17+
}
18+
19+
return [
20+
'id' => $object->getId(),
21+
'login_name' => $object->getLoginName(),
22+
'email' => $object->getEmail(),
23+
'super_admin' => $object->isSuperAdmin(),
24+
'created_at' => $object->getCreatedAt()?->format(\DateTimeInterface::ATOM),
25+
];
26+
}
27+
28+
public function supportsNormalization($data, string $format = null): bool
29+
{
30+
return $data instanceof Administrator;
31+
}
32+
}

0 commit comments

Comments
 (0)