RESTful多个更新(例如:清除购物车)?
问题描述:
比方说,我有一个具有“购物车”功能的在线商店,我希望以REST方式实施“空购物车”链接。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为“空购物车”链接是什么样子?
相反,我能想到的车作为操作通用支架(as described here):
# 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/
这种做法似乎复杂得多,它需要的。什么会更好?
答
不要做第二种方法。通过一个端点漏斗不同的actions
不会感到RESTful IMO。
您有DELETE /cart_items/cart_item_id/
从购物车中删除cart_item_id
。 DELETE /cart_items/
怎么样清除购物车本身?
答
添加项目到购物车:
POST carts/{cartid}/items
从购物车中获取特定项目:
DELETE carts/{cartid}/items/{itemid}
:
GET carts/{cartid}/items/{itemid}
从购物车中删除特定项目
获取车的状态:
GET carts/{cartid}/state
(可以返回像0,1,表示项目的购物车中的数的值)清空购物车
:
PUT carts/{cartid}/state?state=0
这看起来很直观吗?
使用`DELETE carts/{cartid}/items`清空购物车在您的(非常清晰)示例中看起来更直观。 – Tomas 2010-10-08 09:37:13