public override async Task <AirportResponseModel> GetAirport(AirportFilter request, ServerCallContext context) { var response = new AirportResponseModel(); try { var airport = await _airportRepository.GetAirport(request.City); if (airport == null) { response.Message = $"Airport {request.City} does not exist"; _logger.LogInformation($"Get Airport {request.City} does not exist"); return(response); } response.City = airport.City; response.Country = airport.Country; response.Name = airport.Name; _logger.LogInformation($"Get {airport.Name} Aircraft"); } catch (Exception ex) { _logger.LogError(ex, $"ERROR: Get {request.City} Aircraft"); } return(response); }
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)); }