|
12 | 12 | use Magento\Catalog\Model\ResourceModel\Product\Compare\CompareList as ResourceCompareList; |
13 | 13 | use Magento\Catalog\Model\ResourceModel\Product\Compare\Item\CollectionFactory as CompareItemsCollectionFactory; |
14 | 14 | use Magento\CompareListGraphQl\Model\Service\AddToCompareList; |
| 15 | +use Magento\Framework\Exception\LocalizedException; |
15 | 16 | use Magento\Framework\GraphQl\Query\Resolver\ContextInterface; |
16 | 17 |
|
17 | 18 | /** |
@@ -64,18 +65,33 @@ public function __construct( |
64 | 65 | * @param int $customerListId |
65 | 66 | * @param ContextInterface $context |
66 | 67 | * @return CompareList |
| 68 | + * @throws LocalizedException |
67 | 69 | * @throws \Exception |
68 | 70 | */ |
69 | 71 | public function execute(int $guestListId, int $customerListId, ContextInterface $context): CompareList |
70 | 72 | { |
71 | | - $items = $this->itemCollectionFactory->create(); |
72 | | - $products = $items->getProductsByListId($guestListId); |
| 73 | + if ($guestListId === $customerListId) { |
| 74 | + throw new LocalizedException(__('Cannot merge a list with itself.')); |
| 75 | + } |
73 | 76 |
|
74 | | - $this->addProductToCompareList->execute($customerListId, $products, $context); |
| 77 | + $connection = $this->resourceCompareList->getConnection(); |
| 78 | + $connection->beginTransaction(); |
75 | 79 |
|
76 | | - $guestList = $this->compareListFactory->create(); |
77 | | - $this->resourceCompareList->load($guestList, $guestListId, 'list_id'); |
78 | | - $this->resourceCompareList->delete($guestList); |
| 80 | + try { |
| 81 | + $items = $this->itemCollectionFactory->create(); |
| 82 | + $products = $items->getProductsByListId($guestListId); |
| 83 | + |
| 84 | + $this->addProductToCompareList->execute($customerListId, $products, $context); |
| 85 | + |
| 86 | + $guestList = $this->compareListFactory->create(); |
| 87 | + $this->resourceCompareList->load($guestList, $guestListId, 'list_id'); |
| 88 | + $this->resourceCompareList->delete($guestList); |
| 89 | + |
| 90 | + $connection->commit(); |
| 91 | + } catch (\Exception $e) { |
| 92 | + $connection->rollBack(); |
| 93 | + throw $e; |
| 94 | + } |
79 | 95 |
|
80 | 96 | $customerList = $this->compareListFactory->create(); |
81 | 97 | $this->resourceCompareList->load($customerList, $customerListId, 'list_id'); |
|
0 commit comments