简体   繁体   中英

How to post data to multiple tables in .net core restApi?

Hello i'm trying to post data to multiple tables in .netcore rest api but I don't know how to get started. I have 3 tables places, rides, and quotes they are all related. So first I need to fill the table places with address lat and lng, 2 places to be filled one for departure one for destination. The table rides with FromePlaceId(FK), ToPlaceID(FK) and Kms which is calculated by google maps api. And then quotes which has rideID(Fk) and the calculated price. I know how to post data with multiple controllers one for places, for rides and quotes. But I wanted to do it on a single route. For eg. "/api/calculatedquote/". Thanks in advance.

You can create a class CalculatedquoteRequest with all your "Tables" and send it to your method "/api/calculatedquote/" as json. You don' t need to create one Controller for each "Table". Example:

Class Request:

    public class CalculatedquoteRequest
{
    public Place Departure { get; set; }
    public Place Destination { get; set; }
    public Ride Ride { get; set; }
    public Quote Quotes { get; set; }
}

public class Place
{
    public string Lat { get; set; } 
    public string Lng { get; set; }
}
public class Ride
{
    //your properties of  Ride
}

public class Quote
{
    //your properties of  Quote
}

Your Controller:

    public class Calculatedquote : Controller
{
    [HttpPost]
    public async Task<ActionResult> Calculate([FromBody] CalculatedquoteRequest request)
    {
       //Here you put your code...
        
        return Ok(null);//You will return your response, not null
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM