public void TestFetchFlights_ZAG_ZZZ_BadRequest() { var searchModel = new FlightApiRequestModel(); searchModel.Adults = 2; searchModel.AirportFromCode = "ZAG"; searchModel.AirportToCode = "ZZZ"; searchModel.Currency = Currency.EUR.ToString().ToUpper(); searchModel.FormattedDepartureDate = "2016-11-01"; searchModel.FormattedReturnDate = "2016-11-08"; var service = new FlightApiService(); var apiResponse = service.FindFlights(searchModel); Assert.IsFalse(apiResponse.IsSuccess); Assert.IsNull(apiResponse.Data); }
public ServiceResult <List <ItineraryModel> > Search(FlightSearchModel model) { var searchHash = model.AirportFromId + "." + model.AirportToId + "." + model.DepartureDate.ToShortDateString() + "." + model.ReturnDate?.ToShortDateString() + "." + model.AdultsCount + "." + model.ChildrenCount + "." + model.Currency; var cachedResults = this._flightRepository.GetCached(searchHash); if (cachedResults.Count > 0) { var grouped = cachedResults.GroupBy(p => p.ItineraryEntryId); var result = new List <ItineraryModel>(); foreach (var i in grouped) { var itModel = new ItineraryModel(); itModel.Price = i.First().ItineraryEntryTotalPrice; itModel.InboundFlights = i .Where(p => p.Type == FlightType.Inbound) .OrderBy(p => p.DepartureTime) .ToList(); itModel.OutboundFlights = i .Where(p => p.Type == FlightType.Outbound) .OrderBy(p => p.DepartureTime) .ToList(); result.Add(itModel); } return(this.Ok(result)); } var requestModel = new FlightApiRequestModel(); requestModel.AirportFromCode = this._airportRepository.GetAirportCode(model.AirportFromId); requestModel.AirportToCode = this._airportRepository.GetAirportCode(model.AirportToId); requestModel.Adults = model.AdultsCount; requestModel.Children = model.ChildrenCount; requestModel.Currency = model.Currency.ToString().ToUpper(); requestModel.FormattedDepartureDate = model.DepartureDate.ToString("yyyy-MM-dd"); requestModel.FormattedReturnDate = model.ReturnDate?.ToString("yyyy-MM-dd"); var flightApiResult = this._flightApiService.FindFlights(requestModel); if (!flightApiResult.IsSuccess) { return(this.Error <List <ItineraryModel> >(flightApiResult.ResponseMessage)); } using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions() { IsolationLevel = IsolationLevel.ReadCommitted })) { var searchEntry = new SearchEntryDTO(); searchEntry.Id = Guid.NewGuid(); searchEntry.SearchEntryHash = searchHash; searchEntry.AdultsCount = model.AdultsCount; searchEntry.ChildrenCount = model.ChildrenCount; searchEntry.AirportFromId = model.AirportFromId; searchEntry.AirportToId = model.AirportToId; searchEntry.DepartureDate = model.DepartureDate; searchEntry.ReturnDate = model.ReturnDate; searchEntry.Currency = model.Currency; this._searchEntryRepository.InsertOrUpdate(searchEntry); var usCulture = new CultureInfo("en-us"); foreach (var fareItem in flightApiResult.Data.results) { var price = decimal.Parse(fareItem.fare.total_price, usCulture.NumberFormat); var itineraryEntry = new ItineraryEntryDTO(); itineraryEntry.Id = Guid.NewGuid(); itineraryEntry.SearchEntryId = searchEntry.Id; itineraryEntry.TotalPrice = price; this._itineraryEntryRepository.InsertOrUpdate(itineraryEntry); var outboundAdded = new HashSet <string>(); foreach (var outbound in fareItem.itineraries.Select(i => i.outbound)) { var flightEntry = this.GetFlightEntry(itineraryEntry.Id, outbound.flights, FlightType.Outbound); var hashOutKey = flightEntry.ArrivalTime.ToString() + "." + flightEntry.DepartureTime.ToString() + "." + flightEntry.StopsCount; if (outboundAdded.Contains(hashOutKey)) { continue; } outboundAdded.Add(hashOutKey); this._flightRepository.InsertOrUpdate(flightEntry); } var inboundAdded = new HashSet <string>(); foreach (var inbound in fareItem.itineraries.Select(i => i.inbound).Where(i => i != null)) { var flightEntry = this.GetFlightEntry(itineraryEntry.Id, inbound.flights, FlightType.Inbound); var hashInKey = flightEntry.ArrivalTime.ToString() + "." + flightEntry.DepartureTime.ToString() + "." + flightEntry.StopsCount; if (inboundAdded.Contains(hashInKey)) { continue; } inboundAdded.Add(hashInKey); this._flightRepository.InsertOrUpdate(flightEntry); } } scope.Complete(); } return(this.Search(model)); }