Skip to content

Commit e384615

Browse files
committed
Add: update template endpoint
1 parent 567ac65 commit e384615

2 files changed

Lines changed: 150 additions & 0 deletions

File tree

src/Messaging/Controller/TemplateController.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PhpList\RestBundle\Common\Service\Provider\PaginatedDataProvider;
1515
use PhpList\RestBundle\Common\Validator\RequestValidator;
1616
use PhpList\RestBundle\Messaging\Request\CreateTemplateRequest;
17+
use PhpList\RestBundle\Messaging\Request\UpdateTemplateRequest;
1718
use PhpList\RestBundle\Messaging\Serializer\TemplateNormalizer;
1819
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
1920
use Symfony\Component\HttpFoundation\JsonResponse;
@@ -229,6 +230,72 @@ public function createTemplates(Request $request): JsonResponse
229230
);
230231
}
231232

233+
#[Route('/{templateId}', name: 'update', methods: ['PUT'])]
234+
#[OA\Put(
235+
path: '/api/v2/templates/{templateId}',
236+
description: '🚧 **Status: Beta** – This method is under development. Avoid using in production. ' .
237+
'Returns a JSON response of updated template.',
238+
summary: 'Update template.',
239+
requestBody: new OA\RequestBody(
240+
description: 'Pass session credentials',
241+
required: true,
242+
content: new OA\MediaType(
243+
mediaType: 'multipart/form-data',
244+
schema: new OA\Schema(ref: '#/components/schemas/CreateTemplateRequest')
245+
)
246+
),
247+
tags: ['templates'],
248+
parameters: [
249+
new OA\Parameter(
250+
name: 'php-auth-pw',
251+
description: 'Session key obtained from login',
252+
in: 'header',
253+
required: true,
254+
schema: new OA\Schema(type: 'string')
255+
),
256+
],
257+
responses: [
258+
new OA\Response(
259+
response: 201,
260+
description: 'Success',
261+
content: new OA\JsonContent(
262+
type: 'array',
263+
items: new OA\Items(ref: '#/components/schemas/Template')
264+
)
265+
),
266+
new OA\Response(
267+
response: 403,
268+
description: 'Failure',
269+
content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedResponse')
270+
),
271+
new OA\Response(
272+
response: 422,
273+
description: 'Failure',
274+
content: new OA\JsonContent(ref: '#/components/schemas/ValidationErrorResponse')
275+
),
276+
]
277+
)]
278+
public function updateTemplates(
279+
Request $request,
280+
#[MapEntity(mapping: ['templateId' => 'id'])] ?Template $template = null,
281+
): JsonResponse {
282+
$this->requireAuthentication($request);
283+
284+
if (!$template) {
285+
throw $this->createNotFoundException('Template not found.');
286+
}
287+
288+
/** @var UpdateTemplateRequest $templateRequest */
289+
$templateRequest = $this->validator->validate($request, UpdateTemplateRequest::class);
290+
$template = $this->templateManager->update(template: $template, updateTemplateDto: $templateRequest->getDto());
291+
$this->entityManager->flush();
292+
293+
return $this->json(
294+
data: $this->normalizer->normalize($template),
295+
status: Response::HTTP_CREATED
296+
);
297+
}
298+
232299
#[Route('/{templateId}', name: 'delete', requirements: ['templateId' => '\d+'], methods: ['DELETE'])]
233300
#[OA\Delete(
234301
path: '/api/v2/templates/{templateId}',
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\RestBundle\Messaging\Request;
6+
7+
use OpenApi\Attributes as OA;
8+
use PhpList\Core\Domain\Messaging\Model\Dto\CreateTemplateDto;
9+
use PhpList\Core\Domain\Messaging\Model\Dto\UpdateTemplateDto;
10+
use PhpList\RestBundle\Common\Request\RequestInterface;
11+
use PhpList\RestBundle\Messaging\Validator\Constraint\ContainsPlaceholder;
12+
use Symfony\Component\HttpFoundation\File\UploadedFile;
13+
use Symfony\Component\Validator\Constraints as Assert;
14+
15+
#[OA\Schema(
16+
schema: 'CreateTemplateRequest',
17+
required: ['title'],
18+
properties: [
19+
new OA\Property(property: 'title', type: 'string', example: 'Newsletter Template'),
20+
new OA\Property(
21+
property: 'content',
22+
type: 'string',
23+
example: '<html><body>[CONTENT]</body></html>',
24+
nullable: true
25+
),
26+
new OA\Property(property: 'text', type: 'string', example: '[CONTENT]'),
27+
new OA\Property(
28+
property: 'file',
29+
description: 'Optional file upload for HTML content',
30+
type: 'string',
31+
format: 'binary'
32+
),
33+
new OA\Property(
34+
property: 'check_links',
35+
description: 'Check that all links have full URLs',
36+
type: 'boolean',
37+
example: true
38+
),
39+
new OA\Property(
40+
property: 'check_images',
41+
description: 'Check that all images have full URLs',
42+
type: 'boolean',
43+
example: false
44+
),
45+
new OA\Property(
46+
property: 'check_external_images',
47+
description: 'Check that all external images exist',
48+
type: 'boolean',
49+
example: true
50+
),
51+
],
52+
type: 'object'
53+
)]
54+
class UpdateTemplateRequest implements RequestInterface
55+
{
56+
#[Assert\NotBlank(normalizer: 'trim')]
57+
#[Assert\NotNull]
58+
public string $title;
59+
60+
#[ContainsPlaceholder]
61+
public ?string $content = null;
62+
63+
#[ContainsPlaceholder]
64+
public ?string $text = null;
65+
66+
public ?UploadedFile $file = null;
67+
public bool $checkLinks = false;
68+
public bool $checkImages = false;
69+
public bool $checkExternalImages = false;
70+
71+
public function getDto(): UpdateTemplateDto
72+
{
73+
return new UpdateTemplateDto(
74+
title: $this->title,
75+
content: $this->content,
76+
text: $this->text,
77+
fileContent: $this->file instanceof UploadedFile ? file_get_contents($this->file->getPathname()) : null,
78+
shouldCheckLinks: $this->checkLinks,
79+
shouldCheckImages: $this->checkImages,
80+
shouldCheckExternalImages: $this->checkExternalImages,
81+
);
82+
}
83+
}

0 commit comments

Comments
 (0)