public async Task <IActionResult> Search(FlightSearchDTO model)
        {
            FlightSearchResponse availableFlights = null;
            SearchPageViewModel  vm = null;

            if (ModelState.IsValid)
            {
                //create search request object

                SearchRequestModel rm = new SearchRequestModel
                {
                    FromLocation  = model.From,
                    ToLocation    = model.To,
                    DepartureDate = model.DepartureDate.ToShortDateString(),


                    Persons = new List <Person>
                    {
                        new Person
                        {
                            PassengerType = "ADT",
                            Quantity      = 1
                        }
                    },

                    UserData = new UserData
                    {
                        Ip           = "89.134.155.92",
                        BrowserAgent = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0"
                    }
                };

                try
                {
                    rm.ReturnDate = model.ReturnDate.Value.ToShortDateString();
                }
                catch (Exception)
                {
                    rm.ReturnDate = "";
                }

                string        rmString      = JsonConvert.SerializeObject(rm);
                StringContent searchRequest = new StringContent(rmString, Encoding.UTF8, "application/json");

                try
                {
                    string responseString = string.Empty;

                    using (var client = HttpService.GetHttpClient())
                    {
                        while (responseString == string.Empty)
                        {
                            Thread.Sleep(3000);
                            var result = await client.PostAsync("https://dev.allmyles.com/v2.0/flights", searchRequest);

                            responseString = await result.Content.ReadAsStringAsync();
                        }
                    }

                    availableFlights = JsonConvert.DeserializeObject <FlightSearchResponse>(responseString);
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.Message);
                }
            }



            if (availableFlights != null)
            {
                vm            = new SearchPageViewModel();
                vm.FirstLegs  = new List <FlightSegment>();
                vm.ReturnLegs = new List <FlightSegment>();

                foreach (var resultSet in availableFlights.flightResultSet)
                {
                    foreach (var combination in resultSet.combinations)
                    {
                        foreach (var flighSegments in combination.firstLeg.flightSegments)
                        {
                            vm.FirstLegs.Add(flighSegments);
                        }

                        foreach (var flighSegments in combination.returnLeg.flightSegments)
                        {
                            vm.ReturnLegs.Add(flighSegments);
                        }
                    }
                }
            }

            return(View(vm));
        }
Beispiel #2
0
        public async Task <Result <FlightDTO> > AddFlight(ClaimsPrincipal claims, FlightSearchDTO flightDTO)
        {
            try
            {
                var user = await userManager.GetUserAsync(claims);

                if (user is null)
                {
                    return(new NotFoundResult <FlightDTO>("User was not found"));
                }

                var flight = await aviationstackFlightService.GetFlight(flightDTO.FlightNo);

                if (flight is null)
                {
                    return(new NotFoundResult <FlightDTO>("Flight was not found"));
                }

                var flightInDatabase = await repository.GetFlightByDateAndFlightNumberAsync(flight.Data.FirstOrDefault().FlightDate, flight.Data.FirstOrDefault().Flight.Iata);

                if (flightInDatabase is null)
                {
                    var mappingResult = mapper.Map <FlightsResponse, Entity.Flight>(flight);

                    mappingResult.LastUpdated = DateTime.Now;
                    mappingResult.Chatroom    = new Entity.Chatroom()
                    {
                        Flight   = mappingResult,
                        FlightId = mappingResult.Id,
                    };

                    var newFlight = await repository.AddFlightAsync(mappingResult);

                    if (newFlight is null)
                    {
                        return(new InvalidResult <FlightDTO>("Flight not added"));
                    }

                    var addedNewFlight = await repository.AddUserFlightAsync(user, newFlight);

                    if (addedNewFlight is null)
                    {
                        return(new InvalidResult <FlightDTO>("Flight not added to user"));
                    }

                    var addedNewFlightMapping = mapper.Map <Entity.UserFlight, FlightDTO>(addedNewFlight);

                    return(new SuccessResult <FlightDTO>(addedNewFlightMapping));
                }

                var userFlight = await repository.GetUserFlightByIdAsync(user.Id, flightInDatabase.Id);

                if (userFlight != null)
                {
                    return(new InvalidResult <FlightDTO>("Flight is already assigned to user"));
                }

                var addedFlight = await repository.AddUserFlightAsync(user, flightInDatabase);

                if (addedFlight is null)
                {
                    return(new InvalidResult <FlightDTO>("Flight was not added"));
                }

                var addedFlightMapping = mapper.Map <Entity.UserFlight, FlightDTO>(addedFlight);

                return(new SuccessResult <FlightDTO>(addedFlightMapping));
            }
            catch (Exception ex)
            {
                return(new UnexpectedResult <FlightDTO>(ex.Message));
            }
        }
Beispiel #3
0
        public async Task <IActionResult> AddFlight(FlightSearchDTO flight)
        {
            var result = await flightService.AddFlight(User, flight);

            return(this.FromResult(result));
        }