Skip to content

Commit

Permalink
Merge pull request #41 from Stepa4man/ASYNC_IMPORT-37
Browse files Browse the repository at this point in the history
ASYNC-IMPORT:37 Implement DB operation for save source endpoints
  • Loading branch information
nuzil authored Feb 12, 2019
2 parents d65da77 + fb20539 commit 7d150f1
Show file tree
Hide file tree
Showing 6 changed files with 300 additions and 10 deletions.
56 changes: 56 additions & 0 deletions app/code/Magento/ImportService/Api/SourceRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\ImportService\Api;

use Magento\ImportService\Api\Data\SourceInterface;

/**
* Interface SourceRepositoryInterface
*/
interface SourceRepositoryInterface
{
/**
* Saves source
*
* @param \Magento\ImportService\Api\Data\SourceInterface $source
* @return \Magento\ImportService\Api\Data\SourceInterface
*/
public function save(SourceInterface $source);

/**
* Provides source by ID
*
* @param int $id
* @return \Magento\ImportService\Api\Data\SourceInterface
*/
public function getById($id);

/**
* Provides sources which match a specific criteria.
*
* @param \Magento\Framework\Api\SearchCriteriaInterface $criteria
* @return \Magento\Framework\Api\SearchResultsInterface
*/
public function getList(\Magento\Framework\Api\SearchCriteriaInterface $criteria);

/**
* Deletes source
*
* @param \Magento\ImportService\Api\Data\SourceInterface $source
* @return bool
*/
public function delete(\Magento\ImportService\Api\Data\SourceInterface $source);

/**
* Deletes source by ID
*
* @param int $id
* @return bool
*/
public function deleteById($id);
}
24 changes: 24 additions & 0 deletions app/code/Magento/ImportService/Model/ResourceModel/Source.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\ImportService\Model\ResourceModel;

use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
use Magento\ImportService\Api\Data\SourceInterface;

/**
* Class SourceResourceModel
*/
class Source extends AbstractDb
{
const TABLE_NAME = 'import_service_source';

protected function _construct()
{
$this->_init(self::TABLE_NAME, SourceInterface::ENTITY_ID);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\ImportService\Model\ResourceModel\Source;

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
use Magento\ImportService\Model\ResourceModel\Source as SourceResourceModel;
use Magento\ImportService\Model\Source;

/**
* Collection of Import Service Sources
*/
class Collection extends AbstractCollection
{
/**
* Source collection constructor
*/
protected function _construct()
{
$this->_init(Source::class, SourceResourceModel::class);
}
}
32 changes: 24 additions & 8 deletions app/code/Magento/ImportService/Model/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,37 @@
namespace Magento\ImportService\Model;

use Magento\Framework\Model\AbstractModel;
use Magento\ImportService\Api\Data\SourceExtensionInterface;
use Magento\ImportService\Api\Data\SourceInterface;
use Magento\ImportService\Model\ResourceModel\Source as SourceResource;

/**
* Class Source
*/
class Source extends AbstractModel implements SourceInterface
{
const CACHE_TAG = 'magento_import_service_source';

/**
* @return int
* Source constructor
*/
protected function _construct()
{
$this->_init(SourceResource::class);
}

/**
* Get unique page cache identities
*
* @return array
*/
public function getIdentities()
{
return [self::CACHE_TAG . '_' . $this->getId()];
}

/**
* @inheritDoc
*/
public function getSourceId()
{
Expand Down Expand Up @@ -97,9 +118,7 @@ public function getCreatedAt()
}

/**
* {@inheritdoc}
*
* @return \Magento\ImportService\Api\Data\SourceExtensionInterface|null
* @inheritdoc
*/
public function getExtensionAttributes()
{
Expand All @@ -108,11 +127,8 @@ public function getExtensionAttributes()

/**
* {@inheritdoc}
*
* @param \Magento\ImportService\Api\Data\SourceExtensionInterface $extensionAttributes
* @return $this
*/
public function setExtensionAttributes(\Magento\ImportService\Api\Data\SourceExtensionInterface $extensionAttributes)
public function setExtensionAttributes(SourceExtensionInterface $extensionAttributes)
{
return $this->_setExtensionAttributes($extensionAttributes);
}
Expand Down
168 changes: 168 additions & 0 deletions app/code/Magento/ImportService/Model/SourceRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\ImportService\Model;

use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Api\SearchResultsInterfaceFactory;
use Magento\Framework\Api\SearchResultsInterface;
use Magento\Framework\Api\SortOrder;
use Magento\Framework\Exception\CouldNotDeleteException;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Model\AbstractModel;
use Magento\ImportService\Api\Data\SourceInterface;
use Magento\ImportService\Api\SourceRepositoryInterface;
use Magento\ImportService\Model\ResourceModel\Source as SourceResourceModel;
use Magento\ImportService\Model\ResourceModel\Source\CollectionFactory as SourceCollectionFactory;

/**
* Class SourceRepository
*/
class SourceRepository implements SourceRepositoryInterface
{
/**
* @var SourceFactory
*/
private $sourceFactory;

/**
* @var SourceResourceModel
*/
private $sourceResourceModel;

/**
* @var SourceCollectionFactory
*/
private $sourceCollectionFactory;

/**
* @var SearchResultsInterfaceFactory
*/
private $searchResultsFactory;

/**
* @param SourceFactory $sourceFactory
* @param SourceResourceModel $sourceResourceModel
* @param SourceCollectionFactory $sourceCollectionFactory
* @param SearchResultsInterfaceFactory $searchResultsFactory
*/
public function __construct(
SourceFactory $sourceFactory,
SourceResourceModel $sourceResourceModel,
SourceCollectionFactory $sourceCollectionFactory,
SearchResultsInterfaceFactory $searchResultsFactory
) {
$this->sourceFactory = $sourceFactory;
$this->sourceResourceModel = $sourceResourceModel;
$this->sourceCollectionFactory = $sourceCollectionFactory;
$this->searchResultsFactory = $searchResultsFactory;
}

/**
* @inheritdoc
*
* @throws CouldNotSaveException
*/
public function save(SourceInterface $source)
{
try {
$this->sourceResourceModel->save($source);
} catch (\Exception $e) {
throw new CouldNotSaveException(__($e->getMessage()));
}

return $source;
}

/**
* @inheritdoc
*
* @throws NoSuchEntityException
*/
public function getById($id)
{
$source = $this->sourceFactory->create();
$this->sourceResourceModel->load($source, $id);
if (!$source->getId()) {
throw new NoSuchEntityException(__('Source with id "%1" does not exist.', $id));
}

return $source;
}

/**
* @inheritdoc
*
* @throws CouldNotDeleteException
*/
public function delete(SourceInterface $source)
{
try {
/** @var AbstractModel|SourceInterface $source */
$this->sourceResourceModel->delete($source);
} catch (\Exception $exception) {
throw new CouldNotDeleteException(__($exception->getMessage()));
}

return true;
}

/**
* @inheritdoc
*
* @throws CouldNotDeleteException
* @throws NoSuchEntityException
*/
public function deleteById($id)
{
return $this->delete($this->getById($id));
}

/**
* @inheritdoc
*/
public function getList(SearchCriteriaInterface $criteria)
{
/** @var SearchResultsInterface $searchResults */
$searchResults = $this->searchResultsFactory->create();
$searchResults->setSearchCriteria($criteria);
$sourceCollection = $this->sourceCollectionFactory->create();
foreach ($criteria->getFilterGroups() as $filterGroup) {
$fields = [];
$conditions = [];
foreach ($filterGroup->getFilters() as $filter) {
$condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
$fields[] = $filter->getField();
$conditions[] = [$condition => $filter->getValue()];
}
if ($fields) {
$sourceCollection->addFieldToFilter($fields, $conditions);
}
}
$searchResults->setTotalCount($sourceCollection->getSize());
$sortOrders = $criteria->getSortOrders();
if ($sortOrders) {
/** @var SortOrder $sortOrder */
foreach ($sortOrders as $sortOrder) {
$sourceCollection->addOrder(
$sortOrder->getField(),
($sortOrder->getDirection() == SortOrder::SORT_ASC) ? 'ASC' : 'DESC'
);
}
}
$sourceCollection->setCurPage($criteria->getCurrentPage());
$sourceCollection->setPageSize($criteria->getPageSize());
$sources = [];
foreach ($sourceCollection as $sourceModel) {
$sources[] = $sourceModel;
}
$searchResults->setItems($sources);

return $searchResults;
}
}
4 changes: 2 additions & 2 deletions app/code/Magento/ImportService/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
<preference for="Magento\ImportService\Api\SourceUploadInterface" type="Magento\ImportService\Model\SourceUpload" />
<preference for="Magento\ImportService\Api\Data\SourceInterface" type="Magento\ImportService\Model\Source" />
<preference for="Magento\ImportService\Api\Data\SourceUploadResponseInterface" type="Magento\ImportService\Model\SourceUploadResponse" />

<preference for="Magento\ImportService\Api\SourceRepositoryInterface"
type="Magento\ImportService\Model\SourceRepository"/>
<type name="Magento\ImportService\Model\Import\SourceProcessorPool">
<arguments>
<argument name="sourceProcessors" xsi:type="array">
Expand All @@ -21,5 +22,4 @@
</argument>
</arguments>
</type>

</config>

0 comments on commit 7d150f1

Please sign in to comment.