public IActionResult ListSearchResults(string categoryOrBrand, string sortBy, string query) { IEnumerable <RatingEntry> ratingEntries = ratingEntryRepository.AllRatings; List <RatedShoe> ratedShoes = new List <RatedShoe>(); bool isInitialSearch = !string.IsNullOrEmpty(query); if (isInitialSearch) { IEnumerable <Shoe> shoes = shoeRepository.GetShoesByNameOrBrandOrCategory(query); ratedShoes = ShoeRatingController.CreateRatedShoeList(shoes, ratingEntries, ratedShoes).ToList(); return(View("ListSearchResults", new ShoesListViewModel { RatedShoes = ratedShoes, CurrentCategoryOrBrand = $"Search results for {query}" })); } else { SortBy sortByEnum = SortByStringToEnum(sortBy); ratedShoes = ShowShoesWithSortByOption(ratedShoes, categoryOrBrand, ratingEntries, sortByEnum); return(View("ListSearchResults", new ShoesListViewModel { CurrentCategoryOrBrand = categoryOrBrand, RatedShoes = ratedShoes })); } }
private List <RatedShoe> ShowShoesWithSortByOption(List <RatedShoe> ratedShoes, string categoryOrBrand, IEnumerable <RatingEntry> ratingEntries, SortBy sortByEnum) { //categoryOrBrand is set to "Search results for ________(the query)" from the last request var categoryOrBrandArray = categoryOrBrand.Split(" "); List <string> queryIsolated = new List <string>(); //take just the words of the query, i.e. after "Search results for" for (int i = 3; i < categoryOrBrandArray.Length; i++) { queryIsolated.Add(categoryOrBrandArray[i]); } string combinedQuery = ""; foreach (string s in queryIsolated) { combinedQuery += $"{s} "; } IEnumerable <Shoe> shoes = shoeRepository.GetShoesByNameOrBrandOrCategory(combinedQuery.Trim()); ratedShoes = ShoeRatingController.CreateRatedShoeList(shoes, ratingEntries, ratedShoes).ToList(); if (sortByEnum == SortBy.ascendingByPrice) { ratedShoes = ratedShoes.OrderBy(s => s.Shoe.Price).ToList(); } else if (sortByEnum == SortBy.descendingByPrice) { ratedShoes = ratedShoes.OrderByDescending(s => s.Shoe.Price).ToList(); } else if (sortByEnum == SortBy.ascendingByRating) { ratedShoes = ratedShoes.OrderBy(s => s.OverallRating).ToList(); } else { ratedShoes = ratedShoes.OrderByDescending(s => s.OverallRating).ToList(); } return(ratedShoes); }
public IActionResult ListShoes(string categoryOrBrand, string sortBy) { IEnumerable <RatingEntry> ratingEntries = ratingEntryRepository.AllRatings; List <RatedShoe> ratedShoes = new List <RatedShoe>(); ratedShoes = ShoeRatingController.CreateRatedShoeList(shoeRepository.AllShoes, ratingEntries, ratedShoes).ToList(); bool NoCategoryOrBrandNorAscendingOrDescendingSelected = string.IsNullOrEmpty(categoryOrBrand) && string.IsNullOrEmpty(sortBy); bool AscendingOrDescendingButNoCategoryOrBrandSelected = string.IsNullOrEmpty(categoryOrBrand) && !string.IsNullOrEmpty(sortBy); if (NoCategoryOrBrandNorAscendingOrDescendingSelected) //i.e. just all shoes { return(View(new ShoesListViewModel { CurrentCategoryOrBrand = categoryOrBrand, RatedShoes = ratedShoes.OrderBy(r => r.Shoe.ShoeId).ToList() })); } else if (AscendingOrDescendingButNoCategoryOrBrandSelected) { SortBy sortByEnum = SortByStringToEnum(sortBy); return(View(new ShoesListViewModel { CurrentCategoryOrBrand = categoryOrBrand, RatedShoes = ShowShoesAscendingOrDescendingWithNoCategoryOrBrand(ratedShoes, sortByEnum) })); } else { return(View(new ShoesListViewModel { CurrentCategoryOrBrand = categoryOrBrand, RatedShoes = ShowShoesWithCategoryOrBrandSelection(categoryOrBrand, ratedShoes, sortBy) })); } }