편안한 다중 업데이트 (예 : 쇼핑 카트 지우기)?
-
22-07-2019 - |
문제
"쇼핑 카트"기능이있는 온라인 상점이 있고 편안한 방식으로 "빈 카트"링크를 구현하고 싶다고 가정 해 봅시다.
간단하게하기 위해 내 리소스가 카트가 포함 된 카트라고 가정 해 봅시다. 내 uris는 다음과 같습니다.
# 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/
그렇다면 "빈 카트"링크에 대한 편안한 URI는 어떻게 생겼습니까?
대신, 나는 카트를 행동의 범용 보유자로 생각할 수있었습니다.여기에 설명 된대로):
# 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
하나의 엔드 포인트를 통해 편안한 IMO를 느끼지 않습니다.
당신은 가지고 있습니다 DELETE /cart_items/cart_item_id/
그것은 제거합니다 cart_item_id
그들의 카트에서. 는 어때 DELETE /cart_items/
카트 자체를 지우려면?
다른 팁
카트에 항목 추가 :
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 /cart_items/
흥미로운 아이디어입니다 여기에서도 논의되었습니다.