RESTful Multiple updates (ex: delete cart)?

Let's say I have an online store with a "shopping cart" function, and I want to implement the "empty shopping cart" link using RESTful.

For simplicity, let's say my resources are a Basket containing CartItems, each of which has a Product. My URIs can be:

# add a product to the current user Cart
POST / products / product_id / cart_items /

# remove a product from the current user Cart
DELETE / cart_items / cart_item_id /

If so, what will a RESTful URI look like for an empty cart link?

Instead, I could think of Cart as a universal holder for Actions ( as described here ):

# add a product
# form data contains eg, product_id = 123 & action = add
POST / carts / cart_id / actions /

# remove a product
# action_id is the id of the action adding product 123
DELETE actions / action_id

# empty cart
# form data contains action = clear
POST / carts / cart_id / actions /

This approach seems more complicated than necessary. What would be better?

+5
source share
3 answers

Do not follow the second approach. Reusing different actionsacross one endpoint does not feel RESTful IMO.

You have one DELETE /cart_items/cart_item_id/that removes cart_item_idfrom your trash. How about DELETE /cart_items/cleaning the trolley itself?

+15
source

Adding item to cart:

POST carts/{cartid}/items

Extract a specific item from the recycle bin:

GET carts/{cartid}/items/{itemid}

Removing a specific item from the trash:

DELETE carts/{cartid}/items/{itemid}

Getting the status of the cart:

GET carts/{cartid}/state

( , 0,1, )

:

PUT carts/{cartid}/state?state=0

?

+6

DELETE /cart_items/is an interesting idea that has also been discussed here .

+1
source

All Articles