public IActionResult Index(int id) { var post = _post.GetPost(id); _post.UpdatePostViews(post); var model = _mapper.Map <Post, PostListViewModel>(post); model.PostTagList = _mapper.Map <List <PostTag>, List <PostTagViewModel> >(_postTag.GetPostTagList(o => o.PostId == id)); model.CategoryName = _category.GetCategory(o => o.CategoryId == model.CategoryId).CategoryName; model.UserName = _userManager.FindByIdAsync(model.UserId).Result.UserName; model.CountUserPosts = _post.GetCountUserPosts(model.UserId); model.CountUserViews = _post.GetCountUserViews(model.UserId).Value; foreach (var postTag in model.PostTagList) { postTag.TagName = _tag.GetTag(o => o.TagId == postTag.TagId).TagName; } return(View(model)); }
public IActionResult Index(string id, PostSearchViewModel model) { if (model.PageIndex == 0) { model.PageIndex = 1; } if (model.PageSize == 0) { model.PageSize = 10; } IPagedList <Post> posts = _post.GetPostList(o => o.UserId == id).OrderByDescending(o => o.DateCreated).ToList().ToPagedList(model.PageIndex, model.PageSize);; IEnumerable <PostListViewModel> postList = _mapper.Map <IEnumerable <PostListViewModel> >(posts); // create an instance of StaticPagedList with the mapped IEnumerable and original IPagedList metadata IPagedList <PostListViewModel> postListViewModel = new StaticPagedList <PostListViewModel>(postList, posts.GetMetaData()); foreach (var item in postListViewModel) { item.PostTagList = _mapper.Map <List <PostTag>, List <PostTagViewModel> >(_postTag.GetPostTagList(o => o.PostId == item.PostId)); foreach (var postTag in item.PostTagList) { postTag.TagName = _tag.GetTag(o => o.TagId == postTag.TagId).TagName; } item.CategoryName = _category.GetCategory(o => o.CategoryId == item.CategoryId).CategoryName; } var authorModel = new PostAuthorViewModel { PostList = postListViewModel }; authorModel.UserName = _userManager.FindByIdAsync(id).Result.UserName; authorModel.CountUserPosts = _post.GetCountUserPosts(id); authorModel.CountUserViews = _post.GetCountUserViews(id).Value; return(View(authorModel)); }
public IActionResult Index(PostSearchViewModel model) { if (model.PageIndex == 0) { model.PageIndex = 1; } if (model.PageSize == 0) { model.PageSize = 10; } ViewBag.SearchKey = model.SearchKey; var data = _post.GetPostList(); if (!string.IsNullOrEmpty(model.SearchKey)) { data = data.FindAll(o => o.Content.Contains(model.SearchKey) || o.Title.Contains(model.SearchKey)); } IPagedList <Post> posts = data.OrderByDescending(o => o.DateCreated).ToList().ToPagedList(model.PageIndex, model.PageSize); // map to IEnumerable IEnumerable <PostListViewModel> postList = _mapper.Map <IEnumerable <PostListViewModel> >(posts); // create an instance of StaticPagedList with the mapped IEnumerable and original IPagedList metadata IPagedList <PostListViewModel> postListViewModel = new StaticPagedList <PostListViewModel>(postList, posts.GetMetaData()); foreach (var item in postListViewModel) { item.UserName = _userManager.FindByIdAsync(item.UserId).Result.UserName; item.CategoryName = _category.GetCategory(o => o.CategoryId == item.CategoryId).CategoryName; item.PostTagList = _mapper.Map <List <PostTag>, List <PostTagViewModel> >(_postTag.GetPostTagList(o => o.PostId == item.PostId)); foreach (var postTag in item.PostTagList) { postTag.TagName = _tag.GetTag(o => o.TagId == postTag.TagId).TagName; } } var postInfoModel = new PostInfoViewModel { PostList = postListViewModel, TagList = _mapper.Map <List <Tag>, List <TagViewModel> >(_tag.GetTagList()), CategoryList = _mapper.Map <List <Category>, List <CategoryViewModel> >(_category.GetCategoryList()) }; return(View(postInfoModel)); }