コード例 #1
0
        public async Task <IEnumerable <FlightViewModel> > GetFlightsAsync(FlightForSearchModel model)
        {
            var flights = _context.Flights
                          .Include(flight => flight.DepartureAirport)
                          .Include(flight => flight.ArrivalAirport)
                          .Include(flight => flight.Tickets)
                          .ThenInclude(ticket => ticket.TicketType)
                          .AsQueryable();

            if (model.departureAirportId != null)
            {
                flights = flights.Where(flight => flight.DepartureAirportId == model.departureAirportId);
            }

            if (model.arrivalAirportId != null)
            {
                flights = flights.Where(flight => flight.ArrivalAirportId == model.arrivalAirportId);
            }

            if (model.date != default(DateTime))
            {
                flights = flights.Where(flight => flight.DepartureDate.Date == model.date.Date);
            }

            if (model.ticketsNumber != null)
            {
                flights = flights.Where(flight => flight.Tickets.Count >= model.ticketsNumber);
            }

            if (!string.IsNullOrEmpty(model.Status))
            {
                flights = flights.Where(flight => flight.Status == model.Status);
            }

            var flightsSearchResults = await flights
                                       .OrderBy(flight => flight.Id)
                                       .ToListAsync();

            var results = _mapper.Map <IEnumerable <FlightViewModel> >(flightsSearchResults);

            return(results);
        }
コード例 #2
0
        public async Task <ActionResult <IEnumerable <Flight> > > GetFlights([FromQuery] FlightForSearchModel model)
        {
            var results = await _flightService.GetFlightsAsync(model);

            return(Ok(results));
        }