Question

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>
Was it helpful?

Solution

no , the paths are like for interface and model should be like :
app\code\EternalParquet\ProductByEbayItemid\Api\ProductByEbayItemidInterface
and app\code\EternalParquet\ProductByEbayItemid\Model\ProductByEbayItemidQuery.
you should specify parameters before the function

/**
     * Returns greeting message to user
     *
     * @api
     * @param mixed $ebayitemid ID.
     * @return string Greeting message with users name.
     */

OTHER TIPS

Call the interface and model in the webapi.xml in the below format

<route url="/V1/account/getproductid" method="GET">
        <service class="EternalParquet\ProductByEbayItemid\Api\ProductByEbayItemidInterface" method="getProductid"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>

and in di.xml

 <preference for="EternalParquet\ProductByEbayItemid\Api\ProductByEbayItemidInterface" type="Mymodule\ProductByEbayItemid\Model\ProductByEbayItemidQuery" />

the method and parameters should be same in API interface and model

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top