Example #1
0
        public async Task <IActionResult> Index()
        {
            AirportIndexModel viewModel = new AirportIndexModel
            {
                SearchAirport = new AirportRequestModel(),
                Airports      = new List <AirportResponseModel>()
            };

            return(View(viewModel));
        }
Example #2
0
        public async Task <IActionResult> Search(AirportRequestModel airportRequestModel)
        {
            AirportIndexModel viewModel = new AirportIndexModel
            {
                SearchAirport = new AirportRequestModel(),
                Airports      = new List <AirportResponseModel>(),
                Notification  = new AlertNotification()
            };

            if (string.IsNullOrEmpty(airportRequestModel.Name) && string.IsNullOrEmpty(airportRequestModel.Country))
            {
                viewModel.Notification.SetNotification(NotificationType.Error, "You must have atleast a Name/ICAO or Country to Search for Airports!");
                return(View("Index", viewModel));
            }

            List <Airport> airportsFound = new List <Airport>();
            Country        foundCountry  = new Country();

            if (!string.IsNullOrEmpty(airportRequestModel.Country))
            {
                foundCountry = _context.Countries.FirstOrDefault(co => co.Name == airportRequestModel.Country);

                airportsFound = string.IsNullOrEmpty(airportRequestModel.Name)
                                                                                ? await _context.Airports.Where(ap => ap.CountryId == foundCountry.Id).ToListAsync()
                                                                                : await _context.Airports.Where(ap => ap.CountryId == foundCountry.Id && ap.Name == airportRequestModel.Name).ToListAsync();
            }
            else
            {
                airportsFound = string.IsNullOrEmpty(airportRequestModel.Name)
                                                                                ? await _context.Airports.ToListAsync()
                                                                                : await _context.Airports.Where(ap => ap.Name.ToUpper() == airportRequestModel.Name.ToUpper() || ap.ICAO.ToUpper() == airportRequestModel.Name.ToUpper()).ToListAsync();
            }

            List <AirportResponseModel> AllAirports = new List <AirportResponseModel>();

            foreach (var airport in airportsFound)
            {
                if (string.IsNullOrEmpty(foundCountry.Name))
                {
                    foundCountry = _context.Countries.FirstOrDefault(co => airport.CountryId == co.Id);
                }

                AirportResponseModel newAirportResponse = new AirportResponseModel
                {
                    Name    = airport.Name,
                    ICAO    = airport.ICAO,
                    IATA    = airport.IATA,
                    Country = foundCountry
                };
                AllAirports.Add(newAirportResponse);
            }

            viewModel.Airports = AllAirports;
            return(View("Index", viewModel));
        }