'How to insert data into multiple tables using a POST request on Postman?

I have 3 tables that need to be populated; OrderHistory, OrderHistoryItem and OrderHistoryLabel. These three tables are connected to each other through the following keys:

UserID - references AspNetUsers(Id)
OrderID in OrderHistoryItem - references OrderHistory(OrderID)

This picture will provide more information about the table but I included only what I deem important at this stage.

The following code is written in visual studio and represents the method used to create the order by adding all needed values in their tables.

    [Route("v1/orderhistory")]
    [HttpPost()]
    public IActionResult CreateOrder([FromBody]OrderHistoryDto ohdata, [FromBody]OrderHistoryItemDto itemdata, [FromBody]HistoryLabelDto labeldata)
    {
        try
        {
            if (ohdata == null) throw new ArgumentException("No data specified");
            if (itemdata == null) throw new ArgumentException("No data specified");
            if (labeldata == null) throw new ArgumentException("No data specified");
            //string orderId = "40281804-FE51-4893-AEA8-944903C27771";
            var orderID = Guid.NewGuid();

            using (var con = _connFactory())
            {
                con.Open();
                con.Execute("INSERT INTO dbo.OrderHistoryItemLabel (LabelID, OrderProductID) VALUES (@labelID, @orderProductID)", new
                {
                    labeldata.orderProductID,
                    labeldata.labelID
                });

                con.Execute(@"INSERT INTO dbo.OrderHistoryItem (ProductID, OrderID, ProductGroup, NormalPrice, Price, StandardUnit, PricePerStandardAmount, Quantity, Discount, IsDiscounted, TotalPrice) 
                            VALUES (@productID, @orderID, @productGroup, @normalPrice, @price, @standardUnit, @pricePerStandardAmount, @quantity, @discount, @isDiscounted, @totalPrice)", new
                {
                    itemdata.productID,
                    itemdata.orderID,
                    itemdata.productGroup,
                    itemdata.normalPrice,
                    itemdata.price,
                    itemdata.standardUnit,
                    itemdata.pricePerStandardAmount,
                    itemdata.quantity,
                    itemdata.discount,
                    itemdata.isDiscounted,
                    itemdata.itemTotalPrice,
                    labeldata.orderProductID
                });
                con.Execute("INSERT INTO dbo.OrderHistory (UserID, TransferredAtUtc, OrderTotalPrice, StoreID) VALUES (@userID, @transferredAtUtc, @orderTotalPrice, @storeID)", new
                {
                    ohdata.userID,
                    ohdata.transferredAtUtc,
                    ohdata.orderTotalPrice,
                    ohdata.storeID
                });
            }
            return Ok(orderID);
        }
        catch (Exception exc)
        {
            return BadRequest();
        }
    }
}

As a testing tool, I am using Postman, and the POST request written in JSON is the following:

{
    "TransferredAtUtc": "",
    "OrderTotalPrice": 344343.43,
    "StoreId": 1,
    "Items": [
        {
            "LabelID": [
                1,
                2,
                3,
                4,
                5
            ],
            "ProductID": 1,
            "ProductGroup": "asd",
            "NormalPrice": 2,
            "Price": 2,
            "StandardUnit": "asd",
            "PricePerStandardAmount": 2,
            "Quantity": 1,
            "Discount": 0,
            "IsDiscounted": 0,
            "ItemTotalPrice": 2
        },
        {
            "LabelID": [
                1,
                2,
                5
            ],
            "ProductID": 434,
            "ProductGroup": "asd",
            "NormalPrice": 2,
            "Price": 22,
            "StandardUnit": "asd",
            "PricePerStandardAmount": 2,
            "Quantity": 1,
            "Discount": 0,
            "IsDiscounted": 0,
            "ItemTotalPrice": 2
        }
    ]
}

]

I have little to no knowledge of how I am supposed to write the code in JSON so I am kindly asking for your advice, as every time I am sending a request, the if() clauses at the beginning of the method throw me out. The problem seems to be that the values are not being populated so my guess is that the JSON code that I wrote is not properly written. Any tips?



Solution 1:[1]

In my opinion I would combine OrderHistoryDto OrderHistoryItemDto and HistoryLabelDto into one model such as CreatedOrder inside which would contain these three items. That way you only have one item being passed in through the body and you can still easily access the three items.

Solution 2:[2]

Here's some configuration.

First Content-Type should be set to application/json

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jack Tyler
Solution 2 Ed Bangga