//[ChildActionOnly] public ActionResult Posts() { PostWrapperVm vm = this.forumManager.GetPosts(this.HttpContext.Session, this.CurrentLanguageCode); vm.LanguageCode = this.CurrentLanguageCode; return this.PartialView(vm); }
public PostWrapperVm GetPosts(HttpSessionStateBase session, string languageCode) { IQueryable <Post> entityPosts = null; var vm = new PostWrapperVm(); var filter = session[CRRConfig.ForumFilter] as ForumFilterBm; var currentPage = 0; var sessionPage = session[CRRConfig.CurrentForumPage] as int?; if (int.TryParse(sessionPage.ToString(), out currentPage)) { } var resultLength = 0; var maxPage = 0; if (filter == null || (string.IsNullOrWhiteSpace(filter.Title) && string.IsNullOrWhiteSpace(filter.Content) && string.IsNullOrWhiteSpace(filter.Tags) && string.IsNullOrWhiteSpace(filter.Category))) { resultLength = this.forumService.GetPosts() .Count(x => x.IsQuestion && !x.IsDeleted); if (resultLength < 1) { vm.Posts = new List <PostVm>(); vm.Page = 0; vm.Pages = this.FillPages(0); session[CRRConfig.CurrentForumPage] = 0; return(vm); } maxPage = this.CalculateMaxPage(resultLength); if (currentPage >= maxPage) { --currentPage; session[CRRConfig.CurrentForumPage] = currentPage; } var entityPostsConnectionIsClose = this.forumService.GetPosts() .Where(x => x.IsQuestion && !x.IsDeleted) .OrderByDescending(x => x.CreatedOn) .Skip(currentPage * CRRConfig.NumberOfForumPostsPerPage) .Take(CRRConfig.NumberOfForumPostsPerPage).ToArray(); var mvss = Mapper.Map <IEnumerable <Post>, IEnumerable <PostVm> >(entityPostsConnectionIsClose).ToList(); for (int i = 0; i < mvss.Count; i++) { mvss[i].Categories = this.SystemNameToCurrentLanguage(mvss[i].Categories, languageCode); } vm.Posts = mvss; vm.Page = currentPage + 1; vm.Pages = this.FillPages(maxPage); return(vm); } //entityPosts = this.forumService.GetPosts(); //var vms = Mapper.Map<IEnumerable<Post>, IEnumerable<PostVm>>(entityPosts); //var tempCollection = new HashSet<PostVm>(); var tempCollection = new HashSet <Post>(); var vms = this.forumService.GetPosts().Where(x => !x.IsDeleted).ToArray(); if (vms.Length < 1) { vm.Posts = new List <PostVm>(); vm.Page = 0; vm.Pages = this.FillPages(0); session[CRRConfig.CurrentForumPage] = 0; return(vm); } if (!string.IsNullOrWhiteSpace(filter.Title)) { foreach (var postVm in vms) { if (postVm.Title.Contains(filter.Title)) { tempCollection.Add(postVm); } } } if (!string.IsNullOrWhiteSpace(filter.Content)) { foreach (var postVm in vms) { if (postVm.Content.Contains(filter.Content)) { tempCollection.Add(postVm); } } } if (!string.IsNullOrWhiteSpace(filter.Category)) { var categories = filter.Category.Split(' '); categories = this.GetCategorySystemNameByString(categories); foreach (var c in categories) { foreach (var postVm in vms) { if (postVm.Categories.Any(x => x.Name == c)) { tempCollection.Add(postVm); } } } } if (!string.IsNullOrWhiteSpace(filter.Tags)) { var tags = filter.Tags.Split(' ').Select(x => x.ToLower()); foreach (var t in tags) { foreach (var postVm in vms) { if (postVm.Tags.Any(x => x.Name == t)) { tempCollection.Add(postVm); } } } } var takeOnlyOriginals = new HashSet <Post>(); foreach (var tempPost in tempCollection) { if (tempPost.IsQuestion) { takeOnlyOriginals.Add(tempPost); } else { takeOnlyOriginals.Add(tempPost.Original); } } if (takeOnlyOriginals.Count == 0) { vm.Posts = new List <PostVm>(); vm.Page = 1; vm.Pages = new[] { 1 }; session[CRRConfig.CurrentForumPage] = 0; return(vm); } resultLength = takeOnlyOriginals.Count; maxPage = this.CalculateMaxPage(resultLength); //session[CRRConfig.MaxForumPage] = maxPage; if (currentPage >= maxPage) { --currentPage; session[CRRConfig.CurrentForumPage] = currentPage; } var filtered = Mapper.Map <IEnumerable <Post>, IEnumerable <PostVm> >(takeOnlyOriginals); var result = filtered.OrderByDescending(x => x.CreatedOn).Skip(currentPage * CRRConfig.NumberOfForumPostsPerPage).Take(CRRConfig.NumberOfForumPostsPerPage).ToList(); if (!result.Any()) { vm.Page = 1; session[CRRConfig.CurrentForumPage] = 0; } else { vm.Page = currentPage + 1; } for (int i = 0; i < result.Count; i++) { result[i].Categories = this.SystemNameToCurrentLanguage(result[i].Categories, languageCode); } vm.Posts = result; vm.Pages = this.FillPages(maxPage); return(vm); }