Skip to content

Commit 3ab2742

Browse files
authored
Merge pull request #3 from landofcoder/develop
GraphQl get ListTag
2 parents 7fae7ef + 0d3ef8e commit 3ab2742

File tree

11 files changed

+181
-37
lines changed

11 files changed

+181
-37
lines changed

Controller/GraphQl/View.php

100644100755
File mode changed.

Model/Resolver/DataProvider/Product.php

100644100755
Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,92 @@
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+
$productsData = [
73+
TagProductLinkInterface::KEY_SKU => $productData->getSku(),
74+
TagProductLinkInterface::KEY_POSITION => $productData->getPosition(),
75+
TagProductLinkInterface::KEY_TAG_ID => $productData->getTagId(),
76+
];
77+
// $productData->setSku($product->getSku())
78+
// ->setPosition($product->getData('tag_index_position'))
79+
// ->setTagId($tagModel->getId());
80+
}
81+
return $productsData;
82+
}
83+
// $product = $this->productsManagement->getProducts($tagCode);
84+
85+
// $productData = [
86+
// TagProductLinkInterface::KEY_SKU => $product->getSku(),
87+
// TagProductLinkInterface::KEY_POSITION => $product->getPosition(),
88+
// TagProductLinkInterface::KEY_TAG_ID => $product->getTagId(),
89+
// ];
90+
// return $productData;
2391
}
2492
}

Model/Resolver/DataProvider/Tag.php

100644100755
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,23 @@ class Tag
2323

2424
/**
2525
* @param \Lof\ProductTags\Api\TagRepositoryInterface $tagRepository
26+
* @param \Lof\ProductTags\Model\TagFactory $tagModelFactory
2627
*/
2728
public function __construct(
29+
\Lof\ProductTags\Model\TagFactory $tagModelFactory,
2830
\Lof\ProductTags\Api\TagRepositoryInterface $tagRepository
2931
) {
3032
$this->tagRepository = $tagRepository;
33+
$this->_tagModelFactory = $tagModelFactory;
3134
}
3235

3336
/**
34-
* @param int $tagId
37+
* @param Lof\ProductTags\Model\Tag $tag
3538
* @return array
3639
* @throws NoSuchEntityException
3740
*/
38-
public function getData(int $tagId): array
41+
public function getData( $tag): array
3942
{
40-
$tag = $this->tagRepository->getById($tagId);
41-
4243
if (false === $tag->getStatus()) {
4344
throw new NoSuchEntityException();
4445
}
@@ -49,6 +50,7 @@ public function getData(int $tagId): array
4950
TagInterface::TAG_TITLE => $tag->getTagTitle(),
5051
TagInterface::TAG_IDENTIFIER => $tag->getIdentifier(),
5152
TagInterface::TAG_DESCRIPTION => $tag->getTagDescription(),
53+
TagInterface::STORE_ID => $tag->getStoreId(),
5254
];
5355
return $tagData;
5456
}

Model/Resolver/Product.php

100644100755
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

100644100755
Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Lof\ProductTagsGraphQl\Model\Resolver;
99

10+
use Lof\ProductTags\Api\Data\TagInterface;
11+
use Lof\ProductTags\Api\TagRepositoryInterface;
1012
use Lof\ProductTagsGraphQl\Model\Resolver\DataProvider\Tag as TagDataProvider;
1113
use Magento\Framework\Exception\NoSuchEntityException;
1214
use Magento\Framework\GraphQl\Config\Element\Field;
@@ -17,17 +19,24 @@
1719

1820
class Tag implements ResolverInterface
1921
{
22+
/**
23+
* @var TagRepositoryInterface
24+
*/
25+
private $tagRepository;
26+
2027
/**
2128
* @var TagDataProvider
2229
*/
2330
private $tagDataProvider;
2431

2532
/**
26-
* @param DataProvider\Tag $tagRepository
33+
* @param DataProvider\Tag $tagDataProvider
2734
*/
28-
public function __construct(DataProvider\Tag $tagDataProvider)
35+
public function __construct(DataProvider\Tag $tagDataProvider,
36+
\Lof\ProductTags\Api\TagRepositoryInterface $tagRepository)
2937
{
3038
$this->tagDataProvider = $tagDataProvider;
39+
$this->tagRepository = $tagRepository;
3140
}
3241

3342
/**
@@ -40,38 +49,51 @@ public function resolve(
4049
array $value = null,
4150
array $args = null
4251
) {
43-
$tagId = $this->getTagId($args);
44-
$tagData = $this->getTagData($tagId);
45-
46-
return $tagData;
52+
$tags = $this->getTags($args);
53+
$tagsData = $this->getTagsData($tags);
54+
$resultData = [
55+
'items' => $tagsData,
56+
];
57+
return $resultData;
4758
}
4859

4960
/**
5061
* @param array $args
51-
* @return int
62+
* @return Lof\ProductTags\Model\ResourceModel\Tag\Collection $tagList
5263
* @throws GraphQlInputException
5364
*/
54-
private function getTagId(array $args): int
65+
private function getTags(array $args)
5566
{
56-
if (!isset($args['id'])) {
57-
throw new GraphQlInputException(__('"Tag id should be specified'));
67+
if (isset($args['identifiers'])||isset($args['tag_id'])||isset($args['tag_title'])||isset($args['status'])) {
68+
//throw new GraphQlInputException(__('"identifiers" of Tag should be specified'));
69+
$taglist = $this->tagRepository->getListTag($args);
5870
}
59-
60-
return (int)$args['id'];
71+
else{
72+
throw new GraphQlInputException(__('"identifiers", "tag_id", "tag_title" or "status" of Tag should be specified'));
73+
}
74+
//$taglist = $this->tagRepository->getListTag($args);
75+
76+
return $taglist;
6177
}
6278

6379
/**
64-
* @param int $tagId
80+
* @param Lof\ProductTags\Model\ResourceModel\Tag\Collection $tagList
6581
* @return array
6682
* @throws GraphQlNoSuchEntityException
6783
*/
68-
private function getTagData(int $tagId): array
84+
private function getTagsData($tagList): array
6985
{
70-
try {
71-
$tagData = $this->tagDataProvider->getData($tagId);
72-
} catch (NoSuchEntityException $e) {
73-
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
86+
$tagsData = [];
87+
if($tagList->getSize()){
88+
foreach ($tagList as $tagItem) {
89+
$tagId = $tagItem->getId();
90+
try {
91+
$tagsData[$tagId] = $this->tagDataProvider->getData($tagItem);
92+
} catch (NoSuchEntityException $e) {
93+
$tagsData[$tagId] = new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
94+
}
95+
}
7496
}
75-
return $tagData;
97+
return $tagsData;
7698
}
7799
}

README.md

100644100755
File mode changed.

composer.json

100644100755
File mode changed.

etc/frontend/routes.xml

100644100755
File mode changed.

etc/module.xml

100644100755
File mode changed.

etc/schema.graphqls

100644100755
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22
type Query {
33

44
tag (
5-
id: Int @doc(description: "Query by id.")
5+
identifiers: String @doc(description: "Query by identifier."),
6+
tag_id: String @doc(description: "Query by tag_id."),
7+
tag_title: String @doc(description: "Query by tag_title."),
8+
status: Boolean @doc(description: "Query by status.")
69
) : Tag @resolver( class: "Lof\\ProductTagsGraphQl\\Model\\Resolver\\Tag") ,
710
product (
8-
product_id: Int @doc(description: "Query by product_id.")
11+
identifiers: [String] @doc(description: "Query by identifier.")
912
) : Product @resolver( class: "Lof\\ProductTagsGraphQl\\Model\\Resolver\\Product") ,
1013
}
11-
1214
type Tag {
15+
items: [TagsData] @doc(description: "An array information Tags.")
16+
}
17+
type TagsData {
1318
tag_id : Int @doc(description: "Query by tag_id.") ,
1419
status : Boolean @doc(description: "Query by status.") ,
1520
tag_title : String @doc(description: "Query by tag_title.") ,
@@ -18,10 +23,11 @@ type Tag {
1823
tag_description : String @doc(description: "Query by tag_description.") ,
1924
number_products : Int @doc(description: "Query by number_products.") ,
2025
create_at : String @doc(description: "Query by create_at.") ,
26+
store_id : [Int] @doc(description: "Query by store_id.") ,
2127
}
2228

2329
type Product {
2430
tag_id : Int @doc(description: "Query by tag_id.") ,
25-
product_id : Int @doc(description: "Query by product_id.") ,
31+
sku : String @doc(description: "Query by sku.") ,
2632
position : Int @doc(description: "Query by position.") ,
2733
}

0 commit comments

Comments
 (0)