Skip to content

Commit abb90cf

Browse files
authored
Merge pull request #69 from kpodemski/search-alias
SearchAlias resource
2 parents 97f3e63 + 4dfa045 commit abb90cf

File tree

4 files changed

+677
-0
lines changed

4 files changed

+677
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/**
4+
* Copyright since 2007 PrestaShop SA and Contributors
5+
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
6+
*
7+
* NOTICE OF LICENSE
8+
*
9+
* This source file is subject to the Academic Free License version 3.0
10+
* that is bundled with this package in the file LICENSE.md.
11+
* It is also available through the world-wide-web at this URL:
12+
* https://opensource.org/licenses/AFL-3.0
13+
* If you did not receive a copy of the license and are unable to
14+
* obtain it through the world-wide-web, please send an email
15+
* to [email protected] so we can send you a copy immediately.
16+
*
17+
* @author PrestaShop SA and Contributors <[email protected]>
18+
* @copyright Since 2007 PrestaShop SA and Contributors
19+
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
20+
*/
21+
22+
declare(strict_types=1);
23+
24+
namespace PrestaShop\Module\APIResources\ApiPlatform\Resources\SearchAlias;
25+
26+
use ApiPlatform\Metadata\ApiProperty;
27+
use ApiPlatform\Metadata\ApiResource;
28+
use PrestaShop\PrestaShop\Core\Domain\Alias\Command\BulkDeleteSearchTermsAliasesCommand;
29+
use PrestaShop\PrestaShop\Core\Domain\Alias\Exception\AliasNotFoundException;
30+
use PrestaShopBundle\ApiPlatform\Metadata\CQRSDelete;
31+
use Symfony\Component\HttpFoundation\Response;
32+
33+
#[ApiResource(
34+
operations: [
35+
new CQRSDelete(
36+
uriTemplate: '/search-aliases/delete',
37+
CQRSCommand: BulkDeleteSearchTermsAliasesCommand::class,
38+
scopes: ['search_alias_write'],
39+
CQRSCommandMapping: [
40+
'[searchTerms]' => '[searchTerms]',
41+
],
42+
),
43+
],
44+
exceptionToStatus: [
45+
AliasNotFoundException::class => Response::HTTP_NOT_FOUND,
46+
],
47+
)]
48+
class BulkSearchAliasesDelete
49+
{
50+
#[ApiProperty(
51+
openapiContext: [
52+
'type' => 'array',
53+
'items' => ['type' => 'string'],
54+
]
55+
)]
56+
public array $searchTerms = [];
57+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
3+
/**
4+
* Copyright since 2007 PrestaShop SA and Contributors
5+
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
6+
*
7+
* NOTICE OF LICENSE
8+
*
9+
* This source file is subject to the Academic Free License version 3.0
10+
* that is bundled with this package in the file LICENSE.md.
11+
* It is also available through the world-wide-web at this URL:
12+
* https://opensource.org/licenses/AFL-3.0
13+
* If you did not receive a copy of the license and are unable to
14+
* obtain it through the world-wide-web, please send an email
15+
* to [email protected] so we can send you a copy immediately.
16+
*
17+
* @author PrestaShop SA and Contributors <[email protected]>
18+
* @copyright Since 2007 PrestaShop SA and Contributors
19+
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
20+
*/
21+
22+
declare(strict_types=1);
23+
24+
namespace PrestaShop\Module\APIResources\ApiPlatform\Resources\SearchAlias;
25+
26+
use ApiPlatform\Metadata\ApiProperty;
27+
use ApiPlatform\Metadata\ApiResource;
28+
use PrestaShop\PrestaShop\Core\Domain\Alias\Command\AddSearchTermAliasesCommand;
29+
use PrestaShop\PrestaShop\Core\Domain\Alias\Command\DeleteSearchTermAliasesCommand;
30+
use PrestaShop\PrestaShop\Core\Domain\Alias\Command\UpdateSearchTermAliasesCommand;
31+
use PrestaShop\PrestaShop\Core\Domain\Alias\Exception\AliasConstraintException;
32+
use PrestaShop\PrestaShop\Core\Domain\Alias\Exception\AliasNotFoundException;
33+
use PrestaShop\PrestaShop\Core\Domain\Alias\Query\GetAliasesBySearchTermForEditing;
34+
use PrestaShopBundle\ApiPlatform\Metadata\CQRSCreate;
35+
use PrestaShopBundle\ApiPlatform\Metadata\CQRSDelete;
36+
use PrestaShopBundle\ApiPlatform\Metadata\CQRSGet;
37+
use PrestaShopBundle\ApiPlatform\Metadata\CQRSUpdate;
38+
use Symfony\Component\HttpFoundation\Response;
39+
use Symfony\Component\Validator\Constraints as Assert;
40+
41+
#[ApiResource(
42+
operations: [
43+
new CQRSGet(
44+
uriTemplate: '/search-alias/{search}',
45+
CQRSQuery: GetAliasesBySearchTermForEditing::class,
46+
scopes: ['search_alias_read'],
47+
exceptionToStatus: [AliasNotFoundException::class => 404],
48+
CQRSQueryMapping: self::QUERY_MAPPING,
49+
),
50+
new CQRSCreate(
51+
uriTemplate: '/search-alias',
52+
validationContext: ['groups' => ['Default', 'Create']],
53+
CQRSCommand: AddSearchTermAliasesCommand::class,
54+
scopes: ['search_alias_write'],
55+
CQRSCommandMapping: self::COMMAND_MAPPING,
56+
),
57+
new CQRSUpdate(
58+
uriTemplate: '/search-alias/{search}',
59+
CQRSCommand: UpdateSearchTermAliasesCommand::class,
60+
CQRSQuery: GetAliasesBySearchTermForEditing::class,
61+
scopes: ['search_alias_write'],
62+
CQRSCommandMapping: self::UPDATE_COMMAND_MAPPING,
63+
CQRSQueryMapping: self::QUERY_MAPPING,
64+
),
65+
new CQRSDelete(
66+
uriTemplate: '/search-alias/{search}',
67+
CQRSCommand: DeleteSearchTermAliasesCommand::class,
68+
scopes: ['search_alias_write'],
69+
CQRSCommandMapping: self::DELETE_COMMAND_MAPPING,
70+
output: false,
71+
),
72+
],
73+
exceptionToStatus: [
74+
AliasNotFoundException::class => Response::HTTP_NOT_FOUND,
75+
AliasConstraintException::class => Response::HTTP_UNPROCESSABLE_ENTITY,
76+
],
77+
)]
78+
class SearchAlias
79+
{
80+
#[ApiProperty(identifier: true)]
81+
#[Assert\NotBlank(groups: ['Create'])]
82+
#[Assert\Length(min: 1, max: 255)]
83+
public string $search = '';
84+
85+
#[Assert\NotBlank(groups: ['Create'])]
86+
#[Assert\Count(min: 1, groups: ['Create'])]
87+
#[Assert\All(
88+
constraints: [
89+
new Assert\Collection(
90+
fields: [
91+
'alias' => new Assert\NotBlank(groups: ['Default', 'Create']),
92+
'active' => new Assert\Type(type: 'bool', groups: ['Default', 'Create']),
93+
],
94+
allowMissingFields: false,
95+
groups: ['Default', 'Create']
96+
),
97+
],
98+
groups: ['Default', 'Create']
99+
)]
100+
#[ApiProperty(
101+
openapiContext: [
102+
'type' => 'array',
103+
'items' => [
104+
'type' => 'object',
105+
'properties' => [
106+
'id_alias' => ['type' => 'integer'],
107+
'alias' => ['type' => 'string'],
108+
'active' => ['type' => 'boolean'],
109+
],
110+
],
111+
]
112+
)]
113+
public array $aliases = [];
114+
115+
protected const QUERY_MAPPING = [
116+
'[search]' => '[searchTerm]',
117+
'[searchTerm]' => '[search]',
118+
];
119+
120+
protected const COMMAND_MAPPING = [
121+
'[search]' => '[searchTerm]',
122+
'[aliases]' => '[aliases]',
123+
];
124+
125+
protected const UPDATE_COMMAND_MAPPING = [
126+
'[search]' => '[oldSearchTerm]',
127+
'[aliases]' => '[aliases]',
128+
];
129+
130+
protected const DELETE_COMMAND_MAPPING = [
131+
'[search]' => '[searchTerm]',
132+
];
133+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/**
4+
* Copyright since 2007 PrestaShop SA and Contributors
5+
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
6+
*
7+
* NOTICE OF LICENSE
8+
*
9+
* This source file is subject to the Academic Free License version 3.0
10+
* that is bundled with this package in the file LICENSE.md.
11+
* It is also available through the world-wide-web at this URL:
12+
* https://opensource.org/licenses/AFL-3.0
13+
* If you did not receive a copy of the license and are unable to
14+
* obtain it through the world-wide-web, please send an email
15+
* to [email protected] so we can send you a copy immediately.
16+
*
17+
* @author PrestaShop SA and Contributors <[email protected]>
18+
* @copyright Since 2007 PrestaShop SA and Contributors
19+
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
20+
*/
21+
22+
declare(strict_types=1);
23+
24+
namespace PrestaShop\Module\APIResources\ApiPlatform\Resources\SearchAlias;
25+
26+
use ApiPlatform\Metadata\ApiProperty;
27+
use ApiPlatform\Metadata\ApiResource;
28+
use PrestaShopBundle\ApiPlatform\Metadata\PaginatedList;
29+
use PrestaShopBundle\ApiPlatform\Provider\QueryListProvider;
30+
31+
#[ApiResource(
32+
operations: [
33+
new PaginatedList(
34+
uriTemplate: '/search-aliases',
35+
provider: QueryListProvider::class,
36+
scopes: ['search_alias_read'],
37+
gridDataFactory: 'prestashop.core.grid.data_provider.alias_decorator',
38+
),
39+
],
40+
)]
41+
class SearchAliasList
42+
{
43+
#[ApiProperty(identifier: true)]
44+
public string $search = '';
45+
46+
#[ApiProperty(
47+
openapiContext: [
48+
'type' => 'array',
49+
'items' => [
50+
'type' => 'object',
51+
'properties' => [
52+
'id_alias' => ['type' => 'integer'],
53+
'alias' => ['type' => 'string'],
54+
'active' => ['type' => 'boolean'],
55+
],
56+
],
57+
]
58+
)]
59+
public array $aliases = [];
60+
}

0 commit comments

Comments
 (0)