// GET: Post public ViewResult List(string tag, string wordsearch, string filter, int page = 1) { string name = null; if (filter == "по заголовкам постов") { name = wordsearch; } if (filter == "по тегам") { tag = wordsearch; } PostsListViewModel <Post> model = new PostsListViewModel <Post>() { Collection = postsRepository.Get.OrderBy(p => p.PostId) .Where(p => (tag == null || p.Tags.Where(l => l.Name == tag).Count() == 1) && (name == null || (p.Title.Contains(name) || p.Description.Contains(name)))) .Skip((page - 1) * PageSize) .Take(PageSize), PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = (tag == null && name == null) ? postsRepository.Get.Count() : postsRepository.Get .Where(p => (tag == null || p.Tags.Where(l => l.Name == tag).Count() == 1) && (name == null || (p.Title.Contains(name) || p.Description.Contains(name)))).Count() }, CurrentTag = tag }; return(View(model)); }
public IActionResult Index(int page = 1) { int pageSize = 5; List <PostViewModel> list = new List <PostViewModel>(); var posts = db.Posts; foreach (var post in posts) { list.Add(new PostViewModel { Id = post.postID, postName = post.postName, descriptionPost = post.descriptionPost }); } IQueryable <PostViewModel> filterList = list.AsQueryable(); var count = filterList.Count(); var items = filterList.Skip((page - 1) * pageSize). Take(pageSize).ToList(); PostsListViewModel model = new PostsListViewModel { PageViewModel = new PageViewModel(count, page, pageSize), Posts = items }; return(View(model)); }
public ViewResult List() { PostsListViewModel postslistviewmodel = new PostsListViewModel(); postslistviewmodel.posts = _postsRepository.GetAllPosts; return(View(postslistviewmodel)); }
public ActionResult AuthorList(string authorId, int page = 1) { var user = _usersService.GetUser(authorId); if (user == null) { Response.StatusCode = 404; Response.StatusDescription = "Author not found"; Response.End(); return(null); } var model = new PostsListViewModel { Page = page, PageSize = SettingsHelper.PageSize, PagesCount = (int)Math.Ceiling((double)user.Posts.Count / SettingsHelper.PageSize), Posts = user.Posts .Skip((page - 1) * SettingsHelper.PageSize) .Take(SettingsHelper.PageSize) .ToList() }; ViewData["userName"] = user.UserName; ViewData["userId"] = authorId; return(View(model)); }
// GET : Post public ViewResult Index(string id, int page = 1) { ViewBag.Title = id == "ua" ? "Об'яви" : "Объявления"; ViewBag.Lang = id; var langId = id == "ua" ? Consts.UaLang : Consts.RuLang; PostsListViewModel model = new PostsListViewModel() { Posts = _repository .PostCatalog .Select(langId) .OrderByDefault() .Skip((page - 1) * 3) .Take(3), PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = Classes.Consts.pageSize, TotalItems = _repository.PostCatalog.Select(langId).Count() } }; return(View(model)); }
public async Task <IActionResult> Index(string toggle) { var user = await _userManager.GetUserAsync(User); var posts = _context.Post.OrderByDescending(m => m.Id).Include("User").AsQueryable(); if (toggle == "following") { // get posts that from the users that are being followed var followingUserIds = _context .Followings .Where(m => m.UserId == user.Id) .Select(m => m.UserFollowingId) .ToList(); posts = _context.Post.Where(m => followingUserIds.Contains(m.UserId) || m.UserId == user.Id).Include("User").OrderByDescending(m => m.Id); } else if (toggle == "all") { posts = _context.Post.OrderByDescending(m => m.Id).Include("User").AsQueryable(); } var viewModel = new PostsListViewModel { //Posts = await _context.Post.OrderByDescending(m => m.Id).Include("User").ToListAsync(), Posts = posts, NewPost = new Post() }; return(View(viewModel)); }
public ActionResult TagList(int id, int page = 1) { var tag = _tagsService.GetTag(id); if (tag == null) { Response.StatusCode = 404; Response.StatusDescription = "Tag not found"; Response.End(); return(null); } var model = new PostsListViewModel { Page = 1, PageSize = SettingsHelper.PageSize, PagesCount = (int)Math.Ceiling((double)tag.Posts.Count / SettingsHelper.PageSize), Posts = tag.Posts .Skip((page - 1) * SettingsHelper.PageSize) .Take(SettingsHelper.PageSize) .ToList() }; ViewData["Tag"] = tag.Label; ViewData["TagId"] = id; return(View(model)); }
/// <summary> /// Return collection of posts belongs to a particular tag. /// </summary> /// <param name="tagUrlSlug">tag's url slug</param> /// <param name="pageNo">Page index</param> /// <param name="pageSize">Page size</param> /// <returns></returns> public PostsListViewModel PostsByTag(string tagUrlSlug, int pageNo, int pageSize) { PostsListViewModel postsList = new PostsListViewModel(); postsList.Posts = _postRepository.PostsByTag(tagUrlSlug, pageNo, pageSize); postsList.TotalPosts = _postRepository.TotalPostsByTag(tagUrlSlug); return(postsList); }
/// <summary> /// Return the posts that matches the search text. /// </summary> /// <param name="term">search text</param> /// <param name="pageNo">Page index</param> /// <param name="pageSize">Page size</param> /// <returns></returns> public PostsListViewModel SearchPosts(string term, int pageNo, int pageSize) { PostsListViewModel postsList = new PostsListViewModel(); postsList.Posts = _postRepository.Query(x => x.Published == true && (x.ShortDesc.Contains(term) || x.Author.Contains(term) || x.Title.Contains(term) || x.Tags.Any(t => t.Name.Contains(term))), pageNo, pageSize); postsList.TotalPosts = _postRepository.TotalPostsBySearch(term); return(postsList); }
/// <summary> /// Return all of posts based on pagination parameters. /// </summary> /// <param name="pageNo">Page index</param> /// <param name="pageSize">Page size</param> /// <returns></returns> public PostsListViewModel Posts(int pageNo, int pageSize) { PostsListViewModel postsList = new PostsListViewModel(); postsList.Posts = _postRepository.Posts(pageNo, pageSize); postsList.TotalPosts = _postRepository.TotalPosts(true); return(postsList); }
public ActionResult Index() { var postsList = new PostsListViewModel { PostsList = postsService.GetAll() }; return(View(postsList)); }
public ViewResult List(Posts pst) { pst.UserName = "******"; _auc.Add(pst); _auc.SaveChanges(); PostsListViewModel postslistviewmodel = new PostsListViewModel(); postslistviewmodel.posts = _postsRepository.GetAllPosts; return(View(postslistviewmodel)); }
public ViewResult ViewPostsByCategory(string id) { var _category = repository.Categories.Where(p => p.CategoryName == id).Select(p => p.CategoryId).FirstOrDefault(); PostsListViewModel Posts = new PostsListViewModel { Posts = repository.Posts.Where(p => p.CategoryId == _category) .Where(u => u.Published == true).OrderByDescending(p => p.PostId) }; return(View("List", Posts)); }
public ActionResult List(int page) { var model = new PostsListViewModel { Page = page, PageSize = SettingsHelper.PageSize, Posts = _postService.GetPostsForPage(page, SettingsHelper.PageSize), PagesCount = (int)Math.Ceiling((double)_postService.GetTotalPostsCount() / SettingsHelper.PageSize) }; return(View("Index", model)); }
public IActionResult All(int id = 1) { const int itemsPerPage = 12; var viewModel = new PostsListViewModel() { ItemsPerPage = itemsPerPage, PageNumber = id, Posts = this.postsService.GetAll <PostViewModel>(id, 12), ItemsCount = this.postsService.GetCount(), }; return(this.View(viewModel)); }
public async Task <IActionResult> Create([Bind(include: "NewPost")] PostsListViewModel viewModel) { var post = new Post(viewModel.NewPost.Content, viewModel.NewPost.User.Id); //var post = viewModel.NewPost; if (ModelState.IsValid) { _context.Add(post); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(post)); }
public IActionResult Category(int categoryId, string categoryName, int id = 1) { const int itemsPerPage = 12; var viewModel = new PostsListViewModel { ItemsPerPage = itemsPerPage, PageNumber = id, Posts = this.postsService.GetByCategory <PostViewModel>(id, 12, categoryId), ItemsCount = this.postsService.GetCount(), CategoryName = categoryName, }; return(this.View(viewModel)); }
//выводим список ответов на вопрос public ActionResult Discussion(int id, int page = 1) { PostsListViewModel posts = new PostsListViewModel { Talkings = db.Talkings.Where(x => x.AskQuestionId == id).ToList() .OrderByDescending(x => x.Id) .Skip((page - 1) * PageSize_discussion) .Take(PageSize_discussion), PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize_discussion, TotalItems = db.Talkings.Where(x => x.AskQuestionId == id).Count() } }; ViewBag.DescriptionQuestion = db.AskQuestions.Where(x => x.Id == id).FirstOrDefault(); ViewBag.IdQuestion = id; Session["page_discussion"] = page; return(View(posts)); }
/// <summary> /// HTTP GET method which returns view response containing posts list. /// </summary> /// <returns>View with posts list</returns> public ViewResult List(string category = null, int page = 1) { IEnumerable <Post> allPosts = _postRepository.GetAll().OrderByDescending(x => x.CreationDateTime); if (category != null) { allPosts = allPosts.Where(p => p.Category == category); } PostsListViewModel postsListViewModel = new PostsListViewModel() { Posts = allPosts.Skip((page - 1) * PostsPerPage).Take(PostsPerPage).ToList(), PagingInfo = new PagingInfo() { CurrentPage = page, PostsPerPage = PostsPerPage, TotalPosts = allPosts.Count() } }; return(View(postsListViewModel)); }//END of Index method
public void CanPaginate_And_Send_Pagination_View_Model() { using (var trans = teplosetUnit.Db.Database.BeginTransaction()) { PostController controller = new PostController(teplosetUnit); PostsListViewModel result = (PostsListViewModel)controller.Index("ua", 2).Model; PagingInfo pageInfo = result.PagingInfo; List <PostCatalog> posts = result.Posts.ToList(); Assert.AreEqual(pageInfo.CurrentPage, 2); Assert.AreEqual(pageInfo.ItemsPerPage, 3); Assert.AreEqual(pageInfo.TotalItems, 5); Assert.AreEqual(pageInfo.TotalPages, 2); Assert.IsTrue(posts.Count == 2); Assert.AreEqual(posts[0].LangTypeId, new Guid("e409af66-0169-4f26-af48-54af19bcaa53")); trans.Rollback(); } }
//выводим список ответов на вопрос public ActionResult UserDiscussion(int id, int page = 1) { try { var Role = Session["Role"].ToString(); if (Role == "user") { PostsListViewModel posts = new PostsListViewModel { Talkings = db.Talkings.Where(x => x.AskQuestionId == id).ToList() .OrderByDescending(x => x.Id) .Skip((page - 1) * UPageSize_discussion) .Take(UPageSize_discussion), PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = UPageSize_discussion, TotalItems = db.Talkings.Where(x => x.AskQuestionId == id).Count() } }; ViewBag.DescriptionQuestion = db.AskQuestions.Where(x => x.Id == id).FirstOrDefault(); ViewBag.IdQuestion = id; Session["user_page_discussion"] = page; return(View(posts)); } else { return(RedirectToAction("ErrorAccess", "Error")); } } catch (NullReferenceException) { return(RedirectToAction("ErrorAccess", "Error")); } }
public void Can_Paginate() { // Arrange Mock <IPostRepository> mock = new Mock <IPostRepository>(); mock.Setup(m => m.Posts).Returns((new Post[] { new Post { PostId = 1, Title = "My First Title", Content = "Lorem Ipsum", ShortContent = "Lorem", MetaDataDescription = "Lorem, Ipsum", UrlSlug = "/my_first_title", Published = true, PostCreatedAt = DateTime.Parse("2018-12-24 12:00"), User = new User { AuthorName = "Marcus Eklund" }, Blog = new Blog { BlogId = 1 }, Category = new Category { CategoryName = "Development " } }, new Post { PostId = 2, Title = "My Second Title", Content = "Ipsum Lorem", ShortContent = "Ipsum", MetaDataDescription = "Ipsum, Lorem", UrlSlug = "/my_second_title", Published = false, PostCreatedAt = DateTime.Now, User = new User { AuthorName = "Marcus Eklund" }, Blog = new Blog { BlogId = 1 }, Category = new Category { CategoryName = "Development " } }, new Post { PostId = 3, Title = "My Third Title", Content = "Icky Lorem", ShortContent = "Snicky", MetaDataDescription = "Scooby", UrlSlug = "/my_third_title", Published = true, PostCreatedAt = DateTime.Now, User = new User { AuthorName = "Marcus Eklund" }, Blog = new Blog { BlogId = 1 }, Category = new Category { CategoryName = "Development " } } }).AsQueryable <Post>); HomeController controller = new HomeController(mock.Object); controller.PageSize = 3; // Act PostsListViewModel result = controller.List(null, 1).ViewData.Model as PostsListViewModel; // Assert Post[] postArray = result.Posts.ToArray(); Assert.True(postArray.Length == 2); Assert.Equal("My Third Title", postArray[0].Title); Assert.Equal("My First Title", postArray[1].Title); }
public void Can_Send_Pagination_View_Model() { // Arrange Mock <IPostRepository> mock = new Mock <IPostRepository>(); mock.Setup(m => m.Posts).Returns((new Post[] { new Post { PostId = 1, Title = "My First Title", Content = "Lorem Ipsum", ShortContent = "Lorem", MetaDataDescription = "Lorem, Ipsum", UrlSlug = "/my_first_title", Published = true, PostCreatedAt = DateTime.Parse("2018-12-24 12:00"), User = new User { AuthorName = "Marcus Eklund" }, Blog = new Blog { BlogId = 1 }, Category = new Category { CategoryName = "Development " } }, new Post { PostId = 2, Title = "My Second Title", Content = "Ipsum Lorem", ShortContent = "Ipsum", MetaDataDescription = "Ipsum, Lorem", UrlSlug = "/my_second_title", Published = false, PostCreatedAt = DateTime.Now, User = new User { AuthorName = "Marcus Eklund" }, Blog = new Blog { BlogId = 1 }, Category = new Category { CategoryName = "Development " } }, new Post { PostId = 3, Title = "My Third Title", Content = "Icky Lorem", ShortContent = "Snicky", MetaDataDescription = "Scooby", UrlSlug = "/my_third_title", Published = true, PostCreatedAt = DateTime.Now, User = new User { AuthorName = "Marcus Eklund" }, Blog = new Blog { BlogId = 1 }, Category = new Category { CategoryName = "Development " } } }).AsQueryable <Post>); // Arrange HomeController controller = new HomeController(mock.Object) { PageSize = 2 }; // Act PostsListViewModel result = controller.List(null, 1).ViewData.Model as PostsListViewModel; // Assert PagingInfo pageInfo = result.PagingInfo; Assert.Equal(1, pageInfo.CurrentPage); Assert.Equal(2, pageInfo.ItemsPerPage); Assert.Equal(2, pageInfo.TotalItems); Assert.Equal(1, pageInfo.TotalPages); }
public async Task <IActionResult> Index(string searchTitle, string searchTag) { IQueryable <Post> posts = db.Posts .Include(s => s.Sender) .OrderByDescending(p => p.PublishDate); await posts.ForEachAsync(p => { if (p.Text != null) { if (p.Text.Length > 1400) { p.Text = p.Text.Substring(0, 1399); p.Text += "..."; } } }); await posts.ForEachAsync(p => { if (p.Text != null) { p.Text = p.Text .Replace("<b>", " ") .Replace("</b>", " ") .Replace("<h1>", " ") .Replace("</h1>", " ") .Replace("<h2>", " ") .Replace("</h2>", " ") .Replace("<h3>", " ") .Replace("</h3>", " ") .Replace("<h4>", " ") .Replace("</h4>", " "); } }); if (!String.IsNullOrEmpty(searchTitle)) { posts = posts.Where(p => p.Title.Contains(searchTitle)); } List <Post> findedPosts = null; if (!String.IsNullOrEmpty(searchTag)) { string[] tags = searchTag.ToLower().Split(", ", ','); findedPosts = await posts.ToListAsync(); int count = findedPosts.Count; foreach (var tag in tags) { findedPosts.AddRange(findedPosts.Where(p => p.Tags.Contains(tag)).ToList()); } findedPosts = findedPosts.Skip(count).Distinct().ToList(); //findedPosts = await posts.ToListAsync(); //findedPosts = findedPosts.Where(p => p.Tags.Contains(searchTag)); } PostsListViewModel viewModel = new PostsListViewModel { Posts = findedPosts ?? await posts.ToListAsync(), SearchTitle = searchTitle }; return(View(viewModel)); }