Skip to content

Commit 69a83b0

Browse files
authored
Merge pull request #31 from landofcoder/feature9-1
Create Frontend REST API for the extension
2 parents 6601ff0 + 5ed1f3e commit 69a83b0

14 files changed

+482
-48
lines changed

Api/Data/TagInterface.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
11
<?php
2+
/**
3+
* Landofcoder
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Landofcoder.com license that is
8+
* available through the world-wide-web at this URL:
9+
* http://landofcoder.com/license
10+
*
11+
* DISCLAIMER
12+
*
13+
* Do not edit or add to this file if you wish to upgrade this extension to newer
14+
* version in the future.
15+
*
16+
* @category Landofcoder
17+
* @package Lof_ProductTags
18+
* @copyright Copyright (c) 2019 Landofcoder (http://www.landofcoder.com/)
19+
* @license http://www.landofcoder.com/LICENSE-1.0.html
20+
*/
221
namespace Lof\ProductTags\Api\Data;
322

423
interface TagInterface extends \Magento\Framework\Api\ExtensibleDataInterface

Api/Data/TagProductLinkInterface.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Lof\ProductTags\Api\Data;
8+
9+
use Magento\Framework\Api\ExtensibleDataInterface;
10+
11+
/**
12+
* @api
13+
* @since 100.0.2
14+
*/
15+
interface TagProductLinkInterface extends ExtensibleDataInterface
16+
{
17+
/**
18+
* @return string|null
19+
*/
20+
public function getSku();
21+
22+
/**
23+
* @param string $sku
24+
* @return $this
25+
*/
26+
public function setSku($sku);
27+
28+
/**
29+
* @return int|null
30+
*/
31+
public function getPosition();
32+
33+
/**
34+
* @param int $position
35+
* @return $this
36+
*/
37+
public function setPosition($position);
38+
39+
/**
40+
* Get tag id
41+
*
42+
* @return string
43+
*/
44+
public function getTagId();
45+
46+
/**
47+
* Set tag id
48+
*
49+
* @param string $tagId
50+
* @return $this
51+
*/
52+
public function setTagId($tagId);
53+
54+
/**
55+
* Retrieve existing extension attributes object.
56+
*
57+
* @return \Lof\ProductTags\Api\Data\TagProductLinkExtensionInterface|null
58+
*/
59+
public function getExtensionAttributes();
60+
61+
/**
62+
* Set an extension attributes object.
63+
*
64+
* @param \Lof\ProductTags\Api\Data\TagProductLinkExtensionInterface $extensionAttributes
65+
* @return $this
66+
*/
67+
public function setExtensionAttributes(
68+
\Lof\ProductTags\Api\Data\TagProductLinkExtensionInterface $extensionAttributes
69+
);
70+
}

Api/Data/TagSearchResultsInterface.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
<?php
22
/**
3-
* Copyright (c) 2019 Landofcoder
3+
* Landofcoder
44
*
5-
* Permission is hereby granted, free of charge, to any person obtaining a copy
6-
* of this software and associated documentation files (the "Software"), to deal
7-
* in the Software without restriction, including without limitation the rights
8-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
* copies of the Software, and to permit persons to whom the Software is
10-
* furnished to do so, subject to the following conditions:
5+
* NOTICE OF LICENSE
116
*
12-
* The above copyright notice and this permission notice shall be included in all
13-
* copies or substantial portions of the Software.
7+
* This source file is subject to the Landofcoder.com license that is
8+
* available through the world-wide-web at this URL:
9+
* http://landofcoder.com/license
1410
*
15-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
* SOFTWARE.
11+
* DISCLAIMER
12+
*
13+
* Do not edit or add to this file if you wish to upgrade this extension to newer
14+
* version in the future.
15+
*
16+
* @category Landofcoder
17+
* @package Lof_ProductTags
18+
* @copyright Copyright (c) 2019 Landofcoder (http://www.landofcoder.com/)
19+
* @license http://www.landofcoder.com/LICENSE-1.0.html
2220
*/
2321

2422
namespace Lof\ProductTags\Api\Data;

Api/ProductsManagementInterface.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ interface ProductsManagementInterface
2727
{
2828

2929
/**
30-
* GET for products api
31-
* @param string[] $param
32-
* @return string[]
30+
* Get List Product by Identyfier
31+
* @param string $tagCode
32+
* @return \Lof\ProductTags\Api\Data\TagProductLinkInterface[]|false
3333
*/
34-
public function getProducts($param);
34+
public function getProducts($tagCode);
3535
}

Api/TagsManagementInterface.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,18 @@
2323

2424
namespace Lof\ProductTags\Api;
2525

26+
use Magento\Framework\Api\SearchCriteriaInterface;
27+
2628
interface TagsManagementInterface
2729
{
2830

2931
/**
30-
* GET for tags api
31-
* @param string $param
32-
* @return string
32+
* Retrieve Tag matching the specified criteria.
33+
* @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
34+
* @return \Lof\ProductTags\Api\Data\TagSearchResultsInterface
35+
* @throws \Magento\Framework\Exception\LocalizedException
3336
*/
34-
public function getTags($param);
37+
public function getTags(
38+
\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
39+
);
3540
}

Model/Data/Tag.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
<?php
22
/**
3-
* Copyright (c) 2019 Landofcoder
3+
* Landofcoder
44
*
5-
* Permission is hereby granted, free of charge, to any person obtaining a copy
6-
* of this software and associated documentation files (the "Software"), to deal
7-
* in the Software without restriction, including without limitation the rights
8-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
* copies of the Software, and to permit persons to whom the Software is
10-
* furnished to do so, subject to the following conditions:
5+
* NOTICE OF LICENSE
116
*
12-
* The above copyright notice and this permission notice shall be included in all
13-
* copies or substantial portions of the Software.
7+
* This source file is subject to the Landofcoder.com license that is
8+
* available through the world-wide-web at this URL:
9+
* http://landofcoder.com/license
1410
*
15-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
* SOFTWARE.
11+
* DISCLAIMER
12+
*
13+
* Do not edit or add to this file if you wish to upgrade this extension to newer
14+
* version in the future.
15+
*
16+
* @category Landofcoder
17+
* @package Lof_ProductTags
18+
* @copyright Copyright (c) 2019 Landofcoder (http://www.landofcoder.com/)
19+
* @license http://www.landofcoder.com/LICENSE-1.0.html
2220
*/
2321

2422
namespace Lof\ProductTags\Model\Data;

Model/Data/TagProductLink.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Lof\ProductTags\Model\Data;
8+
9+
/**
10+
* @codeCoverageIgnore
11+
*/
12+
class TagProductLink extends \Magento\Framework\Api\AbstractExtensibleObject implements
13+
\Lof\ProductTags\Api\Data\TagProductLinkInterface
14+
{
15+
/**#@+
16+
* Constant for confirmation status
17+
*/
18+
const KEY_SKU = 'sku';
19+
const KEY_POSITION = 'position';
20+
const KEY_TAG_ID = 'tag_id';
21+
/**#@-*/
22+
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function getSku()
27+
{
28+
return $this->_get(self::KEY_SKU);
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public function getPosition()
35+
{
36+
return $this->_get(self::KEY_POSITION);
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function getTagId()
43+
{
44+
return $this->_get(self::KEY_TAG_ID);
45+
}
46+
47+
/**
48+
* @param string $sku
49+
* @return $this
50+
*/
51+
public function setSku($sku)
52+
{
53+
return $this->setData(self::KEY_SKU, $sku);
54+
}
55+
56+
/**
57+
* @param int $position
58+
* @return $this
59+
*/
60+
public function setPosition($position)
61+
{
62+
return $this->setData(self::KEY_POSITION, $position);
63+
}
64+
65+
/**
66+
* Set category id
67+
*
68+
* @param string $tagId
69+
* @return $this
70+
*/
71+
public function setTagId($tagId)
72+
{
73+
return $this->setData(self::KEY_TAG_ID, $tagId);
74+
}
75+
76+
/**
77+
* {@inheritdoc}
78+
*
79+
* @return \Lof\ProductTags\Api\Data\TagProductLinkExtensionInterface|null
80+
*/
81+
public function getExtensionAttributes()
82+
{
83+
return $this->_getExtensionAttributes();
84+
}
85+
86+
/**
87+
* {@inheritdoc}
88+
*
89+
* @param \Lof\ProductTags\Api\Data\TagProductLinkExtensionInterface $extensionAttributes
90+
* @return $this
91+
*/
92+
public function setExtensionAttributes(
93+
\Lof\ProductTags\Api\Data\TagProductLinkExtensionInterface $extensionAttributes
94+
) {
95+
return $this->_setExtensionAttributes($extensionAttributes);
96+
}
97+
}

Model/ProductsManagement.php

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,89 @@
2323

2424
namespace Lof\ProductTags\Model;
2525

26+
use Lof\ProductTags\Api\TagsManagementInterface;
27+
use Lof\ProductTags\Api\Data\TagSearchResultsInterfaceFactory;
28+
use Lof\ProductTags\Api\Data\TagInterfaceFactory;
29+
use Lof\ProductTags\Model\ResourceModel\Tag as ResourceTag;
30+
use Lof\ProductTags\Model\ResourceModel\Tag\CollectionFactory as TagCollectionFactory;
31+
2632
class ProductsManagement implements \Lof\ProductTags\Api\ProductsManagementInterface
2733
{
34+
protected $resource;
35+
36+
protected $tagFactory;
37+
38+
protected $tagCollectionFactory;
39+
40+
protected $searchResultsFactory;
41+
42+
protected $dataTagFactory;
43+
44+
/**
45+
* @var \Lof\ProductTags\Api\Data\TagProductLinkInterfaceFactory
46+
*/
47+
protected $productLinkFactory;
48+
49+
/**
50+
* @param ResourceTag $resource
51+
* @param TagFactory $tagFactory
52+
* @param TagInterfaceFactory $dataTagFactory
53+
* @param TagCollectionFactory $tagCollectionFactory
54+
* @param \Lof\ProductTags\Model\TagFactory $tagModelFactory
55+
* @param TagSearchResultsInterfaceFactory $searchResultsFactory
56+
* @param \Lof\ProductTags\Api\Data\TagProductLinkInterfaceFactory $productLinkFactory
57+
*/
58+
59+
public function __construct(
60+
ResourceTag $resource,
61+
TagFactory $tagFactory,
62+
TagInterfaceFactory $dataTagFactory,
63+
TagCollectionFactory $tagCollectionFactory,
64+
\Lof\ProductTags\Model\TagFactory $tagModelFactory,
65+
TagSearchResultsInterfaceFactory $searchResultsFactory,
66+
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
67+
\Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
68+
\Lof\ProductTags\Api\Data\TagProductLinkInterfaceFactory $productLinkFactory
69+
) {
70+
$this->resource = $resource;
71+
$this->tagFactory = $tagFactory;
72+
$this->_productCollectionFactory = $productCollectionFactory;
73+
$this->_catalogProductVisibility = $catalogProductVisibility;
74+
$this->tagCollectionFactory = $tagCollectionFactory;
75+
$this->searchResultsFactory = $searchResultsFactory;
76+
$this->dataTagFactory = $dataTagFactory;
77+
$this->_tagModelFactory = $tagModelFactory;
78+
$this->productLinkFactory = $productLinkFactory;
79+
}
2880

2981
/**
3082
* {@inheritdoc}
3183
*/
32-
public function getProducts($param)
84+
public function getProducts($tagCode)
3385
{
34-
return 'hello api GET return the $param ' . $param;
86+
$tagModel = $this->_tagModelFactory->create();
87+
$tagModel->loadByIdentifier($tagCode);
88+
if (!$tagModel->getId()) {
89+
return [];
90+
}
91+
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $products */
92+
$products = $tagModel->getProductCollection();
93+
/** @var \Lof\ProductTags\Api\Data\TagProductLinkInterface[] $links */
94+
$links = [];
95+
if($products){
96+
/** @var \Magento\Catalog\Model\Product $product */
97+
foreach ($products->getItems() as $product) {
98+
/** @var \Lof\ProductTags\Api\Data\TagProductLinkInterface $link */
99+
$link = $this->productLinkFactory->create();
100+
$link->setSku($product->getSku())
101+
->setPosition($product->getData('tag_index_position'))
102+
->setTagId($tagModel->getId());
103+
$links[] = $link;
104+
}
105+
}
106+
if($links){
107+
return $links;
108+
}
109+
return false;
35110
}
36111
}

0 commit comments

Comments
 (0)