RESTful多个更新(例如:清除购物车)?

Ric*_*aca 5 rest shopping-cart

假设我有一个带有"购物车"功能的在线商店,我希望以RESTful方式实现"空车"链接.

为简单起见,假设我的资源是一个包含CartItems的Cart,每个CartItem都有一个Product.我的URI可能是:

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

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

如果是这样,"空车"链接的RESTful URI会是什么样的?

相反,我可以认为Cart是Actions的通用持有者(如此处所述):

# add a product
# form data contains e.g., 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/

这种方法似乎比它需要的更复杂.什么是更好的方式?

Cre*_*esh 16

不要做第二种方法.actions通过一个端点进行不同的漏斗并不会感觉到RESTful IMO.

你有从他们的购物车DELETE /cart_items/cart_item_id/中删除cart_item_id.怎么DELETE /cart_items/清理购物车本身?


Pra*_*nth 6

将商品添加到购物车:

POST carts/{cartid}/items

从购物车中检索特定商品:

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

从购物车中删除特定商品:

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

获得购物车的状态:

GET carts/{cartid}/state

(可以返回类似0,1的值,表示购物车中的商品数量)

清空推车:

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

这看起来很直观吗?

  • 使用`DELETE carts/{cartid}/items`清空购物车在您的(非常清楚的)示例中看起来更直观. (8认同)