예제 #1
0
        private async Task <FilteredFlightResponse> GetFilteredFlightResponseDefault(FlightFilter filter)
        {
            FilteredFlightResponse result;

            var locations = (await _locationsRepository.GetAsync(location =>
                                                                 location.Id == filter.OriginLocationId ||
                                                                 location.Id == filter.DestinationLocationId)).ToList();

            var generatedDepartureEntities = (await _entityGenerator.GenerateRandomEntities(locations, filter.OriginDateTime, 10))
                                             .Where(f => f.StartLocationId == filter.OriginLocationId)
                                             .OrderBy(f => f.StartDateTime);
            var filteredDepartureFlights = generatedDepartureEntities.Where(e => e.StartLocationId == filter.OriginLocationId)
                                           .Select(f =>
            {
                f.StartLocation = locations.Where(l => l.Id == f.StartLocationId).FirstOrDefault();
                f.EndLocation   = locations.Where(l => l.Id == f.EndLocationId).FirstOrDefault();
                return(f);
            });

            if (filter.ReturnDateTime != null)
            {
                var generatedArrivalEntities = (await _entityGenerator.GenerateRandomEntities(locations, ((DateTime)filter.ReturnDateTime), 10))
                                               .Where(f => f.StartLocationId == filter.OriginLocationId)
                                               .OrderBy(f => f.StartDateTime);
                var filteredArrivalFlights = generatedArrivalEntities.Where(e => e.EndLocationId == filter.DestinationLocationId)
                                             .Select(f =>
                {
                    f.StartLocation = locations.Where(l => l.Id == f.EndLocationId).FirstOrDefault();
                    f.EndLocation   = locations.Where(l => l.Id == f.StartLocationId).FirstOrDefault();
                    return(f);
                });

                result = new FilteredFlightResponse()
                {
                    Departure = FilteredFlightsMapper.Mapper.Map <List <FlightResponse> >(filteredDepartureFlights),
                    Arrival   = FilteredFlightsMapper.Mapper.Map <List <FlightResponse> >(filteredArrivalFlights)
                };
            }
            else
            {
                result = new FilteredFlightResponse()
                {
                    Departure = FilteredFlightsMapper.Mapper.Map <List <FlightResponse> >(filteredDepartureFlights),
                    Arrival   = null
                };
            }

            return(result);
        }
예제 #2
0
        public static async Task SeedAsync(ScannerContext scannerContext, ILoggerFactory loggerFactory, IEntityGenerator <Flight, Location> entityGenerator, int?retry = 0)
        {
            int retryForAvailability = retry.Value;

            try
            {
                if (!scannerContext.CabinClasses.Any())
                {
                    scannerContext.CabinClasses.AddRange(GetPreconfiguredCabinClasses());
                    await scannerContext.SaveChangesAsync();
                }

                if (!scannerContext.Locations.Any())
                {
                    scannerContext.Locations.AddRange(await GetLocationsFromApi());
                    await scannerContext.SaveChangesAsync();
                }

                if (!scannerContext.Flights.Any())
                {
                    scannerContext.Flights.AddRange(await entityGenerator.GenerateRandomEntities(await scannerContext.Locations.ToListAsync(), 500000));
                    await scannerContext.SaveChangesAsync();
                }

                if (!scannerContext.Tickets.Any())
                {
                    scannerContext.Tickets.AddRange(await GetPreconfiguredTickets(scannerContext));
                    await scannerContext.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                if (retryForAvailability < 3)
                {
                    retryForAvailability++;
                    var log = loggerFactory.CreateLogger <ScannerContextSeed>();
                    log.LogError(ex.Message);
                    await SeedAsync(scannerContext, loggerFactory, entityGenerator, retryForAvailability);
                }
            }
        }