public async Task <ActionResult <IReadOnlyCollection <FlightDto> > > GetAllFlightsForPilotInDateRangeAsync(
            [FromRoute] int pilotId, [FromBody] DateRangeParams dates)
        {
            var validateDate = dates.ValidateDate();

            if (!validateDate)
            {
                return(NotFound("Cannot validate date"));
            }

            var pilot = await _pilotsService.GetPilotAsync(pilotId);

            if (pilot == null)
            {
                return(NotFound("Couldn't find any associated Pilot"));
            }

            var flights = await _flightsService.GetAllFlightsForPilotInDateRangeAsync(pilotId, dates);

            if (flights == null || flights.Count == 0)
            {
                return(NotFound("Oops, empty collection"));
            }

            return(Ok(flights));
        }
        public static bool ValidateDate(this DateRangeParams dates)
        {
            var beginDate = DateTime.Parse(dates.BeginDate);
            var endDate   = DateTime.Parse(dates.EndDate);

            if (endDate < beginDate)
            {
                return(false);
            }

            return(true);
        }
Example #3
0
        public async Task <IReadOnlyCollection <FlightDto> > GetAllFlightsForPilotInDateRangeAsync(int pilotId, DateRangeParams dates)
        {
            var begin = DateTime.Parse(dates.BeginDate);
            var end   = DateTime.Parse(dates.EndDate);

            var pilot = await _paraContext.Pilots
                        .AsNoTracking()
                        .FirstOrDefaultAsync(p => p.ID == pilotId);

            if (pilot == null)
            {
                return(null);
            }

            var flights = _paraContext.Flights
                          .AsNoTracking()
                          .Where(f => f.PilotID == pilotId &&
                                 f.FlightDate >= begin &&
                                 f.FlightDate <= end)
                          .MapFlightCollection();

            return(await flights.ToListAsync());
        }