Home > Mobile >  How to design endpoints for bi-directional entity mapping
How to design endpoints for bi-directional entity mapping

Time:01-02

There are two entities, User and Order. The User entity has a Set<Order> of orders, and it's a bi-directional mapping.

Now I have two controllers, one controller is called OrderController and it has some endpoints to retrieve all orders, etc. The second controller is called UserController and it has some endpoints related to the user, but it also has endpoints such as /users/{username}/orders and /users/{username}/orders/{orderId}.

The question is where should I add an endpoint to persist an order? Should it be a POST in OrderController i.e. /orders or should it be in UserController as /users/{username}/orders endpoint? Orders are created by a user and the OrderController is only used to retrieve all orders, not only for the specific user.

Do you have any tips on how should it be designed?

CodePudding user response:

Zalando API Guideline has some view on this matter which they suggest we SHOULD limit the number of resource types because of the following reasons :

To keep maintenance and service evolution manageable, we should follow "functional segmentation" and "separation of concern" design principles and do not mix different business functionalities in same API definition. In practice this means that the number of resource types exposed via an API should be limited. In this context a resource type is defined as a set of highly related resources such as a collection, its members and any direct sub-resources.

Apply to your case , basically they suggest only has the following resource types :

  • /users
  • /orders

And no need to have /users/{username}/orders/ as /orders already serve the same purpose. If you want to get orders for an user , simply use /orders?user=foobar

  • Related