Ejemplo n.º 1
0
        public ViewResult Search(SearchModel connSearch)
        {
            var allAvailableConnections = connectionRepository.AllIncluding(t => t.Departure, t => t.Destination, t => t.Departure.IcaoCountryCode.Country, t => t.Destination.IcaoCountryCode.Country, t => t.AircraftType).OrderBy(t => t.Departure.Name.Length).ThenBy(t => t.Destination.Name.Length);

            connSearch.PossibleDepartureAirports = allAvailableConnections.Select(t => t.Departure).Distinct().OrderBy(t => t.Name);
            connSearch.PossibleDestinationAirports = allAvailableConnections.Select(t => t.Destination).Distinct().OrderBy(t => t.Name);
            connSearch.PossibleAircraftTypes = new MultiSelectList(allAvailableConnections.Select(t => t.AircraftType).Distinct().ToList().OrderBy(t => t.Longname), "ICAO", "Longname");

            connSearch.AircraftTypeIDs = UserContext.GetCurrent().User.UserTypeRatings.Where(t => t.ValidTo == null).SelectMany(t => t.TypeRating.AircraftTypes.Select(g => g.ICAO)).ToArray();

            connSearch.PossibleAirlines = new List<SelectListItem>();
            connSearch.PossibleAirlines.Add(new SelectListItem() { Text = Schedule.AllAirlines, Value = "all" });
            connSearch.PossibleAirlines.AddRange(allAvailableConnections.Select(t => t.Airline).Distinct().Select(t => new SelectListItem() { Text = t.Name, Value = t.ICAO }).ToList());

            connSearch.Date = DateTime.UtcNow;

            return View(connSearch);
        }
Ejemplo n.º 2
0
        public PartialViewResult GetConnections(SearchModel viewModel)
        {
            TimeSpan minStartTime, maxStartTime;
            TimeSpan minDuration, maxDuration;

            if (viewModel.WithDepTime) {
                MatchCollection startTimeCollection = Regex.Matches(viewModel.StartTime, @"([0-9]{1,2}:[0-9]{2})");
                minStartTime = TimeSpan.Parse(startTimeCollection[0].Value);

                maxStartTime = new TimeSpan(int.Parse(startTimeCollection[1].Value.Split(':')[0]),    // hours
                               int.Parse(startTimeCollection[1].Value.Split(':')[1]),    // minutes
                               0);
            } else {
                minStartTime = maxStartTime = TimeSpan.Zero;
            }

            if (viewModel.WithMaxDuration) {
                MatchCollection durationCollection = Regex.Matches(viewModel.Duration, @"([0-9]{1,2}:[0-9]{2})");
                minDuration = TimeSpan.Parse(durationCollection[0].Value);
                maxDuration = new TimeSpan(int.Parse(durationCollection[1].Value.Split(':')[0]),    // hours
                   int.Parse(durationCollection[1].Value.Split(':')[1]),    // minutes
                   0);
            } else {
                minDuration = maxDuration = TimeSpan.Zero;
            }

            DayOfWeek weekday = viewModel.Date.DayOfWeek;

            var list = connectionRepository.AllIncluding(t => t.AircraftType)
                .Where(t =>
                    weekday == DayOfWeek.Monday ? t.Monday :
                        weekday == DayOfWeek.Tuesday ? t.Tuesday :
                            weekday == DayOfWeek.Wednesday ? t.Wednesday :
                                weekday == DayOfWeek.Thursday ? t.Thursday :
                                    weekday == DayOfWeek.Friday ? t.Friday :
                                        weekday == DayOfWeek.Saturday ? t.Saturday :
                                            weekday == DayOfWeek.Sunday ? t.Sunday :
                                                false
                    )
                .Where(t => (!t.StartsOn.HasValue || t.StartsOn <= viewModel.Date) && (!t.EndsOn.HasValue || t.EndsOn >= viewModel.Date))
                .WhereIf(viewModel.AircraftTypeIDs != null && viewModel.AircraftTypeIDs.Any(), t => viewModel.AircraftTypeIDs.Contains(t.AircraftTypeICAO))
                .WhereIf(!string.IsNullOrEmpty(viewModel.DepartureAirport), t => t.DepartureAirportICAO == viewModel.DepartureAirport)
                .WhereIf(!string.IsNullOrEmpty(viewModel.DestinationAirport), t => t.DestinationAirportICAO == viewModel.DestinationAirport)
                .WhereIf(!viewModel.AirlineIDs.Contains("all") && viewModel.AirlineIDs.Any(), t => viewModel.AirlineIDs.Contains(t.AirlineICAO))
                .WhereIf(viewModel.FlightNumber.HasValue, t => t.FlightNumber == viewModel.FlightNumber).ToList();

            if(viewModel.WithDepTime) {
                list = list.Where(t => t.DepTimeUtc(viewModel.Date) >= minStartTime && t.DepTimeUtc(viewModel.Date) <= maxStartTime).ToList();
            }

            if (viewModel.WithMaxDuration) {
                list = list.Where(t => t.FlightTime(viewModel.Date) <= maxDuration && t.FlightTime(viewModel.Date) >= minDuration).ToList();
            }

            viewModel.Connections = list.OrderBy(t => t.DepTimeUtc(viewModel.Date)).ToList();
            viewModel.ShowReverseButton = true;

            return PartialView("List", viewModel);
        }