Skip to content

[GraphQL] Compare products #29047

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 69 commits into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
2806d48
Add DB schema
Usik2203 Jul 8, 2020
da43047
add list_id during comparing products
Usik2203 Jul 8, 2020
2fe99e9
set customer for compare list during login
Usik2203 Jul 16, 2020
fb69971
Merge branch '2.4-develop' into compare-products
Usik2203 Aug 12, 2020
1fd3c5b
changes based on review
Usik2203 Aug 12, 2020
330c200
started working on compared list graphql modules
Usik2203 Aug 12, 2020
339a1a0
added resolver for getting compare list by id
Usik2203 Aug 17, 2020
d24e544
add remove and assign functionality
Usik2203 Aug 17, 2020
0abdc60
Merge branch '2.4-develop' into compare-products
Usik2203 Aug 17, 2020
a9ea92a
separated some logic
Usik2203 Aug 21, 2020
907877b
have decoupled some resolvers
Usik2203 Aug 21, 2020
e7e6f16
Merge branch '2.4-develop' into compare-products
Usik2203 Oct 3, 2020
3dceba9
Added changes to schema and add some class
Usik2203 Oct 3, 2020
571a73e
set type list_id as varchar in DB
Usik2203 Oct 3, 2020
a8fe53f
add changes to DB scheme
Usik2203 Oct 5, 2020
79eea2b
Extract list id to list_id_mask table
Usik2203 Oct 14, 2020
5f4fd9f
reverted changes
Usik2203 Oct 14, 2020
8be68b4
create compare list
Usik2203 Oct 14, 2020
c981564
added resolver
Usik2203 Oct 14, 2020
b03318a
rename classes
Usik2203 Oct 14, 2020
5e69a51
Refactoring resolvers
Usik2203 Oct 15, 2020
40d5bf6
removing product from compare list and compare list
Usik2203 Oct 16, 2020
eb4de34
fix static issues
Usik2203 Oct 16, 2020
94a9cdd
Merge branch '2.4-develop' into compare-products
Usik2203 Oct 16, 2020
261e00c
static issue fixes
Usik2203 Oct 17, 2020
89d77bb
changes in DB scheme
Usik2203 Oct 17, 2020
48197d4
Minor changes in schema
Usik2203 Oct 21, 2020
058e329
Merge branch '2.4-develop' into compare-products
Usik2203 Oct 21, 2020
455a28e
Add changes after schema updating
Usik2203 Oct 26, 2020
d436687
Add test coverage
Usik2203 Oct 26, 2020
b4a0ab6
Merge branch '2.4-develop' into compare-products
Usik2203 Oct 27, 2020
880e454
add new scenario to test coverage
Usik2203 Oct 28, 2020
bdbdddd
Merge branch '2.4-develop' into compare-products
Usik2203 Nov 2, 2020
0c34fd6
Merge branch '2.4-develop' into compare-products
Usik2203 Nov 3, 2020
193702b
Fixed failed test due to ProductInterface in schema
Usik2203 Nov 4, 2020
2857996
CR recommendations
Usik2203 Nov 4, 2020
1ec40b0
Removed list id mask table
Usik2203 Nov 6, 2020
8780f66
add additional check
Usik2203 Nov 6, 2020
dd5b443
luma compatibility add list id for customer compare list
Usik2203 Nov 6, 2020
2e48ca1
luma compatibility remove compare list
Usik2203 Nov 6, 2020
098a407
minor changes
Usik2203 Nov 7, 2020
32f251f
Merge remote-tracking branch 'mainline/2.4-develop' into compare-prod…
Nov 10, 2020
b21be30
- Modified assigncomparelust output to match schema in architecture
Nov 11, 2020
e4043da
Merge remote-tracking branch 'mainline/2.4-develop' into compare-prod…
Nov 11, 2020
27a592c
- Updated assigncomparelist impl
Nov 12, 2020
7124f66
Merge remote-tracking branch 'mainline/2.4-develop' into compare-prod…
Nov 12, 2020
d7e1568
Merge commit 'b21be308c2a369bcbcde2db5921faaf8b2b237af' into compare-…
Nov 12, 2020
5f7ad0e
Merge commit '27a592cf38b501bcf05d9ef2e5401a9cff7e986b' into compare-…
Nov 12, 2020
923c723
Update schema description
Usik2203 Nov 13, 2020
34790b0
minor changes
Usik2203 Nov 13, 2020
6474eaa
Merge branch '2.4-develop' into compare-products
Usik2203 Nov 13, 2020
a9cffd6
changes in composer.json
Usik2203 Nov 13, 2020
93f7d44
Merge branch '2.4-develop' into compare-products
Usik2203 Nov 13, 2020
ce4fc73
Update app/code/Magento/CompareListGraphQl/etc/module.xml
Usik2203 Nov 16, 2020
a99515d
Merge branch '2.4-develop' into compare-products
Usik2203 Nov 16, 2020
e90810a
compare-products - Fixed errors on creating comparelist and adding pr…
Nov 18, 2020
8aadac8
MC-37399: Product Compare :: Atwix PR review and delivery
dthampy Nov 19, 2020
058a0be
Update app/code/Magento/CompareListGraphQl/etc/schema.graphqls
Usik2203 Nov 23, 2020
9640f32
Update app/code/Magento/CompareListGraphQl/etc/schema.graphqls
Usik2203 Nov 23, 2020
270649c
Merge branch '2.4-develop' into compare-products
Usik2203 Nov 23, 2020
2b144f9
Merge remote-tracking branch 'mainline/2.4-develop' into compare-prod…
Nov 23, 2020
28232a7
Merge remote-tracking branch 'usik/compare-products' into compare-pro…
Nov 25, 2020
600bcac
Merge branch '2.4-develop' into compare-products
Usik2203 Nov 27, 2020
4d11de7
Merge remote-tracking branch 'mainline/2.4-develop' into compare-prod…
Nov 30, 2020
c53a3cf
Merge remote-tracking branch 'usik/compare-products' into compare-pro…
Nov 30, 2020
5cd7d07
Merge remote-tracking branch 'mainline/2.4-develop' into compare-prod…
Dec 1, 2020
17ed57e
- Updated constraints on the catalog_compare_list table
Dec 1, 2020
afb1581
Removing additional calls to the db.
Dec 2, 2020
b2a1894
Merge branch '2.4-develop' into compare-products
Usik2203 Dec 3, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Magento\Catalog\Controller\Product\Compare;

use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\ResourceModel\Product\Compare\Item\Collection;
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
use Magento\Framework\Exception\NoSuchEntityException;

Expand Down
24 changes: 24 additions & 0 deletions app/code/Magento/Catalog/Model/CompareList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Catalog\Model;

use Magento\Framework\Model\AbstractModel;

class CompareList extends AbstractModel
{
/**
* Initialize resource
*
* @return void
*/
protected function _construct()
{
$this->_init(ResourceModel\Product\Compare\CompareList::class);
}
}
58 changes: 58 additions & 0 deletions app/code/Magento/Catalog/Model/CompareListIdToMaskedListId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Model;

use Magento\Catalog\Model\ResourceModel\Product\Compare\CompareList as CompareListResource;
use Magento\Framework\Exception\LocalizedException;

/**
* CompareListId to MaskedListId resolver
*/
class CompareListIdToMaskedListId
{
/**
* @var CompareListFactory
*/
private $compareListFactory;

/**
* @var CompareListResource
*/
private $compareListResource;

/**
* @param CompareListFactory $compareListFactory
* @param CompareListResource $compareListResource
*/
public function __construct(
CompareListFactory $compareListFactory,
CompareListResource $compareListResource
) {
$this->compareListFactory = $compareListFactory;
$this->compareListResource = $compareListResource;
}

/**
* Get listIdMask by listId
*
* @param int $listId
*
* @param int|null $customerId
* @return null|string
* @throws LocalizedException
*/
public function execute(int $listId, int $customerId = null): ?string
{
$compareList = $this->compareListFactory->create();
$this->compareListResource->load($compareList, $listId, 'list_id');
if ((int)$compareList->getCustomerId() !== (int)$customerId) {
throw new LocalizedException(__('This customer is not authorized to access this list'));
}
return $compareList->getListIdMask() ?? null;
}
}
57 changes: 57 additions & 0 deletions app/code/Magento/Catalog/Model/MaskedListIdToCompareListId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Model;

use Magento\Catalog\Model\ResourceModel\Product\Compare\CompareList as CompareListResource;
use Magento\Framework\Exception\LocalizedException;

/**
* MaskedListId to ListId resolver
*/
class MaskedListIdToCompareListId
{
/**
* @var CompareListFactory
*/
private $compareListFactory;

/**
* @var CompareListResource
*/
private $compareListResource;

/**
* @param CompareListFactory $compareListFactory
* @param CompareListResource $compareListResource
*/
public function __construct(
CompareListFactory $compareListFactory,
CompareListResource $compareListResource
) {
$this->compareListFactory = $compareListFactory;
$this->compareListResource = $compareListResource;
}

/**
* Get maskedId by listId
*
* @param string $maskedListId
* @param int $customerId
* @return int
* @throws LocalizedException
*/
public function execute(string $maskedListId, int $customerId = null): int
{
$compareList = $this->compareListFactory->create();
$this->compareListResource->load($compareList, $maskedListId, 'list_id_mask');
if ((int)$compareList->getCustomerId() !== (int)$customerId) {
throw new LocalizedException(__('This customer is not authorized to access this list'));
}
return (int)$compareList->getListId();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Catalog\Model\ResourceModel\Product\Compare;

use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

/**
* Compare List resource class
*/
class CompareList extends AbstractDb
{
/**
* @inheritdoc
*/
protected function _construct()
{
$this->_init('catalog_compare_list', 'list_id');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public function loadByProduct(\Magento\Catalog\Model\Product\Compare\Item $objec
$select->where('visitor_id = ?', (int)$object->getVisitorId());
}

if ($object->getListId()) {
$select->where('list_id = ?', (int)$object->getListId());
}

$data = $connection->fetchRow($select);

if (!$data) {
Expand Down Expand Up @@ -140,6 +144,7 @@ public function purgeVisitorByCustomer($object)

/**
* Update (Merge) customer data from visitor
*
* After Login process
*
* @param \Magento\Catalog\Model\Product\Compare\Item $object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection
*/
protected $_visitorId = 0;

/**
* List Id Filter
*
* @var int
*/
protected $listId = 0;

/**
* Comparable attributes cache
*
Expand Down Expand Up @@ -156,6 +163,30 @@ public function setCustomerId($customerId)
return $this;
}

/**
* Set listId filter to collection
*
* @param int $listId
*
* @return $this
*/
public function setListId(int $listId)
{
$this->listId = $listId;
$this->_addJoinToSelect();
return $this;
}

/**
* Retrieve listId filter applied to collection
*
* @return int
*/
public function getListId(): int
{
return (int)$this->listId;
}

/**
* Set visitor filter to collection
*
Expand Down Expand Up @@ -204,6 +235,10 @@ public function getConditionForJoin()
return ['visitor_id' => $this->getVisitorId()];
}

if ($this->getListId()) {
return ['list_id' => $this->getListId()];
}

return ['customer_id' => ['null' => true], 'visitor_id' => '0'];
}

Expand Down Expand Up @@ -232,6 +267,82 @@ public function _addJoinToSelect()
return $this;
}

/**
* Get products ids by for compare list
*
* @param int $listId
*
* @return array
*/
public function getProductsByListId(int $listId): array
{
$select = $this->getConnection()->select()->
from(
$this->getTable('catalog_compare_item'),
'product_id'
)->where(
'list_id = ?',
$listId
);
return $this->getConnection()->fetchCol($select);
}


/**
* Set list_id for customer compare item
*
* @param int $listId
* @param int $customerId
*/
public function setListIdToCustomerCompareItems(int $listId, int $customerId)
{
foreach ($this->getCustomerCompareItems($customerId) as $itemId) {
$this->getConnection()->update(
$this->getTable('catalog_compare_item'),
['list_id' => $listId],
['catalog_compare_item_id = ?' => (int)$itemId]
);
}
}

/**
* Remove compare list if customer compare list empty
*
* @param int|null $customerId
*/
public function removeCompareList(?int $customerId)
{
if (empty($this->getCustomerCompareItems($customerId))) {
$this->getConnection()->delete(
$this->getTable('catalog_compare_list'),
['customer_id = ?' => $customerId]
);
}
}

/**
* Get customer compare items
*
* @param int|null $customerId
* @return array
*/
private function getCustomerCompareItems(?int $customerId): array
{
if ($customerId) {
$select = $this->getConnection()->select()->
from(
$this->getTable('catalog_compare_item')
)->where(
'customer_id = ?',
$customerId
);

return $this->getConnection()->fetchCol($select);
}

return [];
}

/**
* Retrieve comapre products attribute set ids
*
Expand Down
23 changes: 23 additions & 0 deletions app/code/Magento/Catalog/etc/db_schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,8 @@
default="0" comment="Product ID"/>
<column xsi:type="smallint" name="store_id" unsigned="true" nullable="true" identity="false"
comment="Store ID"/>
<column xsi:type="int" name="list_id" padding="10" unsigned="true" nullable="true" identity="false"
comment="List ID"/>
<constraint xsi:type="primary" referenceId="PRIMARY">
<column name="catalog_compare_item_id"/>
</constraint>
Expand All @@ -558,6 +560,8 @@
referenceColumn="entity_id" onDelete="CASCADE"/>
<constraint xsi:type="foreign" referenceId="CATALOG_COMPARE_ITEM_STORE_ID_STORE_STORE_ID" table="catalog_compare_item"
column="store_id" referenceTable="store" referenceColumn="store_id" onDelete="SET NULL"/>
<constraint xsi:type="foreign" referenceId="CATALOG_COMPARE_ITEM_LIST_ID_CATALOG_COMPARE_LIST_LIST_ID" table="catalog_compare_item"
column="list_id" referenceTable="catalog_compare_list" referenceColumn="list_id" onDelete="CASCADE"/>
<index referenceId="CATALOG_COMPARE_ITEM_PRODUCT_ID" indexType="btree">
<column name="product_id"/>
</index>
Expand All @@ -573,6 +577,25 @@
<column name="store_id"/>
</index>
</table>
<table name="catalog_compare_list" resource="default" engine="innodb" comment="Catalog Compare List with hash Table">
<column xsi:type="int" name="list_id" padding="10" unsigned="true" nullable="false"
identity="true" comment="Compare List ID"/>
<column xsi:type="varchar" name="list_id_mask" nullable="true" length="32" comment="Masked ID"/>
<column xsi:type="int" name="customer_id" padding="10" unsigned="true" nullable="true" identity="false"
comment="Customer ID"/>
<constraint xsi:type="primary" referenceId="PRIMARY">
<column name="list_id"/>
</constraint>
<constraint xsi:type="unique" referenceId="CATALOG_COMPARE_LIST_CUSTOMER_ID">
<column name="customer_id"/>
</constraint>
<constraint xsi:type="foreign" referenceId="CATALOG_COMPARE_LIST_CUSTOMER_ID_CUSTOMER_ENTITY_ENTITY_ID"
table="catalog_compare_list" column="customer_id" referenceTable="customer_entity"
referenceColumn="entity_id" onDelete="CASCADE"/>
<index referenceId="CATALOG_COMPARE_LIST_LIST_ID_MASK" indexType="btree">
<column name="list_id_mask"/>
</index>
</table>
<table name="catalog_product_website" resource="default" engine="innodb"
comment="Catalog Product To Website Linkage Table">
<column xsi:type="int" name="product_id" unsigned="true" nullable="false" identity="false"
Expand Down
Loading