private IQueryable <PostVM> FilterPosts(PostFilterVM filter, IQueryable <PostVM> query) { if (string.IsNullOrWhiteSpace(filter.OrderBy)) { query = query.OrderBy(q => q.Sneaker.Name); } else { switch (filter.OrderBy.Trim().ToLower()) { case "alphabetical": { query = query.OrderBy(q => q.Sneaker.Name); } break; case "rating": { query = query.OrderBy(q => q.Sneaker.Ratings.Average(s => s.Rating.Score)); } break; case "category": { query = query.OrderBy(q => q.Sneaker.Categories.First().Category.Name); } break; } } query = query.Where(q => q.Sneaker.Ratings.Average(m => m.Rating.Score) >= filter.RatingAbove); if (!string.IsNullOrWhiteSpace(filter.SneakerName)) { query = query.Where(q => q.Sneaker.Name.ToLower().Contains(filter.SneakerName.ToLower().Trim())); } if (!string.IsNullOrWhiteSpace(filter.SneakerCategory)) { query = query.Where(q => q.Sneaker.Categories.First().Category.Name.ToLower() == filter.SneakerCategory); } if (!string.IsNullOrWhiteSpace(filter.Keyword)) { query = query.Where(q => q.Sneaker.Keywords.Any(k => k.Name == filter.Keyword)); } return(query); }
private void LoadListSneakersDropdowns(PostFilterVM postFilter) { this.ViewBag.OrderBy = new List <SelectListItem> { new SelectListItem { Text = "Alphabetical", Value = "alphabetical", Selected = postFilter.OrderBy == "alphabetical" }, new SelectListItem { Text = "Category", Value = "category", Selected = postFilter.OrderBy == "category" }, new SelectListItem { Text = "Rating", Value = "rating", Selected = postFilter.OrderBy == "rating" }, }; this.ViewBag.Ratings = new List <SelectListItem> { new SelectListItem { Text = "0", Value = "0", Selected = postFilter.RatingAbove == 0 }, new SelectListItem { Text = "1", Value = "1", Selected = postFilter.RatingAbove == 1 }, new SelectListItem { Text = "2", Value = "2", Selected = postFilter.RatingAbove == 2 }, new SelectListItem { Text = "3", Value = "3", Selected = postFilter.RatingAbove == 4 }, new SelectListItem { Text = "4", Value = "4", Selected = postFilter.RatingAbove == 4 }, new SelectListItem { Text = "5", Value = "5", Selected = postFilter.RatingAbove == 5 }, }; this.ViewBag.SneakerCategories = this.sneakerCategoryService.GetAll().OrderBy(e => e.Name) .Select(e => new SelectListItem { Text = e.Name, Value = e.Name.ToLower(), Selected = postFilter.SneakerCategory == e.Name.ToLower() }) .ToList(); }
public IActionResult Index(PaginationVM pagination, PostFilterVM postFilter) { if (this.HasAlert) { this.SetAlertModel(); } var configuration = new MapperConfiguration(cfg => cfg.CreateMap <SalePost, PostVM>().ReverseMap()); var postsQuery = this.FilterPosts(postFilter, this.salePostService.GetAll().ProjectTo <PostVM>(configuration)); var paginatedPosts = this.PaginateList <PostVM>(pagination, postsQuery).ToList(); foreach (var post in paginatedPosts) { post.Sneaker.PosterImageRelativeLink = FileManager.GetRelativeFilePath(post.Sneaker.PosterImageLink); post.Sneaker.OverallRating = post.Sneaker.Ratings.Any() ? post.Sneaker.Ratings.Average(s => s.Rating.Score) : 0; } int totalPages = this.GetTotalPages(pagination.PageSize, postsQuery.Count()); PostListVM postListViewModel = new PostListVM { Posts = paginatedPosts, NextPage = pagination.Page < totalPages ? pagination.Page + 1 : pagination.Page, PreviousPage = pagination.Page > 1 ? pagination.Page - 1 : pagination.Page, CurrentPage = pagination.Page, TotalPages = totalPages, ShowPagination = totalPages > 1, }; this.LoadListSneakersDropdowns(postFilter); return(this.View(postListViewModel)); }