forked from magedevel/async-import
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ASYNC-IMPORT:37 Implement DB operation for save source endpoints
- Loading branch information
Showing
6 changed files
with
274 additions
and
2 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
app/code/Magento/ImportService/Api/SourceRepositoryInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?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 | ||
{ | ||
/** | ||
* @param \Magento\ImportService\Api\Data\SourceInterface $source | ||
* @return \Magento\ImportService\Api\Data\SourceInterface | ||
*/ | ||
public function save(SourceInterface $source); | ||
|
||
/** | ||
* @param int $id | ||
* @return \Magento\ImportService\Api\Data\SourceInterface | ||
*/ | ||
public function getById($id); | ||
|
||
/** | ||
* @param \Magento\Framework\Api\SearchCriteriaInterface $criteria | ||
* @return \Magento\Framework\Api\SearchResultsInterface | ||
*/ | ||
public function getList(\Magento\Framework\Api\SearchCriteriaInterface $criteria); | ||
|
||
/** | ||
* @param \Magento\ImportService\Api\Data\SourceInterface $source | ||
* @return bool | ||
*/ | ||
public function delete(\Magento\ImportService\Api\Data\SourceInterface $source); | ||
|
||
/** | ||
* @param int $id | ||
* @return bool | ||
*/ | ||
public function deleteById($id); | ||
} |
24 changes: 24 additions & 0 deletions
24
app/code/Magento/ImportService/Model/ResourceModel/Source.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
app/code/Magento/ImportService/Model/ResourceModel/Source/Collection.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?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 | ||
{ | ||
protected function _construct() | ||
{ | ||
$this->_init(Source::class, SourceResourceModel::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
app/code/Magento/ImportService/Model/SourceRepository.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
<?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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters