Create QuestionManagement class
-
01-03-2021 - |
Pregunta
I have made Question repository class:
namespace Vendor\Module\Model;
use Magento\Framework\Api\DataObjectHelper;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Api\Search\FilterGroup;
use Magento\Framework\Api\SortOrder;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\StateException;
use Magento\Framework\Exception\ValidatorException;
use Magento\Framework\Exception\NoSuchEntityException;
use Vendor\Module\Api\QuestionRepositoryInterface;
use Vendor\Module\Api\Data\QuestionInterface;
use Vendor\Module\Api\Data\QuestionInterfaceFactory;
use Vendor\Module\Api\Data\QuestionSearchResultsInterfaceFactory;
use Vendor\Module\Model\ResourceModel\Questions as ResourceData;
use Vendor\Module\Model\ResourceModel\Questions\CollectionFactory as QuestionCollectionFactory;
class QuestionRepository implements QuestionRepositoryInterface
{
protected $_instances = [];
protected $_resource;
protected $_questionCollectionFactory;
protected $_searchResultsFactory;
protected $_questionInterfaceFactory;
protected $_dataObjectHelper;
public function __construct(
ResourceData $resource,
QuestionCollectionFactory $questionCollectionFactory,
QuestionSearchResultsInterfaceFactory $questionSearchResultsInterfaceFactory,
QuestionInterfaceFactory $questionInterfaceFactory,
DataObjectHelper $dataObjectHelper
) {
$this->_resource = $resource;
$this->_questionCollectionFactory = $questionCollectionFactory;
$this->_searchResultsFactory = $questionSearchResultsInterfaceFactory;
$this->_questionInterfaceFactory = $questionInterfaceFactory;
$this->_dataObjectHelper = $dataObjectHelper;
}
/**
* @param QuestionInterface $data
* @return QuestionInterface
* @throws CouldNotSaveException
*/
public function save(QuestionInterface $data)
{
try {
/** @var QuestionInterface|\Magento\Framework\Model\AbstractModel $data */
$this->_resource->save($data);
} catch (\Exception $exception) {
throw new CouldNotSaveException(__(
'Could not save the data: %1',
$exception->getMessage()
));
}
return $data;
}
/**
* Get data record
*
* @param $dataId
* @return mixed
* @throws NoSuchEntityException
*/
public function getById($dataId)
{
if (!isset($this->_instances[$dataId])) {
/** @var \Magebit\Faq\Api\Data\QuestionInterface|\Magento\Framework\Model\AbstractModel $data */
$data = $this->_questionInterfaceFactory->create();
$this->_resource->load($data, $dataId);
if (!$data->getId()) {
throw new NoSuchEntityException(__('Requested data doesn\'t exist'));
}
$this->_instances[$dataId] = $data;
}
return $this->_instances[$dataId];
}
/**
* @param SearchCriteriaInterface $searchCriteria
* @return \Magebit\Faq\Api\Data\QuestionSearchResultsInterface
*/
public function getList(SearchCriteriaInterface $searchCriteria)
{
/** @var \Magebit\Faq\Api\Data\QuestionSearchResultsInterface $searchResults */
$searchResults = $this->_searchResultsFactory->create();
$searchResults->setSearchCriteria($searchCriteria);
/** @var \Magebit\Faq\Model\ResourceModel\Questions\Collection $collection */
$collection = $this->_questionCollectionFactory->create();
//Add filters from root filter group to the collection
/** @var FilterGroup $group */
foreach ($searchCriteria->getFilterGroups() as $group) {
$this->addFilterGroupToCollection($group, $collection);
}
$sortOrders = $searchCriteria->getSortOrders();
/** @var SortOrder $sortOrder */
if ($sortOrders) {
foreach ($searchCriteria->getSortOrders() as $sortOrder) {
$field = $sortOrder->getField();
$collection->addOrder(
$field,
($sortOrder->getDirection() == SortOrder::SORT_ASC) ? 'ASC' : 'DESC'
);
}
} else {
$field = 'data_id';
$collection->addOrder($field, 'ASC');
}
$collection->setCurPage($searchCriteria->getCurrentPage());
$collection->setPageSize($searchCriteria->getPageSize());
$data = [];
foreach ($collection as $datum) {
$dataDataObject = $this->_questionInterfaceFactory->create();
$this->_dataObjectHelper->populateWithArray($dataDataObject, $datum->getData(), QuestionInterface::class);
$data[] = $dataDataObject;
}
$searchResults->setTotalCount($collection->getSize());
return $searchResults->setItems($data);
}
/**
* @param QuestionInterface $data
* @return bool
* @throws CouldNotSaveException
* @throws StateException
*/
public function delete(QuestionInterface $data)
{
/** @var \Magebit\Faq\Api\Data\QuestionInterface|\Magento\Framework\Model\AbstractModel $data */
$id = $data->getId();
try {
unset($this->_instances[$id]);
$this->_resource->delete($data);
} catch (ValidatorException $e) {
throw new CouldNotSaveException(__($e->getMessage()));
} catch (\Exception $e) {
throw new StateException(
__('Unable to remove data %1', $id)
);
}
unset($this->_instances[$id]);
return true;
}
/**
* @param $dataId
* @return bool
* @throws CouldNotSaveException
* @throws NoSuchEntityException
* @throws StateException
*/
public function deleteById($dataId)
{
$data = $this->getById($dataId);
return $this->delete($data);
}
}
QuestionManagement.php
namespace Vendor\Module\Model;
use Vendor\Module\Api\QuestionManagementInterface;
use Vendor\Module\Api\QuestionRepositoryInterface;
class QuestionManagement implements QuestionManagementInterface
{
protected $questionRepository;
public function __construct
(
QuestionRepositoryInterface $questionRepository
)
{
$this->questionRepository = $questionRepository;
}
public function disableQuestion()
{
}
public function enableQuestion()
{
}
}
Can someone help me to write an implementation of disableQuestion() and enableQuestion() methods?
Solución
Try following way to enable/disable. Guess, question enable/disable column name is status.
<?php
namespace Vendor\Module\Model;
use Vendor\Module\Api\QuestionManagementInterface;
use Vendor\Module\Api\QuestionRepositoryInterface;
class QuestionManagement implements QuestionManagementInterface
{
protected $questionRepository;
public function __construct
(
QuestionRepositoryInterface $questionRepository
)
{
$this->questionRepository = $questionRepository;
}
public function disableQuestion($id)
{
$question = $this->questionRepository->getById($id);
$question->setStatus(0);
$this->questionRepository->save($question);
}
public function enableQuestion($id)
{
$question = $this->questionRepository->getById($id);
$question->setStatus(1);
$this->questionRepository->save($question);
}
}
Otros consejos
public function setQuestionStatus($question, $status)
{
//this allows you to pass an id or a question object to the method
//if the id is not found in the db, a NoSuchEntity exception will be twrown.
if (is_int($question)) {
$question = $this->questionRepository->getById($question);
}
$question->setIsActive($status); // method `setIsActive` may be different. depends on how your db field is called
$this->questionRepository->save($question);
}
public function disableQuestion($question)
{
$this->setQuestionStatus($question, 0);
}
public function enableQuestion()
{
$this->setQuestionStatus($question, 1);
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange