public ActionResult AllByUser(PostType?postType = null, PetType?petType = null, City?city = null, int id = 1) { PageableListPostViewModel viewModel; var page = id; var allItemsCount = this.posts.GetByUserId(this.CurrentUser.Id).Count(); var totalPages = (int)Math.Ceiling(allItemsCount / (decimal)ItemsPerPage); var itemsToSkip = (page - 1) * ItemsPerPage; var queryPosts = this.posts.GetByUserId(this.CurrentUser.Id); if (postType != null) { queryPosts = queryPosts.Where(p => p.PostType == postType); } if (petType != null) { queryPosts = queryPosts.Where(p => p.Pet.PetType == petType); } if (city != null) { queryPosts = queryPosts.Where(p => p.Location.City == city); } queryPosts = queryPosts.OrderBy(x => x.CreatedOn) .ThenBy(x => x.Id) .Skip(itemsToSkip) .Take(ItemsPerPage); var posts = queryPosts.To <PostViewModel>().ToList(); viewModel = new PageableListPostViewModel() { CurrentPage = page, TotalPages = totalPages, PostType = postType, PetType = petType, City = city, Posts = posts }; return(this.View(viewModel)); }
public ActionResult All( PostType?postType = null, PetType?petType = null, City?city = null, string orderBy = null, int id = 1) { PageableListPostViewModel viewModel; var page = id; var allItemsCount = this.posts.GetAll().Count(); var totalPages = (int)Math.Ceiling(allItemsCount / (decimal)ItemsPerPage); var itemsToSkip = (page - 1) * ItemsPerPage; var queryPosts = this.posts.GetAll(); if (postType != null) { queryPosts = queryPosts.Where(p => p.PostType == postType); } if (petType != null) { queryPosts = queryPosts.Where(p => p.Pet.PetType == petType); } if (city != null) { queryPosts = queryPosts.Where(p => p.Location.City == city); } if (orderBy != null) { switch (orderBy.ToString()) { case "1": queryPosts = queryPosts.OrderBy(p => p.Title); break; case "2": queryPosts = queryPosts.OrderBy(p => p.Author.UserName); break; default: queryPosts = queryPosts.OrderBy(x => x.CreatedOn).ThenBy(x => x.Id); break; } } queryPosts = queryPosts .Skip(itemsToSkip) .Take(ItemsPerPage); var posts = queryPosts.To <PostViewModel>().ToList(); viewModel = new PageableListPostViewModel() { CurrentPage = page, TotalPages = totalPages, PostType = postType, PetType = petType, City = city, Posts = posts, OrderBy = orderBy }; return(this.View(viewModel)); }