diff --git a/app/code/Magento/Review/Api/Data/ReviewInterface.php b/app/code/Magento/Review/Api/Data/ReviewInterface.php new file mode 100644 index 0000000000000..c2ab728c58c9f --- /dev/null +++ b/app/code/Magento/Review/Api/Data/ReviewInterface.php @@ -0,0 +1,126 @@ +reviewFactory = $reviewFactory; + $this->collectionFactory = $collectionFactory; + $this->searchResultsFactory = $searchResultsInterface; + $this->searchCriteriaFactory = $searchCriteriaBuilderFactory; + } + + /** + * {@inheritdoc} + */ + public function get($reviewId) + { + /** @var \Magento\Review\Model\Review $review */ + $review = $this->reviewFactory->create() + ->load($reviewId); + + if (!$review->getId()) { + throw new NoSuchEntityException(__("Requested Review doesn't exist")); + } + return $review; + } + + /** + * {@inheritdoc} + */ + public function save(\Magento\Review\Api\Data\ReviewInterface $review) + { + /** @var \Magento\Review\Model\Review $review */ + $validationResult = $review->validate(); + if (true !== $validationResult) { + throw new \Magento\Framework\Exception\CouldNotSaveException( + __('Invalid review data: %1', implode(',', $validationResult)) + ); + } + $review->setEntityId(1); // product + + $review->save(); + $review->aggregate(); + return $review; + } + + /** + * {@inheritdoc} + */ + public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria) + { + $collection = $this->collectionFactory->create(); + foreach ($searchCriteria->getFilterGroups() as $filterGroup) { + $this->addFilterGroupToCollection($filterGroup, $collection); + } + + /** @var \Magento\Framework\Api\SortOrder $sortOrder */ + foreach ((array)$searchCriteria->getSortOrders() as $sortOrder) { + $field = $sortOrder->getField(); + $collection->addOrder( + $field, + ($sortOrder->getDirection() == \Magento\Framework\Api\SortOrder::SORT_ASC) ? 'ASC' : 'DESC' + ); + } + $collection->setCurPage($searchCriteria->getCurrentPage()); + $collection->setPageSize($searchCriteria->getPageSize()); + $collection->load(); + + $searchResult = $this->searchResultsFactory->create(); + $searchResult->setSearchCriteria($searchCriteria); + $searchResult->setItems($collection->getItems()); + $searchResult->setTotalCount($collection->getSize()); + + return $searchResult; + } + + /** + * {@inheritdoc} + */ + public function delete($reviewId) + { + $review = $this->reviewFactory->create() + ->load($reviewId); + + if (!$review->getId()) { + throw new NoSuchEntityException(__("Requested Review doesn't exist")); + } + + $review->delete(); + + return $review; + } + + /** + * Helper function that adds a FilterGroup to the collection. + * + * @param \Magento\Framework\Api\Search\FilterGroup $filterGroup + * @param \Magento\Review\Model\ResourceModel\Review\Collection $collection + * @return void + */ + protected function addFilterGroupToCollection( + \Magento\Framework\Api\Search\FilterGroup $filterGroup, + \Magento\Review\Model\ResourceModel\Review\Collection $collection + ) { + $fields = []; + $productFilter = []; + foreach ($filterGroup->getFilters() as $filter) { + $conditionType = $filter->getConditionType() ? $filter->getConditionType() : 'eq'; + + if ($filter->getField() == 'product_id') { + $productFilter[$conditionType][] = $filter->getValue(); + continue; + } + $fields[] = ['attribute' => $filter->getField(), $conditionType => $filter->getValue()]; + } + + if ($productFilter) { + $collection->addEntityFilter('product', $productFilter); + } + + if ($fields) { + $collection->addFieldToFilter($fields); + } + } +} diff --git a/app/code/Magento/Review/Model/Review.php b/app/code/Magento/Review/Model/Review.php index 5b9ab07dead65..3c1a383dd62ad 100644 --- a/app/code/Magento/Review/Model/Review.php +++ b/app/code/Magento/Review/Model/Review.php @@ -7,22 +7,24 @@ use Magento\Catalog\Model\Product; use Magento\Framework\DataObject\IdentityInterface; +use Magento\Framework\Exception\NoSuchEntityException; use Magento\Review\Model\ResourceModel\Review\Product\Collection as ProductCollection; use Magento\Review\Model\ResourceModel\Review\Status\Collection as StatusCollection; +use Magento\Review\Api\Data\ReviewInterface; /** * Review model * * @method string getCreatedAt() - * @method \Magento\Review\Model\Review setCreatedAt(string $value) - * @method \Magento\Review\Model\Review setEntityId(int $value) + * @method \Magento\Review\Model\Review setCreatedAt($date) + * @method \Magento\Review\Model\Review setEntityId($entityId) * @method int getEntityPkValue() - * @method \Magento\Review\Model\Review setEntityPkValue(int $value) + * @method \Magento\Review\Model\Review setEntityPkValue($entityPkValue) * @method int getStatusId() - * @method \Magento\Review\Model\Review setStatusId(int $value) + * @method \Magento\Review\Model\Review setStatusId($statusId) * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ -class Review extends \Magento\Framework\Model\AbstractModel implements IdentityInterface +class Review extends \Magento\Framework\Model\AbstractModel implements IdentityInterface, ReviewInterface { /** * Event prefix for observer @@ -138,6 +140,7 @@ public function __construct( \Magento\Review\Model\ResourceModel\Review\Summary\CollectionFactory $summaryFactory, \Magento\Review\Model\Review\SummaryFactory $summaryModFactory, \Magento\Review\Model\Review\Summary $reviewSummary, + \Magento\Review\Model\Review\StatusFactory $reviewStatusFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\UrlInterface $urlModel, \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null, @@ -149,6 +152,7 @@ public function __construct( $this->_summaryFactory = $summaryFactory; $this->_summaryModFactory = $summaryModFactory; $this->_reviewSummary = $reviewSummary; + $this->_reviewStatusFactory = $reviewStatusFactory; $this->_storeManager = $storeManager; $this->_urlModel = $urlModel; parent::__construct($context, $registry, $resource, $resourceCollection, $data); @@ -379,4 +383,151 @@ public function getIdentities() } return $tags; } + + /** + * {@inheritdoc} + */ + public function getReviewId() + { + return $this->_getData('review_id'); + } + + /** + * {@inheritdoc} + */ + public function setReviewId($reviewId) + { + $this->setData('review_id', $reviewId); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getDetailId() + { + return $this->_getData('detail_id'); + } + + /** + * {@inheritdoc} + */ + public function setDetailId($detailId) + { + $this->setData('detail_id', $detailId); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getTitle() + { + return $this->_getData('title'); + } + + /** + * {@inheritdoc} + */ + public function setTitle($title) + { + $this->setData('title', $title); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getDetail() + { + return $this->_getData('detail'); + } + + /** + * {@inheritdoc} + */ + public function setDetail($detail) + { + $this->setData('detail', $detail); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getNickname() + { + return $this->_getData('nickname'); + } + + /** + * {@inheritdoc} + */ + public function setNickname($nickname) + { + $this->setData('nickname', $nickname); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getCustomerId() + { + return $this->_getData('customer_id'); + } + + /** + * {@inheritdoc} + */ + public function setCustomerId($customerId) + { + $this->setData('customer_id', $customerId); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getEntityCode() + { + return $this->_getData('entity_code'); + } + + /** + * {@inheritdoc} + */ + public function setEntityCode($entityCode) + { + $this->setData('entity_code', $entityCode); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getStatus() + { + /** @var \Magento\Review\Model\Review\Status $status */ + $status = $this->_reviewStatusFactory->create(); + $status->load($this->getStatusId()); + + return $status->getStatusCode(); + } + + /** + * {@inheritdoc} + */ + public function setStatus($statusCode) + { + $status = $this->_statusFactory->create() + ->addFieldToFilter('status_code', $statusCode) + ->getFirstItem(); + if (!$status->getId()) { + throw new NoSuchEntityException(__("Status doesn't exist")); + } + $this->setStatusId($status->getId()); + + return $this; + } } diff --git a/app/code/Magento/Review/etc/di.xml b/app/code/Magento/Review/etc/di.xml index d42d3e3330c67..9f9d30d80cffb 100644 --- a/app/code/Magento/Review/etc/di.xml +++ b/app/code/Magento/Review/etc/di.xml @@ -7,6 +7,10 @@ --> + + + + diff --git a/app/code/Magento/Review/etc/webapi.xml b/app/code/Magento/Review/etc/webapi.xml new file mode 100644 index 0000000000000..574130988282d --- /dev/null +++ b/app/code/Magento/Review/etc/webapi.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +