Skip to content

Commit e97a083

Browse files
committed
Attachment endpoints
1 parent 7f88a4b commit e97a083

File tree

4 files changed

+435
-0
lines changed

4 files changed

+435
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
/**
3+
* Copyright since 2007 PrestaShop SA and Contributors
4+
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5+
*
6+
* NOTICE OF LICENSE
7+
*
8+
* This source file is subject to the Academic Free License version 3.0
9+
* that is bundled with this package in the file LICENSE.md.
10+
* It is also available through the world-wide-web at this URL:
11+
* https://opensource.org/licenses/AFL-3.0
12+
* If you did not receive a copy of the license and are unable to
13+
* obtain it through the world-wide-web, please send an email
14+
* to license@prestashop.com so we can send you a copy immediately.
15+
*
16+
* @author PrestaShop SA and Contributors <contact@prestashop.com>
17+
* @copyright Since 2007 PrestaShop SA and Contributors
18+
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
19+
*/
20+
21+
namespace PrestaShop\Module\APIResources\ApiPlatform\Resources\Attachment;
22+
23+
use ApiPlatform\Metadata\ApiProperty;
24+
use ApiPlatform\Metadata\ApiResource;
25+
use PrestaShop\PrestaShop\Core\ConstraintValidator\Constraints\DefaultLanguage;
26+
use PrestaShop\PrestaShop\Core\ConstraintValidator\Constraints\TypedRegex;
27+
use PrestaShop\PrestaShop\Core\Domain\Attachment\Command\AddAttachmentCommand;
28+
use PrestaShop\PrestaShop\Core\Domain\Attachment\Command\DeleteAttachmentCommand;
29+
use PrestaShop\PrestaShop\Core\Domain\Attachment\Command\EditAttachmentCommand;
30+
use PrestaShop\PrestaShop\Core\Domain\Attachment\Exception\AttachmentConstraintException;
31+
use PrestaShop\PrestaShop\Core\Domain\Attachment\Exception\AttachmentNotFoundException;
32+
use PrestaShop\PrestaShop\Core\Domain\Attachment\Query\GetAttachmentForEditing;
33+
use PrestaShopBundle\ApiPlatform\Metadata\CQRSCreate;
34+
use PrestaShopBundle\ApiPlatform\Metadata\CQRSDelete;
35+
use PrestaShopBundle\ApiPlatform\Metadata\CQRSGet;
36+
use PrestaShopBundle\ApiPlatform\Metadata\CQRSUpdate;
37+
use PrestaShopBundle\ApiPlatform\Metadata\LocalizedValue;
38+
use Symfony\Component\HttpFoundation\File\File;
39+
use Symfony\Component\HttpFoundation\Response;
40+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
41+
use Symfony\Component\Validator\Constraints as Assert;
42+
43+
#[ApiResource(
44+
operations: [
45+
new CQRSGet(
46+
uriTemplate: '/attachments/{attachmentId}',
47+
CQRSQuery: GetAttachmentForEditing::class,
48+
scopes: [
49+
'attachment_read',
50+
],
51+
CQRSQueryMapping: self::QUERY_MAPPING,
52+
),
53+
new CQRSCreate(
54+
uriTemplate: '/attachments',
55+
inputFormats: ['multipart' => ['multipart/form-data']],
56+
// Form data value are all string so we disable type enforcement
57+
denormalizationContext: [ObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true],
58+
validationContext: ['groups' => ['Default', 'Create']],
59+
CQRSCommand: AddAttachmentCommand::class,
60+
CQRSQuery: GetAttachmentForEditing::class,
61+
scopes: [
62+
'attachment_write',
63+
],
64+
CQRSQueryMapping: self::QUERY_MAPPING,
65+
CQRSCommandMapping: self::COMMAND_MAPPING,
66+
),
67+
new CQRSUpdate(
68+
// We have to force POST request, because we cannot use PUT with files AND data
69+
method: CQRSUpdate::METHOD_POST,
70+
uriTemplate: '/attachments/{attachmentId}',
71+
inputFormats: ['multipart' => ['multipart/form-data']],
72+
validationContext: ['groups' => ['Default', 'Update']],
73+
// Form data value are all string so we disable type enforcement
74+
denormalizationContext: [ObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true],
75+
CQRSCommand: EditAttachmentCommand::class,
76+
CQRSQuery: GetAttachmentForEditing::class,
77+
scopes: [
78+
'attachment_write',
79+
],
80+
CQRSQueryMapping: self::QUERY_MAPPING,
81+
CQRSCommandMapping: self::COMMAND_MAPPING,
82+
),
83+
new CQRSDelete(
84+
uriTemplate: '/attachments/{attachmentId}',
85+
CQRSCommand: DeleteAttachmentCommand::class,
86+
scopes: [
87+
'attachment_write',
88+
],
89+
),
90+
],
91+
exceptionToStatus: [
92+
AttachmentConstraintException::class => Response::HTTP_UNPROCESSABLE_ENTITY,
93+
AttachmentNotFoundException::class => Response::HTTP_NOT_FOUND,
94+
],
95+
)]
96+
class Attachment
97+
{
98+
#[ApiProperty(identifier: true)]
99+
public int $attachmentId;
100+
101+
#[LocalizedValue]
102+
#[DefaultLanguage(groups: ['Create'], fieldName: 'names')]
103+
#[DefaultLanguage(groups: ['Update'], fieldName: 'names', allowNull: true)]
104+
#[Assert\All(constraints: [
105+
new TypedRegex([
106+
'type' => TypedRegex::TYPE_CATALOG_NAME,
107+
]),
108+
])]
109+
public array $names;
110+
111+
#[LocalizedValue]
112+
#[DefaultLanguage(groups: ['Create'], fieldName: 'descriptions')]
113+
#[DefaultLanguage(groups: ['Update'], fieldName: 'descriptions', allowNull: true)]
114+
public array $descriptions;
115+
116+
public ?File $attachment = null;
117+
118+
public const QUERY_MAPPING = [
119+
'[name]' => '[names]',
120+
'[description]' => '[descriptions]',
121+
];
122+
123+
public const COMMAND_MAPPING = [
124+
'[names]' => '[localizedNames]',
125+
'[descriptions]' => '[localizedDescriptions]',
126+
'[attachment].pathName' => '[pathName]',
127+
'[attachment].size' => '[fileSize]',
128+
'[attachment].mimeType' => '[mimeType]',
129+
'[attachment].originalName' => '[originalName]',
130+
'[attachment].clientOriginalName' => '[originalName]',
131+
];
132+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Copyright since 2007 PrestaShop SA and Contributors
4+
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5+
*
6+
* NOTICE OF LICENSE
7+
*
8+
* This source file is subject to the Academic Free License version 3.0
9+
* that is bundled with this package in the file LICENSE.md.
10+
* It is also available through the world-wide-web at this URL:
11+
* https://opensource.org/licenses/AFL-3.0
12+
* If you did not receive a copy of the license and are unable to
13+
* obtain it through the world-wide-web, please send an email
14+
* to license@prestashop.com so we can send you a copy immediately.
15+
*
16+
* @author PrestaShop SA and Contributors <contact@prestashop.com>
17+
* @copyright Since 2007 PrestaShop SA and Contributors
18+
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
19+
*/
20+
21+
namespace PrestaShop\Module\APIResources\ApiPlatform\Resources\Attachment;
22+
23+
use ApiPlatform\Metadata\ApiProperty;
24+
use ApiPlatform\Metadata\ApiResource;
25+
use PrestaShop\PrestaShop\Core\Search\Filters\AttachmentFilters;
26+
use PrestaShopBundle\ApiPlatform\Metadata\PaginatedList;
27+
28+
#[ApiResource(
29+
operations: [
30+
new PaginatedList(
31+
uriTemplate: '/attachments',
32+
scopes: [
33+
'attachment_read',
34+
],
35+
ApiResourceMapping: self::MAPPING,
36+
gridDataFactory: 'prestashop.core.grid.data_factory.attachment_decorator',
37+
filtersClass: AttachmentFilters::class,
38+
filtersMapping: [
39+
'[attachmentId]' => '[id_attachment]',
40+
],
41+
),
42+
]
43+
)]
44+
class AttachmentList
45+
{
46+
#[ApiProperty(identifier: true)]
47+
public int $attachmentId;
48+
49+
public string $name;
50+
51+
public string $file;
52+
53+
public string $file_size;
54+
55+
public string $products;
56+
57+
public const MAPPING = [
58+
'[id_attachment]' => '[attachmentId]',
59+
];
60+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright since 2007 PrestaShop SA and Contributors
4+
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5+
*
6+
* NOTICE OF LICENSE
7+
*
8+
* This source file is subject to the Academic Free License version 3.0
9+
* that is bundled with this package in the file LICENSE.md.
10+
* It is also available through the world-wide-web at this URL:
11+
* https://opensource.org/licenses/AFL-3.0
12+
* If you did not receive a copy of the license and are unable to
13+
* obtain it through the world-wide-web, please send an email
14+
* to license@prestashop.com so we can send you a copy immediately.
15+
*
16+
* @author PrestaShop SA and Contributors <contact@prestashop.com>
17+
* @copyright Since 2007 PrestaShop SA and Contributors
18+
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
19+
*/
20+
21+
namespace PrestaShop\Module\APIResources\ApiPlatform\Resources\Attachment;
22+
23+
use ApiPlatform\Metadata\ApiProperty;
24+
use ApiPlatform\Metadata\ApiResource;
25+
use PrestaShop\PrestaShop\Core\Domain\Attachment\Command\BulkDeleteAttachmentsCommand;
26+
use PrestaShop\PrestaShop\Core\Domain\Attachment\Exception\AttachmentNotFoundException;
27+
use PrestaShopBundle\ApiPlatform\Metadata\CQRSDelete;
28+
use Symfony\Component\HttpFoundation\Response;
29+
use Symfony\Component\Validator\Constraints as Assert;
30+
31+
#[ApiResource(
32+
operations: [
33+
new CQRSDelete(
34+
uriTemplate: '/attachments/batch',
35+
output: false,
36+
CQRSCommand: BulkDeleteAttachmentsCommand::class,
37+
scopes: [
38+
'attachment_write',
39+
],
40+
),
41+
],
42+
exceptionToStatus: [
43+
AttachmentNotFoundException::class => Response::HTTP_NOT_FOUND,
44+
],
45+
)]
46+
class BulkAttachments
47+
{
48+
/**
49+
* @var int[]
50+
*/
51+
#[ApiProperty(openapiContext: ['type' => 'array', 'items' => ['type' => 'integer'], 'example' => [1, 3]])]
52+
#[Assert\NotBlank]
53+
public array $attachmentIds;
54+
}

0 commit comments

Comments
 (0)