//gets selected post with pagination except for banned user's post(used in the index) public PostWithEntriesWithPaginationViewModel GetSelectedPostWithEntries(int id, int currentPage = 1) { var user = _httpContext.HttpContext.User; int? UserId = null; List <int> blockedUserIdList = new List <int>(); //gets blocked user list if (user.Claims.Count() > 0) { UserId = int.Parse(user.Claims.ToList().First(x => x.Type == ClaimTypes.NameIdentifier).Value); foreach (var item in _blockedUserService.GetAll(UserId.Value)) { blockedUserIdList.Add(item.BlockedUserId); } } var itemSize = 5; var postListVM = new PostWithEntriesWithPaginationViewModel(); var getPost = _postRepository.Get(x => x.Id == id); if (getPost != null) { postListVM.Post.Title = getPost.Title; getPost.ClickCount++; postListVM.Post.ClickCount = getPost.ClickCount; postListVM.Post.Id = id; postListVM.CurrentPage = currentPage; var itemCount = _entryRepository.GetAll(x => x.PostId == getPost.Id).Where(x => !blockedUserIdList.Contains(x.UserId)).Count(); var pageCount = itemCount / itemSize + (itemCount % itemSize > 0 ? 1 : 0); postListVM.PreviousPage = currentPage - 1 > 0 ? currentPage - 1 : pageCount; postListVM.NextPage = currentPage + 1 <= pageCount ? currentPage + 1 : 1; postListVM.PageCount = pageCount; _postRepository.SaveChanges(); var postEntries = _entryRepository.GetAll(x => x.PostId == getPost.Id).Include("User") .Where(x => !blockedUserIdList.Contains(x.UserId)) .Skip((currentPage - 1) * itemSize) .Take(itemSize) .ToList(); if (postEntries != null) { foreach (var item in postEntries) { var entryModel = new EntryWithRatingViewModel(); entryModel.Content = item.Content; entryModel.UserId = item.UserId; entryModel.CreateDate = item.CreateDate; entryModel.UpdateDate = item.UpdateDate; entryModel.Id = item.Id; entryModel.UserName = item.User.UserName; entryModel.LikeCount = _entryRatingService.GetLikeCount(item.Id); entryModel.DislikeCount = _entryRatingService.GetDislikeCount(item.Id); postListVM.EntryList.Add(entryModel); _entryRepository.SaveChanges(); } } } return(postListVM); }
//main screen-returns the selected post in the selected category with entries to the index page, and also get data to viewbags to pass layout public IActionResult Index(int postCategoryId = 1, int postId = 1, int selectPageNumber = 1) { ViewBag.UserInfo = _accountService.GetUserInfos(); ViewBag.SelectedCategory = _postCategoryService.GetSelectedCategory(x => x.Id == postCategoryId); ViewBag.CategoryList = _postCategoryService.GetAllCategories(); ViewBag.PostList = _postService.GetPostListByCategoryWithEntries(postCategoryId); ViewBag.postCategoryId = postCategoryId; var postWithEntriesViewModel = new PostWithEntriesWithPaginationViewModel(); postWithEntriesViewModel = _postService.GetSelectedPostWithEntries(postId, selectPageNumber); return(View(postWithEntriesViewModel)); }