Skip to content

Commit 85d0dd6

Browse files
committed
MC-34298: Introduce separate POST and PUT API for carts/mine/items
1 parent 5ee9e62 commit 85d0dd6

File tree

6 files changed

+151
-4
lines changed

6 files changed

+151
-4
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Quote\Api;
9+
10+
use Magento\Framework\Exception\CouldNotSaveException;
11+
use Magento\Framework\Exception\InputException;
12+
use Magento\Framework\Exception\NoSuchEntityException;
13+
use Magento\Quote\Api\Data\CartItemInterface;
14+
15+
/**
16+
* Interface AddCartItemInterface
17+
* @api
18+
*/
19+
interface AddCartItemInterface
20+
{
21+
/**
22+
* Add the specified cart item.
23+
*
24+
* @param CartItemInterface $cartItem The item.
25+
* @return CartItemInterface
26+
* @throws NoSuchEntityException The specified cart does not exist.
27+
* @throws CouldNotSaveException The specified item could not be saved to the cart.
28+
* @throws InputException The specified item or cart is not valid.
29+
*/
30+
public function execute(CartItemInterface $cartItem);
31+
}

app/code/Magento/Quote/Api/CartItemRepositoryInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public function getList($cartId);
2929
* @throws \Magento\Framework\Exception\NoSuchEntityException The specified cart does not exist.
3030
* @throws \Magento\Framework\Exception\CouldNotSaveException The specified item could not be saved to the cart.
3131
* @throws \Magento\Framework\Exception\InputException The specified item or cart is not valid.
32+
*
33+
* @deprecated Post and put endpoint should be separated
34+
*
35+
* @see \Magento\Quote\Api\AddCartItemInterface
36+
* @see \Magento\Quote\Api\UpdateCartItemInterface
3237
*/
3338
public function save(\Magento\Quote\Api\Data\CartItemInterface $cartItem);
3439

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Quote\Api;
9+
10+
use Magento\Framework\Exception\CouldNotSaveException;
11+
use Magento\Framework\Exception\InputException;
12+
use Magento\Framework\Exception\NoSuchEntityException;
13+
use Magento\Quote\Api\Data\CartItemInterface;
14+
15+
/**
16+
* Interface AddCartItemInterface
17+
* @api
18+
*/
19+
interface UpdateCartItemInterface
20+
{
21+
/**
22+
* Update the specified cart item.
23+
*
24+
* @param CartItemInterface $cartItem The item.
25+
* @return CartItemInterface Item.
26+
* @throws NoSuchEntityException The specified cart does not exist.
27+
* @throws CouldNotSaveException The specified item could not be saved to the cart.
28+
* @throws InputException The specified item or cart is not valid.
29+
*/
30+
public function execute(CartItemInterface $cartItem);
31+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Quote\Model\Quote\Item;
9+
10+
use Magento\Quote\Api\AddCartItemInterface;
11+
use Magento\Quote\Api\Data\CartItemInterface;
12+
13+
/**
14+
* Add the specified cart item
15+
*/
16+
class AddCartItem implements AddCartItemInterface
17+
{
18+
/**
19+
* @var Repository
20+
*/
21+
private $quoteItemRepository;
22+
23+
/**
24+
* @param Repository $quoteItemRepository
25+
*/
26+
public function __construct(Repository $quoteItemRepository)
27+
{
28+
$this->quoteItemRepository = $quoteItemRepository;
29+
}
30+
31+
/**
32+
* @inheritDoc
33+
*/
34+
public function execute(CartItemInterface $cartItem)
35+
{
36+
if (isset($cartItem[CartItemInterface::KEY_ITEM_ID])) {
37+
unset($cartItem[CartItemInterface::KEY_ITEM_ID]);
38+
}
39+
40+
return $this->quoteItemRepository->save($cartItem);
41+
}
42+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Quote\Model\Quote\Item;
9+
10+
use Magento\Quote\Api\AddCartItemInterface;
11+
use Magento\Quote\Api\Data\CartItemInterface;
12+
13+
/**
14+
* Update the specified cart item
15+
*/
16+
class UpdateCartItem implements AddCartItemInterface
17+
{
18+
/**
19+
* @var Repository
20+
*/
21+
private $quoteItemRepository;
22+
23+
/**
24+
* @param Repository $quoteItemRepository
25+
*/
26+
public function __construct(Repository $quoteItemRepository)
27+
{
28+
$this->quoteItemRepository = $quoteItemRepository;
29+
}
30+
31+
/**
32+
* @inheritDoc
33+
*/
34+
public function execute(CartItemInterface $cartItem)
35+
{
36+
return $this->quoteItemRepository->save($cartItem);
37+
}
38+
}

app/code/Magento/Quote/etc/webapi.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,13 @@
177177
</resources>
178178
</route>
179179
<route url="/V1/carts/:quoteId/items" method="POST">
180-
<service class="Magento\Quote\Api\CartItemRepositoryInterface" method="save"/>
180+
<service class="Magento\Quote\Api\AddCartItemInterface" method="execute"/>
181181
<resources>
182182
<resource ref="Magento_Cart::manage" />
183183
</resources>
184184
</route>
185185
<route url="/V1/carts/:cartId/items/:itemId" method="PUT">
186-
<service class="Magento\Quote\Api\CartItemRepositoryInterface" method="save"/>
186+
<service class="Magento\Quote\Api\UpdateCartItemInterface" method="execute"/>
187187
<resources>
188188
<resource ref="Magento_Cart::manage" />
189189
</resources>
@@ -232,7 +232,7 @@
232232
</data>
233233
</route>
234234
<route url="/V1/carts/mine/items" method="POST">
235-
<service class="Magento\Quote\Api\CartItemRepositoryInterface" method="save"/>
235+
<service class="Magento\Quote\Api\AddCartItemInterface" method="execute"/>
236236
<resources>
237237
<resource ref="self" />
238238
</resources>
@@ -241,7 +241,7 @@
241241
</data>
242242
</route>
243243
<route url="/V1/carts/mine/items/:itemId" method="PUT">
244-
<service class="Magento\Quote\Api\CartItemRepositoryInterface" method="save"/>
244+
<service class="Magento\Quote\Api\UpdateCartItemInterface" method="execute"/>
245245
<resources>
246246
<resource ref="self" />
247247
</resources>

0 commit comments

Comments
 (0)