Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
plugilo
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chính
plugilo
Commits
c70f7339
Commit
c70f7339
authored
Sep 05, 2024
by
Chính
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create API Interface to get price by product id
parent
81b91d06
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
183 additions
and
0 deletions
+183
-0
Api/ProductRepositoryInterface.php
Api/ProductRepositoryInterface.php
+15
-0
Api/ResponseItemInterface.php
Api/ResponseItemInterface.php
+30
-0
Model/Api/ProductRepository.php
Model/Api/ProductRepository.php
+83
-0
Model/Api/ResponseItem.php
Model/Api/ResponseItem.php
+40
-0
etc/di.xml
etc/di.xml
+5
-0
etc/webapi.xml
etc/webapi.xml
+10
-0
No files found.
Api/ProductRepositoryInterface.php
0 → 100644
View file @
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
);
}
Api/ResponseItemInterface.php
0 → 100644
View file @
c70f7339
<?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
);
}
Model/Api/ProductRepository.php
0 → 100644
View file @
c70f7339
<?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
;
}
}
Model/Api/ResponseItem.php
0 → 100644
View file @
c70f7339
<?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
);
}
}
etc/di.xml
0 → 100644
View file @
c70f7339
<?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>
etc/webapi.xml
0 → 100644
View file @
c70f7339
<?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>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment