Beispiel #1
0
        public async Task <PagedList <Restaurant> > GetAllAsync(RestaurantsResourceParameters restaurantsResourceParameters)
        {
            if (restaurantsResourceParameters == null)
            {
                throw new ArgumentNullException(nameof(restaurantsResourceParameters));
            }

            var collection = _context.Restaurants.Include(r => r.Address) as IQueryable <Restaurant>;

            //filtering
            if (restaurantsResourceParameters.HasDelivery != null)
            {
                collection = collection.Where(r => r.HasDelivery == restaurantsResourceParameters.HasDelivery);
            }

            if (!string.IsNullOrWhiteSpace(restaurantsResourceParameters.SearchQuery))
            {
                var searchQuery = restaurantsResourceParameters.SearchQuery.Trim();

                collection = collection.Where(r =>
                                              r.Name.Contains(searchQuery) ||
                                              r.Description.Contains(searchQuery) ||
                                              r.Category.Contains(searchQuery) ||
                                              r.Address.City.Contains(searchQuery));
            }

            //sorting
            if (string.IsNullOrWhiteSpace(restaurantsResourceParameters.SortBy))
            {
                //default sorting
                return(await PagedList <Restaurant> .ToPagedListAsync(collection.OrderBy(r => r.Name),
                                                                      restaurantsResourceParameters.PageNumber, restaurantsResourceParameters.PageSize));
            }

            //custom sorting
            collection = SortRestaurants(collection, restaurantsResourceParameters.SortBy, restaurantsResourceParameters.SortDirection);

            //pagination
            return(await PagedList <Restaurant> .ToPagedListAsync(collection,
                                                                  restaurantsResourceParameters.PageNumber, restaurantsResourceParameters.PageSize));
        }
        public async Task <ActionResult <IEnumerable <RestaurantDto> > > GetRestaurants([FromQuery] RestaurantsResourceParameters restaurantsResourceParameters)
        {
            var restaurants = await _restaurantsRepository.GetAllAsync(restaurantsResourceParameters);

            var metadata = new
            {
                restaurants.TotalCount,
                restaurants.PagesSize,
                restaurants.CurrentPage,
                restaurants.TotalPages,
                restaurants.HasNext,
                restaurants.HasPrevious,
            };

            Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(metadata));

            return(Ok(_mapper.Map <IEnumerable <RestaurantDto> >(restaurants)));
        }