public async Task <ActionResult <List <Flights> > > Get([FromQuery] string relative_to) { try { string urlRequest = Request.QueryString.Value; List <Flights> flights = new List <Flights>(); FlightsFromServers flightsFromServers = new FlightsFromServers(flights, false); if (urlRequest.Contains("sync_all")) { flightsFromServers = await manager.GetAllFlights(relative_to, true); } else { flightsFromServers = await manager.GetAllFlights(relative_to, false); } // At least one response in not valid. if (flightsFromServers.IsError) { return(BadRequest()); } return(Ok(flightsFromServers.FlightsList)); } catch (Exception e) { return(NotFound(e.Message)); } }
public async Task <ActionResult <IEnumerable <Flight> > > GetAllFlights ([FromQuery(Name = "relative_to")] string relativeTo) { // Checks if the words "sync_all" is in the query - checks other servers if it's true. string request = Request.QueryString.Value; bool isExternal = request.Contains("sync_all"); IEnumerable <Flight> flights = null; try { flights = await manager.GetAllFlights(relativeTo, isExternal); } // Problem with connection to other servers. catch (HttpRequestException) { return(BadRequest("problem in request to servers")); } // Problem with date and time format. catch (FormatException) { return(BadRequest("Date and time not in format")); } catch (Exception e) { // Timeout - servers didn't bring the flights fast enough if (e.Message == "The operation was canceled.") { return(BadRequest("Timeout - server didn't bring the flights")); } return(BadRequest(e.Message)); } // Returns list of flights from server/s. return(Ok(flights)); }
public IEnumerable <Flight> Get(DateTime relativeTo) { List <Flight> flightsWithRelaticTime = manager.GetAllFlights().Where(x => x.Date) return(manager.GetAllFlights()); }
//[HttpGet("{id}", Name = "Get")] public async Task <ActionResult <Flight[]> > Get(string relative_to) { bool isSync = false; string s = Request.QueryString.Value; if (s.Contains("sync_all")) { isSync = true; } List <Flight> returnList = new List <Flight>(); //if its the first GET request: // return array of that: 1.externalIs=false 2. Time is now if (!isSync) { DateTime parsedDate = DateTime.Parse(relative_to); parsedDate = TimeZoneInfo.ConvertTimeToUtc(parsedDate); foreach (var f in model.GetAllFlights()) { FlightPlan p; planModel.GetAllPlans().TryGetValue(f.Value.FlightId, out p); //find the start time as appear in the initial time in the flight plan DateTime startTime = TimeZoneInfo.ConvertTimeToUtc(DateTime.Parse(p.InitialLocation.DateTime)); //update current location of flight CurrentFlightLocation(startTime, parsedDate, f.Value); DateTime endTime = CalcEndTime(startTime, f.Value); //update end time of flight f.Value.EndTime = endTime.ToString("yyyy-MM-ddTHH:mm:ssZ"); int resultAfterStart = DateTime.Compare(parsedDate, startTime); int resultBeforeEnd = DateTime.Compare(parsedDate, endTime); if ((resultAfterStart >= 0) && (resultBeforeEnd <= 0)) { if (f.Value.IsExternal == false) { returnList.Add(f.Value); } } } Flight[] array = returnList.ToArray(); return(array); } else { //Sync all: Its the second GET request: ALL flights from this time //FIRST PART - get all internal flights (flights source: drag & drop) DateTime parsedDate = DateTime.Parse(relative_to); parsedDate = TimeZoneInfo.ConvertTimeToUtc(parsedDate); foreach (var f in model.GetAllFlights()) { FlightPlan p; planModel.GetAllPlans().TryGetValue(f.Value.FlightId, out p); //find the start time as appear in the initial time in the flight plan DateTime startTime = TimeZoneInfo.ConvertTimeToUtc(DateTime.Parse(p.InitialLocation.DateTime)); //update current location of flight CurrentFlightLocation(startTime, parsedDate, f.Value); DateTime endTime = CalcEndTime(startTime, f.Value); //update end time of flight f.Value.EndTime = endTime.ToString("yyyy-MM-ddTHH:mm:ssZ"); int resultAfterStart = DateTime.Compare(parsedDate, startTime); int resultBeforeEnd = DateTime.Compare(parsedDate, endTime); if ((resultAfterStart >= 0) && (resultBeforeEnd <= 0)) { returnList.Add(f.Value); } } //second part - get all exnternal flights (flights source: other servers) bool flagIsError = false; foreach (var server in serverModel.GetAllServers()) { try { string url = server.Value.ServerURL + "/api/Flights?relative_to=" + relative_to; List <Flight> flightsFromServer; try { var contentt = await this.client.GetStringAsync(url); flightsFromServer = JsonConvert.DeserializeObject <List <Flight> >(contentt); } catch { flagIsError = true; continue; } foreach (var externalFlight in flightsFromServer) { serverModel.GetServerToFlightDic().AddOrUpdate(externalFlight.FlightId, server.Value, (oldKey, oldVal) => server.Value); externalFlight.IsExternal = true; string url2 = server.Value.ServerURL + "/api/FlightPlan/" + externalFlight.FlightId; var plan = await this.client.GetStringAsync(url2); FlightPlan planFromServer = JsonConvert.DeserializeObject <FlightPlan>(plan); DateTime startTime = TimeZoneInfo.ConvertTimeToUtc(DateTime.Parse(planFromServer.InitialLocation.DateTime)); DateTime endTime = CalcEndTimeForExternal(startTime, planFromServer); externalFlight.EndTime = endTime.ToString("yyyy-MM-ddTHH:mm:ssZ"); int resultAfterStart = DateTime.Compare(parsedDate, startTime); int resultBeforeEnd = DateTime.Compare(parsedDate, endTime); if ((resultAfterStart >= 0) && (resultBeforeEnd <= 0)) { returnList.Add(externalFlight); } } } catch (Exception e) { throw e; } } if (flagIsError) { return(BadRequest()); } Flight[] array = returnList.ToArray(); return(array); } }