Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the Single Delete Command for Source Items #140

Merged
merged 5 commits into from
Nov 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Inventory\Model\ResourceModel\SourceItem;

Expand Down Expand Up @@ -41,19 +42,33 @@ public function execute(array $sourceItems)
if (!count($sourceItems)) {
return;
}

$connection = $this->resourceConnection->getConnection();
$tableName = $this->resourceConnection->getTableName(SourceItemResourceModel::TABLE_NAME_SOURCE_ITEM);

$skuList = [];
foreach ($sourceItems as $sourceItem) {
$skuList[] = $sourceItem->getSku();
}
$whereSql = $this->buildWhereSqlPart($sourceItems);
$connection->delete($tableName, $whereSql);
}

$whereCond = [
$connection->quoteInto(SourceItemInterface::SKU . ' IN(?)', array_unique($skuList))
];
/**
* @param array $sourceItems
* @return string
*/
private function buildWhereSqlPart(array $sourceItems): string
{
$connection = $this->resourceConnection->getConnection();

$connection->delete($tableName, $whereCond);
$condition = [];
foreach ($sourceItems as $sourceItem) {
$skuCondition = $connection->quoteInto(
SourceItemInterface::SKU . ' = ?',
$sourceItem->getSku()
);
$sourceIdCondition = $connection->quoteInto(
SourceItemInterface::SOURCE_ID . ' = ?',
$sourceItem->getSourceId()
);
$condition[] = '(' . $skuCondition . ' AND ' . $sourceIdCondition . ')';
}
return implode(' OR ', $condition);
}
}
52 changes: 0 additions & 52 deletions app/code/Magento/Inventory/Model/SourceItem/Command/Delete.php

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function execute(array $sourceItems)
$this->deleteMultiple->execute($sourceItems);
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
throw new CouldNotDeleteException(__('Could not delete Source Item'), $e);
throw new CouldNotDeleteException(__('Could not delete Source Items'), $e);
}
}
}
18 changes: 0 additions & 18 deletions app/code/Magento/Inventory/Model/SourceItemRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
namespace Magento\Inventory\Model;

use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Inventory\Model\SourceItem\Command\DeleteInterface;
use Magento\Inventory\Model\SourceItem\Command\GetListInterface;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryApi\Api\Data\SourceItemSearchResultsInterface;
use Magento\InventoryApi\Api\SourceItemRepositoryInterface;

Expand All @@ -17,25 +15,17 @@
*/
class SourceItemRepository implements SourceItemRepositoryInterface
{
/**
* @var DeleteInterface
*/
private $commandDelete;

/**
* @var GetListInterface
*/
private $commandGetList;

/**
* @param DeleteInterface $commandDelete
* @param GetListInterface $commandGetList
*/
public function __construct(
DeleteInterface $commandDelete,
GetListInterface $commandGetList
) {
$this->commandDelete = $commandDelete;
$this->commandGetList = $commandGetList;
}

Expand All @@ -46,12 +36,4 @@ public function getList(SearchCriteriaInterface $searchCriteria): SourceItemSear
{
return $this->commandGetList->execute($searchCriteria);
}

/**
* @inheritdoc
*/
public function delete(SourceItemInterface $sourceItem)
{
$this->commandDelete->execute($sourceItem);
}
}
1 change: 0 additions & 1 deletion app/code/Magento/Inventory/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
<preference for="Magento\InventoryApi\Api\SourceItemsDeleteInterface" type="Magento\Inventory\Model\SourceItem\Command\SourceItemsDelete"/>
<preference for="Magento\InventoryApi\Api\Data\SourceItemInterface" type="Magento\Inventory\Model\SourceItem"/>
<preference for="Magento\InventoryApi\Api\Data\SourceItemSearchResultsInterface" type="Magento\Inventory\Model\SourceItemSearchResults"/>
<preference for="Magento\Inventory\Model\SourceItem\Command\DeleteInterface" type="Magento\Inventory\Model\SourceItem\Command\Delete"/>
<preference for="Magento\Inventory\Model\SourceItem\Command\GetListInterface" type="Magento\Inventory\Model\SourceItem\Command\GetList"/>
<type name="Magento\Inventory\Model\SourceItem\Validator\ValidatorChain">
<arguments>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
namespace Magento\InventoryApi\Api;

use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryApi\Api\Data\SourceItemSearchResultsInterface;

/**
Expand Down Expand Up @@ -43,13 +42,4 @@ interface SourceItemRepositoryInterface
* @return \Magento\InventoryApi\Api\Data\SourceItemSearchResultsInterface
*/
public function getList(SearchCriteriaInterface $searchCriteria): SourceItemSearchResultsInterface;

/**
* Delete SourceItem data
*
* @param \Magento\InventoryApi\Api\Data\SourceItemInterface $sourceItem
* @return void
* @throws \Magento\Framework\Exception\CouldNotDeleteException
*/
public function delete(SourceItemInterface $sourceItem);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

/**
* Service method for source items delete multiple
* Performance efficient API, used for stock synchronization
* Performance efficient API
*
* Used fully qualified namespaces in annotations for proper work of WebApi request parser
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

/**
* Service method for source items save multiple
* Performance efficient API, used for stock synchronization
* Performance efficient API
*
* Used fully qualified namespaces in annotations for proper work of WebApi request parser
*
Expand Down
107 changes: 107 additions & 0 deletions app/code/Magento/InventoryApi/Test/Api/SourceItemsDeleteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryApi\Test\Api;

use Magento\Framework\Api\SearchCriteria;
use Magento\Framework\Webapi\Rest\Request;
use Magento\Framework\Webapi\Exception;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\TestFramework\Assert\AssertArrayContains;
use Magento\TestFramework\TestCase\WebapiAbstract;

class SourceItemsDeleteTest extends WebapiAbstract
{
/**#@+
* Service constants
*/
const RESOURCE_PATH = '/V1/inventory/source-items';
const SERVICE_NAME = 'inventoryApiSourceItemsDeleteV1';
/**#@-*/

/**
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
*/
public function testExecute()
{
$sourceItemsForDelete = [
[
SourceItemInterface::SOURCE_ID => 10,
SourceItemInterface::SKU => 'SKU-1',
],
[
SourceItemInterface::SOURCE_ID => 20,
SourceItemInterface::SKU => 'SKU-1',
],
];
$expectedSourceItemsAfterDeleting = [
[
SourceItemInterface::SOURCE_ID => 30,
SourceItemInterface::SKU => 'SKU-1',
SourceItemInterface::QUANTITY => 10,
SourceItemInterface::STATUS => SourceItemInterface::STATUS_OUT_OF_STOCK,
],
[
SourceItemInterface::SOURCE_ID => 40,
SourceItemInterface::SKU => 'SKU-1',
SourceItemInterface::QUANTITY => 10,
SourceItemInterface::STATUS => SourceItemInterface::STATUS_IN_STOCK,
],
[
SourceItemInterface::SOURCE_ID => 50,
SourceItemInterface::SKU => 'SKU-2',
SourceItemInterface::QUANTITY => 5,
SourceItemInterface::STATUS => SourceItemInterface::STATUS_IN_STOCK,
],
];

$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . '?'
. http_build_query(['sourceItems' => $sourceItemsForDelete]),
'httpMethod' => Request::HTTP_METHOD_DELETE,
],
'soap' => [
'service' => self::SERVICE_NAME,
'operation' => self::SERVICE_NAME . 'Execute',
],
];
(TESTS_WEB_API_ADAPTER == self::ADAPTER_REST)
? $this->_webApiCall($serviceInfo)
: $this->_webApiCall($serviceInfo, ['sourceItems' => $sourceItemsForDelete]);

$actualData = $this->getSourceItems();

self::assertEquals(3, $actualData['total_count']);
AssertArrayContains::assert($expectedSourceItemsAfterDeleting, $actualData['items']);
}

/**
* @return array
*/
private function getSourceItems(): array
{
$requestData = [
'searchCriteria' => [SearchCriteria::PAGE_SIZE => 10],
];
$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . '?' . http_build_query($requestData),
'httpMethod' => Request::HTTP_METHOD_GET,
],
'soap' => [
'service' => 'inventoryApiSourceItemRepositoryV1',
'operation' => 'inventoryApiSourceItemRepositoryV1GetList',
],
];
return (TESTS_WEB_API_ADAPTER === self::ADAPTER_REST)
? $this->_webApiCall($serviceInfo)
: $this->_webApiCall($serviceInfo, $requestData);
}
}
12 changes: 12 additions & 0 deletions app/code/Magento/InventoryApi/etc/webapi.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,22 @@
</resources>
</route>
<!-- SourceItem -->
<route url="/V1/inventory/source-items" method="GET">
<service class="Magento\InventoryApi\Api\SourceItemRepositoryInterface" method="getList"/>
<resources>
<resource ref="Magento_InventoryApi::source"/>
</resources>
</route>
<route url="/V1/inventory/source-items" method="POST">
<service class="Magento\InventoryApi\Api\SourceItemsSaveInterface" method="execute"/>
<resources>
<resource ref="Magento_InventoryApi::source"/>
</resources>
</route>
<route url="/V1/inventory/source-items" method="DELETE">
<service class="Magento\InventoryApi\Api\SourceItemsDeleteInterface" method="execute"/>
<resources>
<resource ref="Magento_InventoryApi::source"/>
</resources>
</route>
</routes>
Loading