I have created a new web API module, this API should execute a direct SQL query.
This is the code:
namespace Mymodule\ProductByEbayItemid\Model\ProductByEbayItemidQuery;
class ProductByEbayItemidQuery {
protected $_resourceConnection;
protected $_connection;
public function __construct(
\Magento\Framework\App\ResourceConnection $resourceConnection
) {
$this->_resourceConnection = $resourceConnection;
}
public function getProductid($ebayitemid)
{
$this->_connection = $this->_resourceConnection->getConnection();
//Your custom sql query
$query = "SELECT product_id FROM etbrm2epro_ebay_item WHERE item_id=:item_id";
$binds = array ( 'item_id' => $ebayitemid );
$collection = $this->_connection->fetchOne( $query,$binds );
return $collection;
}
}
I don't know how to instance this class in the API class.
namespace EternalParquet\ProductByEbayItemid\Model;
use EternalParquet\ProductByEbayItemid\Api\ProductByEbayItemidInterface;
class ProductByEbayItemid implements ProductByEbayItemidInterface
{
/**
* Returns product data by ebay item id,
* if m2epro is not installed it return null.
* @ebayitemid ebay item id
* @api
* @return product data.
*/
public function get($ebayitemid) {
//?????
}
}
acl.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
<acl>
<resources>
<resource id="Magento_Backend::admin">
<resource id="EternalParquet_ProductByEbayItemid::productid" title="GetProductByEbayItemid" translate="title" sortOrder="110" />
</resource>
</resources>
</acl>
</config>
di.xml
<?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="EternalParquet\ProductByEbayItemid\Api\ProductByEbayItemidInterface"
type="EternalParquet\ProductByEbayItemid\Model\ProductByEbayItemid" />
</config>
webapi.xml
<?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/productByEbayItemid/productid/:ebayitemid" method="GET">
<service class="EternalParquet\ProductByEbayItemid\Api\ProductByEbayItemidInterface" method="productid"/>
<resources>
<resource ref="EternalParquet_ProductByEbayItemid::productid"/>
</resources>
</route>
</routes>