Пример #1
0
        public IQueryable <ItemModel> SearchResults(SearchFiltersModel searchFilters, PagingModel paging, SortingFiltersModel sorting)
        {
            //in case page requested is higher than existing number of pages
            var page = paging.Page;

            //skip calculation
            int skip = paging.ResultsPerPage * (page - 1);

            IQueryable <ItemModel> items;

            //filter by humidor size
            if (searchFilters.CategoryFilters.Name == "Humidors" && searchFilters.HumidorSizeFilters.Id != 0)
            {
                items = itemRepository.AsItemModelByHumidorSize(searchFilters.HumidorSizeFilters.EqualityValue);
            }
            //filter by color
            else if (searchFilters.CategoryFilters.Name == "Lighters" && searchFilters.ColorFilters.Id != 0)
            {
                items = itemRepository.AsItemModelByColor(searchFilters.ColorFilters.EqualityValue);
            }
            //search items
            else
            {
                //IQueryable<ItemModel> items = itemRepository.AsItemModel();
                items = itemRepository.AsItemModel();
            }

            //filter by category
            items = itemRepository.ByCategory(items, searchFilters.CategoryFilters.EqualityValue);

            //filter by price
            if (searchFilters.PriceFilters.Id != 0)
            {
                items = itemRepository.ByPrice(items, searchFilters.PriceFilters.Min, searchFilters.PriceFilters.Max);
            }

            //order by
            switch (sorting.EqualityValue)
            {
            case "priceAsc":
                items = items.OrderBy(r => r.Price).ThenBy(r => r.Name);
                break;

            case "priceDesc":
                items = items.OrderByDescending(r => r.Price).ThenBy(r => r.Name);
                break;

            case "nameAsc":
                items = items.OrderBy(r => r.Name).ThenBy(r => r.Price);
                break;

            case "nameDesc":
                items = items.OrderByDescending(r => r.Name).ThenBy(r => r.Price);
                break;

            default:
                items = items.OrderByDescending(r => r.VoteCount).ThenBy(r => r.Name);
                break;
            }

            //skip, take
            items = items.Skip(skip).Take(paging.ResultsPerPage);

            return(items);
        }