0

I have three tables Customers, Sellers and Orders, I'm using Rest API, then what is the best way to access the Customer/seller Orders?

1: /customers/:custumerId/orders

2: /orders/customers/:customerId

1: /sellers/:sellerId/orders

2: /orders/sellers/:sellerId

Now I'm using the second (2) way. But I'm with doubt if is the correct way.

David Walschots
  • 12,279
  • 5
  • 36
  • 59

2 Answers2

1

Option 1 seems the most restful pattern. This is because the last listed resource in a path should be the returned resource. So since you are trying to get all the orders from a seller or buyer, the orders should be the last resource in the path. Another way of thinking about this is that the nesting structure of your paths should mirror the nesting structure of your models, e.g. orders are within a customer/seller.

So /customers/:customerId/orders implies that orders of a particular customer will be returned, while /orders/customers/:customerId implies that all a particular customer (of the possibly many on the order) will be returned.

I do see the logic in your second strategy, since you can group all requests for order objects together whether by customer or seller, however a better way to accomplish this pattern would be to use the path /orders and specify the customer or seller in the query string. For example, /orders?customer_id=:customerId and /orders?seller_id=:sellerId.

Note that there is nothing wrong with allowing a resource to be accessible in more than one way. Check out this post for more discussion on nested RESTful routing: What are best practices for REST nested resources?.

Henry Woody
  • 14,024
  • 7
  • 39
  • 56
0

1 will be ideal if I am to choose from those two. Because of the way seller, customer and order are related.

Customer order - 1 to many

Seller customer - many to many

Seller order - 1 to many

Sachith Rukshan
  • 340
  • 6
  • 24