I have read several things about APIs but there is something that has not been clear to me on how to structure resources (endpoints). I will give an easy but illustrative example. We imagine that we have this relationship:
|Clientes| (1:1) ------<>----- (0:M) |Pedidos| (1:1) ------<>------ (1:1) |Estados|
A customer can have zero or many orders and each order has a status.
The question comes when making the resources, the resources that are clear are the following:
GET /clientes (obtener un listado de clientes)
GET /clientes/10 (obtener el detalle de un cliente)
POST /clientes (crear un cliente pasando los datos por el BODY)
(there could be more like the PUT
but to simplify the simplified example.)
The question is, to get the Orders from a Customer as it would be recurso
?
GET /clientes/10/orders
Or instead:
GET /orders?id_cliente=10
The same thing to get the detail of a Order , how would it be?
GET /clientes/10/orders/10
Or it would just make sense to do this (in which the State information it has) would also come out:
GET /orders/10
Or on the subject of deleting a Order .
DELETE /orders/10
or instead:
DELETE clientes/10/orders/10
Or have a resource that is simply:
GET /orders
And take all orders from the database
And to create a Order , there should always be the Customer or you could create a Order and a Customer strong> at the same time with the following resource? For example, a Customer not registered when making a purchase make the Order and register at the same time)
POST /orders
Weighing the Customer data in BODY
as the Order data would go. First you would create the Client and then the Order .
If there is someone who knows how all the valid resources of the example relationship would be, it would be good to share them. I do not want to get into issues of Pagination or other issues that are also important in an API. Only on the subject of resources.