/* * get flight plan from the client and save it. */ public async Task <ActionResult> Post([FromBody] FlightPlan fp) { fp.Id = await GenerateIdAsync(); if (!fp.IsValid()) { return(BadRequest("flight plan is invalid")); } await _fpDb.SaveFP(fp); await _flightToServerDb.SaveFlightToServer(fp.Id, null); return(CreatedAtAction("GetFP", new { id = fp.Id }, fp)); }
/* * return flight plan of flight with specific id. */ public async Task <ActionResult <FlightPlan> > GetFP(string id) { string serverId = await _flightToServerDb.LoadFlightServer(id); if (serverId != null && serverId.Equals("Not Found")) { return(NotFound()); } if (serverId == null) { FlightPlan fp = await _fpDb.LoadFP(id); if (fp == null) { return(NotFound()); } return(fp); } Server server = await _serverDb.LoadServer(serverId); HttpResponseMessage response; try { response = await _client.GetAsync(new string(server.Url + "/api/FlightPlan/" + id)); } catch (Exception) { return(StatusCode(500, "cant get respone from other server")); } if (!response.IsSuccessStatusCode) { if (response.StatusCode == System.Net.HttpStatusCode.NotFound) { return(new NotFoundObjectResult("the extarnal server didn't find the flight plan")); } return(StatusCode(500, "problem in the respone from external server")); } var resp = await response.Content.ReadAsStringAsync(); FlightPlan flightPlan = JsonConvert.DeserializeObject <FlightPlan>(resp); if (flightPlan == null || !flightPlan.IsValid()) { return(StatusCode(500, "problem in the respone from external server")); } return(flightPlan); }
public async Task <ActionResult <FlightPlan> > PostFlightPlan(FlightPlan plan) { if (!plan.IsValid()) { return(BadRequest("Flight plan isn't valid, couldn't post")); } string id = Utiles.GenerateId(plan.CompanyName); bool isAdd = _flightPlans.TryAdd(id, plan); if (!isAdd) { return(BadRequest("Error in POST flight lan")); } var retval = CreatedAtAction(actionName: "GetFlightPlan", new { id }, plan); return(await Task.FromResult(retval)); }