0

We have a cart and cart has line items, now line items can be products, discount coupons, rewards and promotions. I am looking for some inputs in design consideration here. The backend is in Ruby on Rails.

for products I have already designed the URL to be 
post: cart/line_items to create a line item for product
put: cart/line_items/:id to update the quantity
delete: cart/line_items/:id to remove the product

Now for Adding of samples and removing of samples, I am thinking of going this way.

get: cart/line_items/samples to get all the samples.
post: cart/line_items/samples to create a line item for sample
delete: cart/line_items/samples/:id to remove that sample from the cart

for discount coupon, rewards and promotions I am thinking of similar design. It would be great if experts can give me some inputs here.

8
  • why samples is a sub-resource of line_items and products is not ? Seems strange... Aug 31, 2016 at 11:31
  • @AilurusFulgens it was already implemented this way, so if we are going to add samples and other resource cart/line_items/samples then may be I would change the implementation for products also, but this is still under consideration so I am looking for input if I should implement it this way or is there a better implementation.
    – Saad
    Aug 31, 2016 at 11:35
  • Where are the cart IDs in those URLs? I would do something like `post: /cart/:id/line_items'. Similar for all the other URLs. Basically as describes in the Rails Guide under 'nested resources'. Also it is rather unclear what role exactly the 'samples' play here. Why do they need to be below line_items? Could you maybe show your model structure? Aug 31, 2016 at 11:38
  • @thorsten müller Cart is a singular resource. Samples, discount coupons, rewards belongs to a line item, so samples gets added to the cart as a line item. This was implemented quite a long time back
    – Saad
    Aug 31, 2016 at 11:41
  • 2
    Using only line_items was somewhat my first thought. Would depend on how different those things are from each other. An alternative would be to omit the line_items part in the URL (similar to shallow routing in the guide) and while keeping the samples controller using routes like /cart/samples/:id. To show the whole cart you still could have get: /cart/line_items maybe. Would somewhat depend on who consumes this API, potentially a bit confusing to some people. With the params approach you would somewhat run the risk to have a swiss army knife controller. Aug 31, 2016 at 13:40

1 Answer 1

1

Use the Item-collection pair

/collection/{item}/collection/{item}

If you think on these lines, you will design your api as follows.

/cart/12041/line_item/3192/sample/31314

As you can see, each uri contains its identifier(item) before another collection is added.

If you say, cart will not have any item id, then it becomes

/cart_line_item/3192/sample/31314 instead.

All the HTTP verbs will work on this URI convention.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.