Example #1
0
        public async Task <IActionResult> Index(SearchFlightModel model)
        {
            if (ModelState.IsValid)
            {
                var destinationFrom = await _destinationRepository.GetDestinationByNameAsync(model.From);

                //Confirmar a existĂȘncia das cidades
                if (destinationFrom == null)
                {
                    this.ModelState.AddModelError(string.Empty, "Destination From does not exist!");
                    return(View(model));
                }

                var destinationTo = await _destinationRepository.GetDestinationByNameAsync(model.To);

                //Confirmar a existĂȘncia das cidades
                if (destinationTo == null)
                {
                    this.ModelState.AddModelError(string.Empty, "Destination To does not exist!");
                    return(View(model));
                }

                ShowListFlightsModel modelView = new ShowListFlightsModel();
                modelView.Flights = _flightRepository.GetFlightsFromToAndDeparture(model.From, model.To, model.Departure);

                if (modelView.Flights.Count == 0)
                {
                    this.ModelState.AddModelError(string.Empty, "There are no flights from the selected airport!");
                    return(View(model));
                }

                modelView.isRoundTrip = 2; // One-Way

                if (model.Trip == 1)       // Roundtrip
                {
                    modelView.FlightsReturn = _flightRepository.GetFlightsFromToAndDeparture(model.To, model.From, model.Return);
                    modelView.isRoundTrip   = 1;

                    if (modelView.FlightsReturn.Count == 0)
                    {
                        modelView.isRoundTrip = 1;
                        this.ModelState.AddModelError(string.Empty, "There are no flights from the return airport!");
                        return(View(model));
                    }
                }
                // Redireccionar para o booking (levar o modelo)
                TempData.Put("FlightsList", modelView);
                return(RedirectToAction("ViewFlights", "Home"));
            }

            return(View(model));
        }