Skip to content

Commit 10abeff

Browse files
committed
update files
1 parent ec1a95f commit 10abeff

File tree

5 files changed

+154
-31
lines changed

5 files changed

+154
-31
lines changed
Lines changed: 76 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,93 @@
11
<?php
2-
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
37

48
namespace Lof\ProductTagsGraphQl\Model\Resolver\DataProvider;
59

10+
use Lof\ProductTags\Api\Data\TagProductLinkInterface;
11+
use Lof\ProductTags\Model\Data\TagProductLink;
12+
use Lof\ProductTags\Api\ProductsManagementInterface;
13+
use Magento\Framework\Exception\NoSuchEntityException;
14+
15+
/**
16+
* Lof Product Tags data provider
17+
*/
18+
619
class Product
720
{
21+
/**
22+
* @var ProductsManagementInterface
23+
*/
24+
private $productsManagement;
825

9-
private $tag;
26+
/**
27+
* @var \Lof\ProductTags\Api\Data\TagProductLinkInterfaceFactory
28+
*/
29+
protected $productLinkFactory;
1030

1131
/**
12-
* @param \Lof\ProductTags\Api\Data\TagInterface $tag
32+
* @param \Lof\ProductTags\Api\ProductsManagementInterface $productsManagement
33+
* @param \Lof\ProductTags\Model\TagFactory $tagModelFactory
34+
* @param \Lof\ProductTags\Api\Data\TagProductLinkInterfaceFactory $productLinkFactory
1335
*/
1436
public function __construct(
15-
\Lof\ProductTags\Api\Data\TagInterface $tag
37+
\Lof\ProductTags\Api\ProductsManagementInterface $productsManagement,
38+
\Lof\ProductTags\Model\TagFactory $tagModelFactory,
39+
\Lof\ProductTags\Api\Data\TagProductLinkInterfaceFactory $productLinkFactory
1640
) {
17-
$this->tag = $tag;
41+
$this->productsManagement = $productsManagement;
42+
$this->_tagModelFactory = $tagModelFactory;
43+
$this->productLinkFactory = $productLinkFactory;
1844
}
1945

20-
public function getProduct()
46+
/**
47+
* Get product data
48+
*
49+
* @param string $tagCode
50+
* @return array
51+
* @throws NoSuchEntityException
52+
*/
53+
public function getData(string $tagCode): array
2154
{
22-
return 'proviced data';
55+
$tagModel = $this->_tagModelFactory->create();
56+
$tagModel->loadByIdentifier($tagCode);
57+
if (!$tagModel->getId()) {
58+
return [];
59+
}
60+
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $products */
61+
$products = $tagModel->getProductCollection();
62+
/** @var \Lof\ProductTags\Api\Data\TagProductLinkInterface[] $productsData */
63+
$productsData = [];
64+
if($products){
65+
/** @var \Magento\Catalog\Model\Product $product */
66+
foreach ($products->getItems() as $product) {
67+
/** @var \Lof\ProductTags\Api\Data\TagProductLinkInterface $productData */
68+
$productData = $this->productLinkFactory->create();
69+
$productData->setSku($product->getSku())
70+
->setPosition($product->getData('tag_index_position'))
71+
->setTagId($tagModel->getId());
72+
$productData = [
73+
TagProductLinkInterface::KEY_SKU => $product->getSku(),
74+
TagProductLinkInterface::KEY_POSITION => $product->getPosition(),
75+
TagProductLinkInterface::KEY_TAG_ID => $product->getTagId(),
76+
];
77+
// $productData->setSku($product->getSku())
78+
// ->setPosition($product->getData('tag_index_position'))
79+
// ->setTagId($tagModel->getId());
80+
$productsData[] = $productData;
81+
return $productsData;
82+
}
83+
}
84+
// $product = $this->productsManagement->getProducts($tagCode);
85+
86+
// $productData = [
87+
// TagProductLinkInterface::KEY_SKU => $product->getSku(),
88+
// TagProductLinkInterface::KEY_POSITION => $product->getPosition(),
89+
// TagProductLinkInterface::KEY_TAG_ID => $product->getTagId(),
90+
// ];
91+
// return $productData;
2392
}
2493
}

Model/Resolver/DataProvider/Tag.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ public function __construct(
3131
}
3232

3333
/**
34-
* @param int $tagId
34+
* @param string $tagsId
3535
* @return array
3636
* @throws NoSuchEntityException
3737
*/
38-
public function getData(int $tagId): array
38+
public function getData(string $tagsId): array
3939
{
40-
$tag = $this->tagRepository->getById($tagId);
40+
$tag = $this->tagRepository->getById($tagsId);
4141

4242
if (false === $tag->getStatus()) {
4343
throw new NoSuchEntityException();

Model/Resolver/Product.php

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
<?php
2-
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
37

48
namespace Lof\ProductTagsGraphQl\Model\Resolver;
59

10+
use Lof\ProductTagsGraphQl\Model\Resolver\DataProvider\Product as ProductDataProvider;
611
use Magento\Framework\Exception\NoSuchEntityException;
712
use Magento\Framework\GraphQl\Config\Element\Field;
813
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
@@ -13,10 +18,13 @@
1318
class Product implements ResolverInterface
1419
{
1520

21+
/**
22+
* @var ProductDataProvider
23+
*/
1624
private $productDataProvider;
1725

1826
/**
19-
* @param DataProvider\Product $productRepository
27+
* @param DataProvider\Product $productDataProvider
2028
*/
2129
public function __construct(
2230
DataProvider\Product $productDataProvider
@@ -34,7 +42,45 @@ public function resolve(
3442
array $value = null,
3543
array $args = null
3644
) {
37-
$productData = $this->productDataProvider->getProduct();
45+
$tagCodes = $this->getProductIdentifiers($args);
46+
$productData = $this->getProductData($tagCodes);
47+
3848
return $productData;
3949
}
50+
51+
/**
52+
* Get Product identifiers
53+
*
54+
* @param array $args
55+
* @return string[]
56+
* @throws GraphQlInputException
57+
*/
58+
private function getProductIdentifiers(array $args): array
59+
{
60+
if (!isset($args['identifiers']) || !is_array($args['identifiers']) || count($args['identifiers']) === 0) {
61+
throw new GraphQlInputException(__('"identifiers" of Product Tag should be specified'));
62+
}
63+
64+
return $args['identifiers'];
65+
}
66+
67+
/**
68+
* Get Product data
69+
*
70+
* @param array $tagCodes
71+
* @return array
72+
* @throws GraphQlNoSuchEntityException
73+
*/
74+
private function getProductData(array $tagCodes): array
75+
{
76+
$productsData = [];
77+
foreach ($tagCodes as $tagCode) {
78+
try {
79+
$productsData[$tagCode] = $this->productDataProvider->getData($tagCode);
80+
} catch (NoSuchEntityException $e) {
81+
$productsData[$tagCode] = new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
82+
}
83+
}
84+
return $productsData;
85+
}
4086
}

Model/Resolver/Tag.php

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Tag implements ResolverInterface
2323
private $tagDataProvider;
2424

2525
/**
26-
* @param DataProvider\Tag $tagRepository
26+
* @param DataProvider\Tag $tagDataProvider
2727
*/
2828
public function __construct(DataProvider\Tag $tagDataProvider)
2929
{
@@ -40,38 +40,43 @@ public function resolve(
4040
array $value = null,
4141
array $args = null
4242
) {
43-
$tagId = $this->getTagId($args);
44-
$tagData = $this->getTagData($tagId);
45-
46-
return $tagData;
43+
$tagsId = $this->getTagId($args);
44+
$tagsData = $this->getTagData($tagsId);
45+
$resultData = [
46+
'items' => $tagsData,
47+
];
48+
return $resultData;
4749
}
4850

4951
/**
5052
* @param array $args
51-
* @return int
53+
* @return string[]
5254
* @throws GraphQlInputException
5355
*/
54-
private function getTagId(array $args): int
56+
private function getTagId(array $args): array
5557
{
5658
if (!isset($args['id'])) {
5759
throw new GraphQlInputException(__('"Tag id should be specified'));
5860
}
5961

60-
return (int)$args['id'];
62+
return (array)$args['id'];
6163
}
6264

6365
/**
64-
* @param int $tagId
66+
* @param array $tagsId
6567
* @return array
6668
* @throws GraphQlNoSuchEntityException
6769
*/
68-
private function getTagData(int $tagId): array
70+
private function getTagData(array $tagsId): array
6971
{
70-
try {
71-
$tagData = $this->tagDataProvider->getData($tagId);
72-
} catch (NoSuchEntityException $e) {
73-
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
72+
$tagsData = [];
73+
foreach ($tagsId as $tagId) {
74+
try {
75+
$tagsData[$tagId] = $this->tagDataProvider->getData($tagId);
76+
} catch (NoSuchEntityException $e) {
77+
$tagsData[$tagId] = new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
78+
}
7479
}
75-
return $tagData;
80+
return $tagsData;
7681
}
7782
}

etc/schema.graphqls

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
type Query {
33

44
tag (
5-
id: Int @doc(description: "Query by id.")
5+
id: [String] @doc(description: "Query by tag_id.")
66
) : Tag @resolver( class: "Lof\\ProductTagsGraphQl\\Model\\Resolver\\Tag") ,
77
product (
8-
product_id: Int @doc(description: "Query by product_id.")
8+
identifiers: [String] @doc(description: "Query by product_id.")
99
) : Product @resolver( class: "Lof\\ProductTagsGraphQl\\Model\\Resolver\\Product") ,
1010
}
11-
1211
type Tag {
12+
items: [TagsData] @doc(description: "An array information Tags.")
13+
}
14+
type TagsData {
1315
tag_id : Int @doc(description: "Query by tag_id.") ,
1416
status : Boolean @doc(description: "Query by status.") ,
1517
tag_title : String @doc(description: "Query by tag_title.") ,
@@ -22,6 +24,7 @@ type Tag {
2224

2325
type Product {
2426
tag_id : Int @doc(description: "Query by tag_id.") ,
27+
sku : String @doc(description: "Query by sku.") ,
2528
product_id : Int @doc(description: "Query by product_id.") ,
2629
position : Int @doc(description: "Query by position.") ,
2730
}

0 commit comments

Comments
 (0)