Skip to content

Commit 97f3e63

Browse files
authored
Merge pull request PrestaShop#66 from Codencode/attribute-endpoints
Added attribute endpoints
2 parents a499921 + 4cbb03d commit 97f3e63

File tree

4 files changed

+639
-0
lines changed

4 files changed

+639
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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\Attribute;
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\AttributeGroup\Attribute\Command\AddAttributeCommand;
28+
use PrestaShop\PrestaShop\Core\Domain\AttributeGroup\Attribute\Command\DeleteAttributeCommand;
29+
use PrestaShop\PrestaShop\Core\Domain\AttributeGroup\Attribute\Command\EditAttributeCommand;
30+
use PrestaShop\PrestaShop\Core\Domain\AttributeGroup\Attribute\Exception\AttributeConstraintException;
31+
use PrestaShop\PrestaShop\Core\Domain\AttributeGroup\Attribute\Exception\AttributeNotFoundException;
32+
use PrestaShop\PrestaShop\Core\Domain\AttributeGroup\Attribute\Query\GetAttributeForEditing;
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+
#[ApiResource(
42+
operations: [
43+
new CQRSGet(
44+
uriTemplate: '/attributes/attribute/{attributeId}',
45+
CQRSQuery: GetAttributeForEditing::class,
46+
scopes: [
47+
'attribute_read',
48+
],
49+
CQRSQueryMapping: self::QUERY_MAPPING,
50+
),
51+
new CQRSCreate(
52+
uriTemplate: '/attributes/attribute',
53+
validationContext: ['groups' => ['Default', 'Create']],
54+
CQRSCommand: AddAttributeCommand::class,
55+
CQRSQuery: GetAttributeForEditing::class,
56+
scopes: [
57+
'attribute_write',
58+
],
59+
CQRSQueryMapping: self::QUERY_MAPPING,
60+
CQRSCommandMapping: self::CREATE_COMMAND_MAPPING,
61+
),
62+
new CQRSPartialUpdate(
63+
uriTemplate: '/attributes/attribute/{attributeId}',
64+
validationContext: ['groups' => ['Default', 'Update']],
65+
CQRSCommand: EditAttributeCommand::class,
66+
CQRSQuery: GetAttributeForEditing::class,
67+
scopes: [
68+
'attribute_write',
69+
],
70+
CQRSQueryMapping: self::QUERY_MAPPING,
71+
CQRSCommandMapping: self::UPDATE_COMMAND_MAPPING,
72+
),
73+
new CQRSDelete(
74+
uriTemplate: '/attributes/attribute/{attributeId}',
75+
CQRSCommand: DeleteAttributeCommand::class,
76+
scopes: [
77+
'attribute_write',
78+
],
79+
),
80+
],
81+
exceptionToStatus: [
82+
AttributeConstraintException::class => Response::HTTP_UNPROCESSABLE_ENTITY,
83+
AttributeNotFoundException::class => Response::HTTP_NOT_FOUND,
84+
],
85+
)]
86+
class Attribute
87+
{
88+
#[ApiProperty(identifier: true)]
89+
public int $attributeId;
90+
91+
#[ApiProperty(openapiContext: ['type' => 'integer', 'example' => 1])]
92+
public int $attributeGroupId;
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+
public string $color;
105+
106+
#[ApiProperty(openapiContext: ['type' => 'array', 'items' => ['type' => 'integer'], 'example' => [1, 3]])]
107+
#[Assert\NotBlank(allowNull: true)]
108+
public array $shopIds;
109+
110+
public int $position;
111+
112+
public const QUERY_MAPPING = [
113+
'[localizedNames]' => '[names]',
114+
'[name]' => '[names]',
115+
'[associatedShopIds]' => '[shopIds]',
116+
];
117+
118+
public const CREATE_COMMAND_MAPPING = [
119+
'[names]' => '[localizedValue]',
120+
'[shopIds]' => '[associatedShopIds]',
121+
];
122+
123+
public const UPDATE_COMMAND_MAPPING = [
124+
'[names]' => '[localizedNames]',
125+
'[shopIds]' => '[associatedShopIds]',
126+
];
127+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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\Attribute;
22+
23+
use ApiPlatform\Metadata\ApiProperty;
24+
use ApiPlatform\Metadata\ApiResource;
25+
use ApiPlatform\Metadata\Link;
26+
use PrestaShopBundle\ApiPlatform\Metadata\PaginatedList;
27+
28+
#[ApiResource(
29+
operations: [
30+
new PaginatedList(
31+
uriTemplate: '/attributes/group/{attributeGroupId}/attributes',
32+
scopes: [
33+
'attribute_read',
34+
],
35+
uriVariables: [
36+
'attributeGroupId' => new Link(
37+
identifiers: ['attributeGroupId']
38+
),
39+
],
40+
ApiResourceMapping: self::MAPPING,
41+
gridDataFactory: 'prestashop.core.grid.data.factory.attribute_decorator',
42+
filtersMapping: [
43+
'[attributeId]' => '[id_attribute]',
44+
],
45+
),
46+
]
47+
)]
48+
class AttributeList
49+
{
50+
#[ApiProperty(identifier: true)]
51+
public int $attributeId;
52+
53+
#[ApiProperty(readable: false, writable: false)]
54+
public int $attributeGroupId;
55+
56+
public string $name;
57+
58+
public int $values;
59+
60+
public int $position;
61+
62+
public const MAPPING = [
63+
'[id_attribute]' => '[attributeId]',
64+
];
65+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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\Attribute;
22+
23+
use ApiPlatform\Metadata\ApiProperty;
24+
use ApiPlatform\Metadata\ApiResource;
25+
use PrestaShop\PrestaShop\Core\Domain\AttributeGroup\Attribute\Command\BulkDeleteAttributeCommand;
26+
use PrestaShop\PrestaShop\Core\Domain\AttributeGroup\Attribute\Exception\AttributeNotFoundException;
27+
use PrestaShopBundle\ApiPlatform\Metadata\CQRSUpdate;
28+
use Symfony\Component\HttpFoundation\Response;
29+
use Symfony\Component\Validator\Constraints as Assert;
30+
31+
#[ApiResource(
32+
operations: [
33+
new CQRSUpdate(
34+
uriTemplate: '/attributes/attributes/delete',
35+
// No output 204 code
36+
output: false,
37+
CQRSCommand: BulkDeleteAttributeCommand::class,
38+
scopes: [
39+
'attribute_write',
40+
],
41+
),
42+
],
43+
exceptionToStatus: [
44+
AttributeNotFoundException::class => Response::HTTP_NOT_FOUND,
45+
],
46+
)]
47+
class BulkAttribute
48+
{
49+
/**
50+
* @var int[]
51+
*/
52+
#[ApiProperty(openapiContext: ['type' => 'array', 'items' => ['type' => 'integer'], 'example' => [1, 3]])]
53+
#[Assert\NotBlank]
54+
public array $attributeIds;
55+
}

0 commit comments

Comments
 (0)