Skip to content

Commit ab4d666

Browse files
committed
Attachment endpoints
1 parent 7f88a4b commit ab4d666

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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\CQRSPartialUpdate;
37+
use PrestaShopBundle\ApiPlatform\Metadata\LocalizedValue;
38+
use Symfony\Component\HttpFoundation\Response;
39+
use Symfony\Component\Validator\Constraints as Assert;
40+
/**
41+
* //TODO <cnc> questa API necessita della seguente PR per poter funzionare:
42+
* - https://github.com/PrestaShop/PrestaShop/pull/39966
43+
*/
44+
#[ApiResource(
45+
operations: [
46+
new CQRSGet(
47+
uriTemplate: '/attachments/{attachmentId}',
48+
CQRSQuery: GetAttachmentForEditing::class,
49+
scopes: [
50+
'attachment_read',
51+
],
52+
CQRSQueryMapping: self::QUERY_MAPPING,
53+
),
54+
new CQRSCreate(
55+
uriTemplate: '/attachments',
56+
validationContext: ['groups' => ['Default', 'Create']],
57+
CQRSCommand: AddAttachmentCommand::class,
58+
CQRSQuery: GetAttachmentForEditing::class,
59+
scopes: [
60+
'attachment_write',
61+
],
62+
CQRSQueryMapping: self::QUERY_MAPPING,
63+
CQRSCommandMapping: self::COMMAND_MAPPING,
64+
),
65+
new CQRSPartialUpdate(
66+
uriTemplate: '/attachments/{attachmentId}',
67+
validationContext: ['groups' => ['Default', 'Update']],
68+
CQRSCommand: EditAttachmentCommand::class,
69+
CQRSQuery: GetAttachmentForEditing::class,
70+
scopes: [
71+
'attachment_write',
72+
],
73+
CQRSQueryMapping: self::QUERY_MAPPING,
74+
CQRSCommandMapping: self::COMMAND_MAPPING,
75+
),
76+
new CQRSDelete(
77+
uriTemplate: '/attachments/{attachmentId}',
78+
CQRSCommand: DeleteAttachmentCommand::class,
79+
scopes: [
80+
'attachment_write',
81+
],
82+
),
83+
],
84+
exceptionToStatus: [
85+
AttachmentConstraintException::class => Response::HTTP_UNPROCESSABLE_ENTITY,
86+
AttachmentNotFoundException::class => Response::HTTP_NOT_FOUND,
87+
],
88+
)]
89+
class Attachment
90+
{
91+
#[ApiProperty(identifier: true)]
92+
public int $attachmentId;
93+
94+
#[LocalizedValue]
95+
#[DefaultLanguage(groups: ['Create'], fieldName: 'names')]
96+
#[DefaultLanguage(groups: ['Update'], fieldName: 'names', allowNull: true)]
97+
#[Assert\All(constraints: [
98+
new TypedRegex([
99+
'type' => TypedRegex::TYPE_CATALOG_NAME,
100+
]),
101+
])]
102+
public array $names;
103+
104+
#[LocalizedValue]
105+
#[DefaultLanguage(groups: ['Create'], fieldName: 'descriptions')]
106+
#[DefaultLanguage(groups: ['Update'], fieldName: 'descriptions', allowNull: true)]
107+
public array $descriptions;
108+
109+
public const QUERY_MAPPING = [
110+
'[name]' => '[names]',
111+
'[description]' => '[descriptions]',
112+
];
113+
114+
public const COMMAND_MAPPING = [
115+
'[names]' => '[localizedNames]',
116+
'[descriptions]' => '[localizedDescriptions]',
117+
];
118+
}
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+
}

0 commit comments

Comments
 (0)