Exemplo n.º 1
0
        public async Task <IReadOnlyCollection <FlightDto> > GetAllFlightsAsync(FlightsSSFP options)
        {
            var flights = _paraContext.Flights
                          .AsNoTracking()
                          .FilterFlightBy(options.FilterBy, options.TakeOffSiteId, options.LandingSiteId, options.ParagliderId)
                          .SortFlightBy(options.SortBy)
                          .Select(f => new FlightDto
            {
                FlightId        = f.ID,
                FlightDate      = f.FlightDate,
                Duration        = f.Duration,
                PilotName       = $"{f.Pilot.FirstName} {f.Pilot.LastName}",
                ParagliderName  = f.Paraglider.Name,
                TakeOffSiteName = f.TakeOffSite.Name,
                LandingSiteName = f.LandingSite.Name,
                ParagliderId    = f.ParagliderID,
                LandingSiteId   = f.LandingSiteID,
                TakeOffSiteId   = f.TakeOffSiteID
            });

            options.SetPagingValues(flights);

            var pageQuery = flights.Page(options.PageNumber - 1, options.PageSize);

            return(await pageQuery.ToListAsync());
        }
        private string CreateResourceUri(FlightsSSFP options, ResourceUriType type)
        {
            switch (type)
            {
            case ResourceUriType.PreviousPage:
                return(Url.Link("GetAllFlightssAsync",
                                new
                {
                    PageNumber = options.PageNumber = 1,
                    options.PageSize,
                    options.FilterBy,
                    options.SortBy
                }));

            case ResourceUriType.NextPage:
                return(Url.Link("GetAllFlightsAsync",
                                new
                {
                    PageNumber = options.PageNumber + 1,
                    options.PageSize,
                    options.FilterBy,
                    options.SortBy
                }));

            default:
                return(Url.Link("GetAllFlightsAsync",
                                new
                {
                    options.PageNumber,
                    options.PageSize,
                    options.FilterBy,
                    options.SortBy
                }));
            }
        }
        public async Task <ActionResult <IReadOnlyCollection <FlightDto> > > GetAllFlightsAsync([FromQuery] FlightsSSFP options)
        {
            var flights = await _flightsService.GetAllFlightsAsync(options);

            if (flights == null)
            {
                return(NotFound("No flights found"));
            }

            var previousPageLink = options.HasPrevious ? CreateResourceUri(options, ResourceUriType.PreviousPage) : null;
            var nextPageLink     = options.HasNext ? CreateResourceUri(options, ResourceUriType.NextPage) : null;

            var paginationMetadata = new
            {
                options.TotalCount,
                options.PageSize,
                options.PageNumber,
                options.TotalPages,
                options.SortBy,
                previousPageLink,
                nextPageLink
            };

            Response.Headers.Add("X-Pagination", JsonSerializer.Serialize(paginationMetadata));

            return(Ok(flights));
        }