Пример #1
0
        private IEnumerable <Flight> ConvertFlightApiResponseToFlight(FlightApiResponse flightApiResponse, DestinationPlace destinationPlace, DateTime requestedInboundDate, DateTime requestedOutboundDate)
        {
            List <Flight> flights = new List <Flight>();

            foreach (var itinerary in flightApiResponse.Itineraries)
            {
                List <Flight> subflights = new List <Flight>(new Flight[itinerary.PricingOptions.Count]);
                for (int i = 0; i < itinerary.PricingOptions.Count; i++)
                {
                    subflights[i]       = new Flight();
                    subflights[i].Price = Convert.ToDecimal(itinerary.PricingOptions[i].Price);
                    int   agentId = itinerary.PricingOptions[i].Agents.First();
                    Agent agent   = flightApiResponse.Agents.Single(a => a.Id == agentId);
                    subflights[i].AgentName             = agent.Name;
                    subflights[i].AgentType             = agent.Type.ToLower() == "Airline" ? AgentType.AIRLINEE : AgentType.TRAVEL_AGENT;
                    subflights[i].linkToBookAgent       = new Uri(itinerary.PricingOptions[i].DeeplinkUrl);
                    subflights[i].DestinationPlace      = destinationPlace;
                    subflights[i].RequestedInboundDate  = requestedInboundDate;
                    subflights[i].RequestedOutboundDate = requestedOutboundDate;
                }
                Leg outboundLeg = flightApiResponse.Legs.Single(l => l.Id == itinerary.OutboundLegId);
                Leg inboundLeg  = flightApiResponse.Legs.Single(l => l.Id == itinerary.InboundLegId);

                foreach (Flight flight in subflights)
                {
                    flight.InboundDepartureDate  = inboundLeg.Departure;
                    flight.InboundArrivalDate    = inboundLeg.Arrival;
                    flight.OutboundArrivalDate   = outboundLeg.Arrival;
                    flight.OutboundDepartureDate = outboundLeg.Departure;
                }
                flights.AddRange(subflights);
            }
            return(flights);
        }
Пример #2
0
        public IEnumerable <Flight> GetFlights(FlightSearchParams flightSearchParam, FlightSerachFilter flightSerachFilter)
        {
            IEnumerable <Flight> flightsResponse = null;

            try
            {
                FlightApiSearchParams flightApiSearchParam = new FlightApiSearchParams();
                flightApiSearchParam.DestinationPlace = flightSearchParam.DestinationPlace;
                flightApiSearchParam.InboundDate      = flightSearchParam.InboundDate;
                flightApiSearchParam.OutboundDate     = flightSearchParam.OutboundDate;
                FlightsRetriever flightsRetriever = new FlightsRetriever();
                List <Flight>    flights          = flightsRetriever.GetFlightsBySearchParams(flightSearchParam.DestinationPlace, flightSearchParam.InboundDate, flightSearchParam.OutboundDate).ToList();
                // check if flights exist in db
                if (flights.Any() == false)
                {
                    log.Info(string.Format("Flights with params : {0},{1},{2} not exist in db ", flightSearchParam.DestinationPlace, flightSearchParam.OutboundDate.ToString("dd/MM/yyyy"), flightSearchParam.InboundDate.ToString("dd/MM/yyyy")));
                    string            sessionId         = this.CreateSession(flightApiSearchParam);
                    FlightApiResponse flightApiResponse = this.PullSessionResult(sessionId);
                    flights = this.ConvertFlightApiResponseToFlight(flightApiResponse, flightSearchParam.DestinationPlace, flightSearchParam.InboundDate, flightSearchParam.OutboundDate).ToList();
                    // store flight in db
                    flightsRetriever.InsertFlights(flights);
                }
                else
                {
                    log.Info(string.Format("Flights with params : {0},{1},{2} found in db ", flightSearchParam.DestinationPlace, flightSearchParam.OutboundDate.ToString("dd/MM/yyyy"), flightSearchParam.InboundDate.ToString("dd/MM/yyyy")));
                }
                FlightFilterProcessor flightFilterProcessor = new FlightFilterProcessor();
                flightsResponse = flightFilterProcessor.FilterFlights(flights, flightSerachFilter);
            }
            catch (Exception e)
            {
                log.Info(string.Format("GetFlights error with params : {0},{1},{2}   ", flightSearchParam.DestinationPlace, flightSearchParam.InboundDate.ToString("dd/MM/yyyy"), flightSearchParam.OutboundDate.ToString("dd/MM/yyyy")));
                log.Error(string.Format("Error is : {0} stack trace is : {1} ", e.Message, e.StackTrace));
            }
            return(flightsResponse);
        }