public PageResult <RestaurantDto> GetAll(RestaurantQuerry querry) { var searchPhrase = querry.SearchPhrase; var baseQuerry = _dbContext.Restaurants .Include(r => r.Adress) .Include(r => r.Dishes) .Where(r => ((searchPhrase == null)) || (r.Name.ToLower().Contains(searchPhrase.ToLower()) || r.Description.ToLower().Contains(searchPhrase.ToLower()))); if (!string.IsNullOrEmpty(querry.SortBy)) { var columnSelectors = new Dictionary <string, Expression <Func <Restaurant, object> > > { { nameof(Restaurant.Name), r => r.Name }, { nameof(Restaurant.Description), r => r.Description }, { nameof(Restaurant.Category), r => r.Category } }; var selectedColumn = columnSelectors[querry.SortBy]; if (querry.SortDirection == SortDirection.ASC) { baseQuerry = baseQuerry.OrderBy(selectedColumn); } else { baseQuerry = baseQuerry.OrderByDescending(selectedColumn); } } var restaurants = baseQuerry.Skip(querry.PageSize * (querry.PageNumber - 1)) .Take(querry.PageSize) .ToList(); var restaurantsDtos = _mapper.Map <List <RestaurantDto> >(restaurants); var result = new PageResult <RestaurantDto>(restaurantsDtos, baseQuerry.Count(), querry.PageSize, querry.PageNumber); return(result); }
public ActionResult <IEnumerable <RestaurantDto> > GetAll([FromQuery] RestaurantQuerry querry) { var restaurantsDtos = _restaurantService.GetAll(querry); return(Ok(restaurantsDtos)); }