Commit 0ecefe50 authored by Chính's avatar Chính

Merge branch 'feature/product_api_interface' into 'master'

Create API Interface to get price by product id

See merge request !2
parents 81b91d06 c70f7339
<?php
namespace Plugilo\Plugilo\Api;
interface ProductRepositoryInterface
{
/**
* Return a filtered product.
*
* @param int $id
* @return \Plugilo\Plugilo\Api\ResponseItemInterface
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getItem(int $id);
}
<?php
namespace Plugilo\Plugilo\Api;
interface ResponseItemInterface
{
const DATA_ID = 'id';
const DATA_PRICE = 'price';
/**
* @return int
*/
public function getId();
/**
* @return float
*/
public function getPrice();
/**
* @param int $id
* @return $this
*/
public function setId(int $id);
/**
* @param float $price
* @return $this
*/
public function setPrice(float $price);
}
<?php
namespace Plugilo\Plugilo\Model\Api;
use Plugilo\Plugilo\Api\ProductRepositoryInterface;
use Plugilo\Plugilo\Api\ResponseItemInterfaceFactory;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Framework\Exception\NoSuchEntityException;
/**
* Class ProductRepository
*/
class ProductRepository implements ProductRepositoryInterface
{
/**
* @var CollectionFactory
*/
private $productCollectionFactory;
/**
* @var ResponseItemInterfaceFactory
*/
private $responseItemFactory;
/**
* @param CollectionFactory $productCollectionFactory
* @param ResponseItemInterfaceFactory $responseItemFactory
*/
public function __construct(
CollectionFactory $productCollectionFactory,
ResponseItemInterfaceFactory $responseItemFactory
) {
$this->productCollectionFactory = $productCollectionFactory;
$this->responseItemFactory = $responseItemFactory;
}
/**
* {@inheritDoc}
*
* @param int $id
* @return ResponseItemInterface
* @throws NoSuchEntityException
*/
public function getItem(int $id): mixed
{
$collection = $this->getProductCollection()
->addAttributeToFilter('entity_id', ['eq' => $id]);
/** @var ProductInterface $product */
$product = $collection->getFirstItem();
if (!$product->getId()) {
throw new NoSuchEntityException(__('Product not found'));
}
return $this->getResponseItemFromProduct($product);
}
/**
* @return Collection
*/
private function getProductCollection(): mixed
{
/** @var Collection $collection */
$collection = $this->productCollectionFactory->create();
$collection
->addAttributeToSelect(
[
'entity_id',
ProductInterface::PRICE,
]
);
return $collection;
}
/**
* @param ProductInterface $product
* @return ResponseItemInterface
*/
private function getResponseItemFromProduct(ProductInterface $product): mixed
{
/** @var ResponseItemInterface $responseItem */
$responseItem = $this->responseItemFactory->create();
$responseItem->setId($product->getId())->setPrice($product->getPrice());
return $responseItem;
}
}
<?php
namespace Plugilo\Plugilo\Model\Api;
use Plugilo\Plugilo\Api\ResponseItemInterface;
use Magento\Framework\DataObject;
use Symfony\Component\Config\Definition\FloatNode;
/**
* Class ResponseItem
*/
class ResponseItem extends DataObject implements ResponseItemInterface
{
public function getId(): int
{
return $this->_getData(self::DATA_ID);
}
public function getPrice(): float
{
return $this->_getData(self::DATA_PRICE);
}
/**
* @param int $id
* @return $this
*/
public function setId(int $id): mixed
{
return $this->setData(self::DATA_ID, $id);
}
/**
* @param float $price
* @return $this
*/
public function setPrice(float $price): mixed
{
return $this->setData(self::DATA_PRICE, $price);
}
}
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Plugilo\Plugilo\Api\ProductRepositoryInterface" type="Plugilo\Plugilo\Model\Api\ProductRepository"/>
<preference for="Plugilo\Plugilo\Api\ResponseItemInterface" type="Plugilo\Plugilo\Model\Api\ResponseItem"/>
</config>
<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route url="/V1/plugilo/getProduct/:id" method="GET">
<service class="Plugilo\Plugilo\Api\ProductRepositoryInterface" method="getItem" />
<resources>
<resource ref="anonymous" />
</resources>
</route>
</routes>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment