Skip to content

Commit 8fabf69

Browse files
committed
update file
1 parent da3a61c commit 8fabf69

File tree

4 files changed

+43
-24
lines changed

4 files changed

+43
-24
lines changed

Model/Resolver/DataProvider/Product.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,16 @@ public function getData(string $tagCode): array
6969
$productData->setSku($product->getSku())
7070
->setPosition($product->getData('tag_index_position'))
7171
->setTagId($tagModel->getId());
72-
$productData = [
73-
TagProductLinkInterface::KEY_SKU => $product->getSku(),
74-
TagProductLinkInterface::KEY_POSITION => $product->getPosition(),
75-
TagProductLinkInterface::KEY_TAG_ID => $product->getTagId(),
72+
$productsData = [
73+
TagProductLinkInterface::KEY_SKU => $productData->getSku(),
74+
TagProductLinkInterface::KEY_POSITION => $productData->getPosition(),
75+
TagProductLinkInterface::KEY_TAG_ID => $productData->getTagId(),
7676
];
7777
// $productData->setSku($product->getSku())
7878
// ->setPosition($product->getData('tag_index_position'))
7979
// ->setTagId($tagModel->getId());
80-
$productsData[] = $productData;
81-
return $productsData;
8280
}
81+
return $productsData;
8382
}
8483
// $product = $this->productsManagement->getProducts($tagCode);
8584

Model/Resolver/DataProvider/Tag.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,29 @@ 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 string $tagsId
37+
* @param string $tagsIdentifier
3538
* @return array
3639
* @throws NoSuchEntityException
3740
*/
38-
public function getData(string $tagsId): array
41+
public function getData(string $tagsIdentifier): array
3942
{
40-
$tag = $this->tagRepository->getById($tagsId);
43+
$tagModel = $this->_tagModelFactory->create();
44+
$tagModel->loadByIdentifier($tagsIdentifier);
45+
if (!$tagModel->getId()) {
46+
return [];
47+
}
48+
$tag = $this->tagRepository->getById($tagModel->getId());
4149

4250
if (false === $tag->getStatus()) {
4351
throw new NoSuchEntityException();

Model/Resolver/Tag.php

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,41 +40,54 @@ public function resolve(
4040
array $value = null,
4141
array $args = null
4242
) {
43-
$tagsId = $this->getTagId($args);
44-
$tagsData = $this->getTagData($tagsId);
43+
$tagsIdentifier = $this->getTagIdentifier($args);
44+
$tagsData = $this->getTagData($tagsIdentifier);
4545
$resultData = [
4646
'items' => $tagsData,
4747
];
4848
return $resultData;
4949
}
5050

5151
/**
52-
* @param array $args
52+
* @param string $args
5353
* @return string[]
5454
* @throws GraphQlInputException
5555
*/
56-
private function getTagId(array $args): array
56+
private function getTagIdentifier(string $args): array
5757
{
58-
if (!isset($args['id'])) {
59-
throw new GraphQlInputException(__('"Tag id should be specified'));
58+
if (!isset($args['identifiers']) || !is_array($args['identifiers']) || count($args['identifiers']) === 0) {
59+
throw new GraphQlInputException(__('"identifiers" of Tag should be specified'));
6060
}
6161

62-
return (array)$args['id'];
62+
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
63+
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
64+
$connection = $resource->getConnection();
65+
$tableName = $resource->getTableName('lof_producttags_tag');
66+
$select = $connection->select()->from(
67+
$tableName,
68+
'identifier'
69+
)
70+
->where(
71+
'identifier'
72+
)
73+
->like($args['identifiers']);
74+
return $select;
6375
}
6476

6577
/**
66-
* @param array $tagsId
78+
* @param array $tagsIdentifier
6779
* @return array
6880
* @throws GraphQlNoSuchEntityException
6981
*/
70-
private function getTagData(array $tagsId): array
82+
private function getTagData(array $tagsIdentifier): array
7183
{
7284
$tagsData = [];
73-
foreach ($tagsId as $tagId) {
85+
86+
foreach ($tagsIdentifier as $tagIdentifier) {
7487
try {
75-
$tagsData[$tagId] = $this->tagDataProvider->getData($tagId);
88+
$tagsData[$tagIdentifier] = $this->tagDataProvider->getData($tagIdentifier);
7689
} catch (NoSuchEntityException $e) {
77-
$tagsData[$tagId] = new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
90+
$tagsData[$tagIdentifier] = new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
7891
}
7992
}
8093
return $tagsData;

etc/schema.graphqls

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

44
tag (
5-
id: [String] @doc(description: "Query by tag_id.")
5+
identifiers: FilterTypeInput @doc(description: "Query by tag_id.")
66
) : Tag @resolver( class: "Lof\\ProductTagsGraphQl\\Model\\Resolver\\Tag") ,
77
product (
8-
identifiers: [String] @doc(description: "Query by product_id.")
8+
identifiers: [String] @doc(description: "Query by identifier.")
99
) : Product @resolver( class: "Lof\\ProductTagsGraphQl\\Model\\Resolver\\Product") ,
1010
}
1111
type Tag {
@@ -25,6 +25,5 @@ type TagsData {
2525
type Product {
2626
tag_id : Int @doc(description: "Query by tag_id.") ,
2727
sku : String @doc(description: "Query by sku.") ,
28-
product_id : Int @doc(description: "Query by product_id.") ,
2928
position : Int @doc(description: "Query by position.") ,
3029
}

0 commit comments

Comments
 (0)