Skip to content

Commit

Permalink
Merge pull request #20 from jolelievre/module-endpoint
Browse files Browse the repository at this point in the history
Add module GET endpoint and bulk PUT for status
  • Loading branch information
jolelievre authored Mar 27, 2024
2 parents a71fc39 + 959b5ff commit 255bb95
Show file tree
Hide file tree
Showing 6 changed files with 428 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config/admin/services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:
_defaults:
public: false
autowire: true

PrestaShop\Module\APIResources\List\ModuleQueryBuilder:
parent: 'prestashop.core.grid.abstract_query_builder'
autowire: true
58 changes: 58 additions & 0 deletions src/ApiPlatform/Resources/Module/BulkModules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

declare(strict_types=1);

namespace PrestaShop\Module\APIResources\ApiPlatform\Resources\Module;

use ApiPlatform\Metadata\ApiResource;
use PrestaShop\PrestaShop\Core\Domain\Module\Command\BulkToggleModuleStatusCommand;
use PrestaShopBundle\ApiPlatform\Metadata\CQRSUpdate;

#[ApiResource(
operations: [
new CQRSUpdate(
uriTemplate: '/modules/toggle-status',
output: false,
CQRSCommand: BulkToggleModuleStatusCommand::class,
scopes: [
'module_write',
],
CQRSCommandMapping: [
'[enabled]' => '[expectedStatus]',
],
),
],
)]
class BulkModules
{
/**
* @var string[]
*/
public array $modules;

public bool $enabled;
}
66 changes: 66 additions & 0 deletions src/ApiPlatform/Resources/Module/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

declare(strict_types=1);

namespace PrestaShop\Module\APIResources\ApiPlatform\Resources\Module;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use PrestaShop\Module\APIResources\List\ModuleQueryBuilder;
use PrestaShop\PrestaShop\Core\Domain\Module\Query\GetModuleInfos;
use PrestaShopBundle\ApiPlatform\Metadata\CQRSGet;
use PrestaShopBundle\ApiPlatform\Metadata\DQBPaginatedList;

#[ApiResource(
operations: [
new CQRSGet(
uriTemplate: '/module/{moduleId}',
CQRSQuery: GetModuleInfos::class,
scopes: [
'module_read',
],
),
new DQBPaginatedList(
uriTemplate: '/modules',
scopes: [
'module_read',
],
queryBuilder: ModuleQueryBuilder::class,
),
],
)]
class Module
{
#[ApiProperty(identifier: true)]
public int $moduleId;

public string $technicalName;

public string $version;

public bool $enabled;
}
97 changes: 97 additions & 0 deletions src/List/ModuleQueryBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

declare(strict_types=1);

namespace PrestaShop\Module\APIResources\List;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Query\QueryBuilder;
use PrestaShop\PrestaShop\Core\Grid\Query\AbstractDoctrineQueryBuilder;
use PrestaShop\PrestaShop\Core\Grid\Query\DoctrineSearchCriteriaApplicatorInterface;
use PrestaShop\PrestaShop\Core\Grid\Search\SearchCriteriaInterface;

class ModuleQueryBuilder extends AbstractDoctrineQueryBuilder
{
public function __construct(
Connection $connection,
string $dbPrefix,
private readonly DoctrineSearchCriteriaApplicatorInterface $searchCriteriaApplicator
) {
parent::__construct($connection, $dbPrefix);
}

public function getSearchQueryBuilder(SearchCriteriaInterface $searchCriteria)
{
$builder = $this->getModuleQueryBuilder($searchCriteria)
->select('m.id_module AS moduleId, m.name AS technicalName, m.active AS enabled, m.version');

$this->searchCriteriaApplicator
->applySorting($searchCriteria, $builder)
->applyPagination($searchCriteria, $builder);

return $builder;
}

public function getCountQueryBuilder(SearchCriteriaInterface $searchCriteria)
{
return $this->getModuleQueryBuilder($searchCriteria)->select('COUNT(id_module)');
}

private function getModuleQueryBuilder(SearchCriteriaInterface $searchCriteria): QueryBuilder
{
$qb = $this->connection
->createQueryBuilder()
->from($this->dbPrefix . 'module', 'm')
;

$allowedFilters = [
'moduleId' => 'id_module',
'technicalName' => 'name',
'enabled' => 'active',
'version' => 'version',
];

foreach ($searchCriteria->getFilters() as $filterName => $filterValue) {
if (!array_key_exists($filterName, $allowedFilters)) {
continue;
}

$columnName = $allowedFilters[$filterName];
if (in_array($filterName, ['moduleId', 'enabled'])) {
$qb->andWhere($columnName . ' = :' . $filterName);
$qb->setParameter($filterName, $filterValue);

continue;
}

$qb->andWhere($columnName . ' LIKE :' . $filterName);
$qb->setParameter($filterName, '%' . $filterValue . '%');
}

return $qb;
}
}
24 changes: 24 additions & 0 deletions tests/Integration/ApiPlatform/ApiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,30 @@ protected function getBearerToken(array $scopes = []): string
return json_decode($response->getContent())->access_token;
}

protected function listItems(string $listUrl, array $scopes = [], array $filters = []): array
{
$bearerToken = $this->getBearerToken($scopes);
$response = static::createClient()->request('GET', $listUrl, [
'auth_bearer' => $bearerToken,
'extra' => [
'parameters' => [
'filters' => $filters,
],
],
]);
self::assertResponseStatusCodeSame(200);

$decodedResponse = json_decode($response->getContent(), true);
$this->assertNotFalse($decodedResponse);
$this->assertArrayHasKey('totalItems', $decodedResponse);
$this->assertArrayHasKey('sortOrder', $decodedResponse);
$this->assertArrayHasKey('limit', $decodedResponse);
$this->assertArrayHasKey('filters', $decodedResponse);
$this->assertArrayHasKey('items', $decodedResponse);

return $decodedResponse;
}

protected static function createApiClient(array $scopes = [], int $lifetime = 10000): void
{
$command = new AddApiClientCommand(
Expand Down
Loading

0 comments on commit 255bb95

Please sign in to comment.