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.
Merge pull request #41 from Stepa4man/ASYNC_IMPORT-37
ASYNC-IMPORT:37 Implement DB operation for save source endpoints
- Loading branch information
Showing
6 changed files
with
300 additions
and
10 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
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,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
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); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
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,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); | ||
} | ||
} |
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
168 changes: 168 additions & 0 deletions
168
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,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; | ||
} | ||
} |
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