Skip to content

Commit 533bd7e

Browse files
authored
Merge pull request PrestaShop#90 from Codencode/feature-value-endpoints
Feature value endpoints
2 parents 9cc1e0c + 5d313dc commit 533bd7e

4 files changed

Lines changed: 444 additions & 0 deletions

File tree

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\Feature;
22+
23+
use ApiPlatform\Metadata\ApiProperty;
24+
use ApiPlatform\Metadata\ApiResource;
25+
use PrestaShop\PrestaShop\Core\Domain\Feature\Command\BulkDeleteFeatureValueCommand;
26+
use PrestaShop\PrestaShop\Core\Domain\Feature\Exception\FeatureValueNotFoundException;
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: '/features/values/batch',
35+
output: false,
36+
CQRSCommand: BulkDeleteFeatureValueCommand::class,
37+
scopes: [
38+
'feature_value_write',
39+
],
40+
),
41+
],
42+
exceptionToStatus: [
43+
FeatureValueNotFoundException::class => Response::HTTP_NOT_FOUND,
44+
],
45+
)]
46+
class BulkFeatureValues
47+
{
48+
/**
49+
* @var int[]
50+
*/
51+
#[ApiProperty(openapiContext: ['type' => 'array', 'items' => ['type' => 'integer'], 'example' => [1, 3]])]
52+
#[Assert\NotBlank]
53+
public array $featureValueIds;
54+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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\Feature;
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\Feature\Command\AddFeatureValueCommand;
28+
use PrestaShop\PrestaShop\Core\Domain\Feature\Command\DeleteFeatureValueCommand;
29+
use PrestaShop\PrestaShop\Core\Domain\Feature\Command\EditFeatureValueCommand;
30+
use PrestaShop\PrestaShop\Core\Domain\Feature\Exception\FeatureValueConstraintException;
31+
use PrestaShop\PrestaShop\Core\Domain\Feature\Exception\FeatureValueNotFoundException;
32+
use PrestaShop\PrestaShop\Core\Domain\Feature\Query\GetFeatureValueForEditing;
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: '/features/value/{featureValueId}',
45+
CQRSQuery: GetFeatureValueForEditing::class,
46+
scopes: [
47+
'feature_value_read',
48+
],
49+
CQRSQueryMapping: self::QUERY_MAPPING,
50+
),
51+
new CQRSCreate(
52+
uriTemplate: '/features/value',
53+
validationContext: ['groups' => ['Default', 'Create']],
54+
CQRSCommand: AddFeatureValueCommand::class,
55+
CQRSQuery: GetFeatureValueForEditing::class,
56+
scopes: [
57+
'feature_value_write',
58+
],
59+
CQRSQueryMapping: self::QUERY_MAPPING,
60+
CQRSCommandMapping: self::COMMAND_MAPPING,
61+
),
62+
new CQRSPartialUpdate(
63+
uriTemplate: '/features/value/{featureValueId}',
64+
validationContext: ['groups' => ['Default', 'Update']],
65+
CQRSCommand: EditFeatureValueCommand::class,
66+
CQRSQuery: GetFeatureValueForEditing::class,
67+
scopes: [
68+
'feature_value_write',
69+
],
70+
CQRSQueryMapping: self::QUERY_MAPPING,
71+
CQRSCommandMapping: self::COMMAND_MAPPING,
72+
),
73+
new CQRSDelete(
74+
uriTemplate: '/features/value/{featureValueId}',
75+
CQRSCommand: DeleteFeatureValueCommand::class,
76+
scopes: [
77+
'feature_value_write',
78+
],
79+
),
80+
],
81+
exceptionToStatus: [
82+
FeatureValueConstraintException::class => Response::HTTP_UNPROCESSABLE_ENTITY,
83+
FeatureValueNotFoundException::class => Response::HTTP_NOT_FOUND,
84+
],
85+
)]
86+
class FeatureValue
87+
{
88+
#[ApiProperty(identifier: true)]
89+
public int $featureValueId;
90+
91+
#[ApiProperty(openapiContext: ['type' => 'integer', 'example' => 1])]
92+
public int $featureId;
93+
94+
#[LocalizedValue]
95+
#[DefaultLanguage(groups: ['Create'], fieldName: 'values')]
96+
#[DefaultLanguage(groups: ['Update'], fieldName: 'values', allowNull: true)]
97+
#[Assert\All(constraints: [
98+
new TypedRegex([
99+
'type' => TypedRegex::TYPE_CATALOG_NAME,
100+
]),
101+
])]
102+
public array $values;
103+
104+
public int $position;
105+
106+
public const QUERY_MAPPING = [
107+
'[value]' => '[values]',
108+
'[localizedValues]' => '[values]',
109+
];
110+
111+
public const COMMAND_MAPPING = [
112+
'[values]' => '[localizedValues]',
113+
];
114+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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\Feature;
22+
23+
use ApiPlatform\Metadata\ApiProperty;
24+
use ApiPlatform\Metadata\ApiResource;
25+
use ApiPlatform\Metadata\Link;
26+
use PrestaShop\PrestaShop\Core\Search\Filters\FeatureValueFilters;
27+
use PrestaShopBundle\ApiPlatform\Metadata\PaginatedList;
28+
29+
#[ApiResource(
30+
operations: [
31+
new PaginatedList(
32+
uriTemplate: '/features/{featureId}/values',
33+
scopes: [
34+
'feature_value_read',
35+
],
36+
uriVariables: [
37+
'featureId' => new Link(
38+
identifiers: ['featureId']
39+
),
40+
],
41+
ApiResourceMapping: self::MAPPING,
42+
gridDataFactory: 'prestashop.core.grid.data.factory.feature_value',
43+
filtersClass: FeatureValueFilters::class,
44+
filtersMapping: [
45+
'[featureValueId]' => '[id_feature_value]',
46+
],
47+
),
48+
]
49+
)]
50+
class FeatureValueList
51+
{
52+
#[ApiProperty(identifier: true)]
53+
public int $featureValueId;
54+
55+
#[ApiProperty(readable: false, writable: false)]
56+
public int $featureId;
57+
58+
public string $value;
59+
60+
public int $position;
61+
62+
public const MAPPING = [
63+
'[id_feature_value]' => '[featureValueId]',
64+
'[value]' => '[value]',
65+
'[position]' => '[position]',
66+
];
67+
}

0 commit comments

Comments
 (0)