resthttppost

REST. Make order request


I have two entities: Order and OrderItem. Should I use one or two routes for request that make an order?

One route:

POST /orders (also creates OrderItems)

Two routes:

POST /orders

POST /orderItems


Solution

  • Use Just a single route which contains all the orderlist details, something like this

    {
      "customerId": 123,
      "orderDate": "2024-09-23",
      "orderItems": [
        {
          "productId": 1,
          "quantity": 2,
          "price": 10.99
        },
        {
          "productId": 2,
          "quantity": 1,
          "price": 20.49
        }
      ]
    }
    

    this will make sure that order and its items are created together in one transaction