Resources in a RestFul API

1

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.

    
asked by RodriKing 14.05.2018 в 19:13
source

1 answer

1

Your question is very good, which gives you the solution to one of your questions (Since the others are very similar)

1. To get a Customer's Orders as the resource would be?

Depends on the implementation of your JSON (Body Response Message), which within the customers object you can have an Order List , like the following way:

{
    "Nombre": "Diego Orellana",
    "Cliente": 1
    "Pedidos": 
        [
            {
                "Descripcion": "Bebida",
                "Cantidad": 1,
                "Precio": 1.00,
                "Moneda": "$"
            },
            {
                "Descripcion": "Hamburguersa",
                "Cantidad": 1,
                "Precio": 2.00,
                "Moneda": "$"
            }
        ]
}

As you can see, when you call GET / customers / 1 , you will get a client, and within them the corresponding customer orders (A customer can have many orders).

It is worth mentioning that this is an example, because it depends on the business logic that you implement in your application.

Also within an API, you can implement several controllers (For example, BringOffersDay (int day), which you could call as follows:

  

/ offers / BringOfDays Day = 1

Which you recommend that you read a bit of API Implementation (Because it is an extensive topic, and you can do many things, among them you will touch the topic of Data Transfer Object (DTO), which is closely related to JSON ).

To finish with the answer, to delete a client would be as follows:

  

DELETE / customers / 1

I hope I have helped you a little, greetings.

    
answered by 14.05.2018 в 19:59