public PostShowViewModel GetPostWithComments(int postId, SortParametersDto sortParameters) { BlogContext db = new BlogContext(); PostShowViewModel postModel = new PostShowViewModel { Post = db.Posts.FirstOrDefault(post => post.Id.Equals(postId)) }; postModel.Profile = db.Profiles.FirstOrDefault(pr => pr.ApplicationUser.Equals(postModel.Post.Author)); if (postModel.Post == null) { return(null); } var store = new UserStore <ApplicationUser>(new ApplicationDbContext()); var userManager = new UserManager <ApplicationUser>(store); var author = postModel.Post.Author; ApplicationUser user = userManager.FindByIdAsync(author).Result; if (user != null) { postModel.Post.Author = user.UserName; } var tags = db.Tags.Where(tag => tag.PostId.Equals(postId)).ToList(); foreach (var tag in tags) { postModel.Post.PostTags.Add(tag); } postModel.Comments = _commentsService.GetPagedCommentsByPostId(postId, author, sortParameters); return(postModel); }
public async Task <ActionResult> Index(string search, string sortBy, string orderBy, int page = 1) { var sortParameters = new SortParametersDto() { OrderBy = orderBy ?? "asc", SortBy = sortBy ?? "Title", CurrentPage = page, PageSize = 10 }; var posts = await _postsService.GetPosts(sortParameters, search); var popularPostsSortParameters = new SortParametersDto() { OrderBy = "asc", SortBy = "Likes", DisplayType = "list", CurrentPage = 1, PageSize = 5 }; posts.PopularItems.PopularPosts = await _postsService.GetPopularPosts(popularPostsSortParameters); var popularTagsSortParameters = new SortParametersDto() { OrderBy = "asc", SortBy = "Title", CurrentPage = 1, PageSize = 5 }; posts.PopularItems.PopularTags = await _tagsService.GetPopularTags(popularTagsSortParameters); return(View(posts)); }
/// <inheritdoc cref="IPostsService"/> public async Task <List <Post> > GetPopularPosts(SortParametersDto sortParameters, bool onlyWithComments = false) { var postsQueryable = GetAll(); var postModel = await postsQueryable .Include(x => x.Author) .Include(x => x.Author.Profile) .Include(x => x.Comments) .Include(x => x.PostTags) .ToListAsync(); if (onlyWithComments) { postModel = postModel.Where(x => x.Comments.Count > 0).ToList(); } if (sortParameters == null) { return(postModel); } postModel = SortPosts(postModel.AsQueryable(), sortParameters).ToList(); if (sortParameters.DisplayType != null && !sortParameters.DisplayType.Equals("grid")) { postModel = postModel .Skip((sortParameters.CurrentPage - 1) * sortParameters.PageSize) .Take(sortParameters.PageSize).ToList(); } return(postModel); }
public ActionResult Show(int?id, string sorts, int page = 1) { if (id == null) { return(RedirectToAction("Index", "Posts")); } var sortParameters = new SortParametersDto() { CurrentPage = page, PageSize = 10 }; var postModelToUpdate = _db.Posts.FirstOrDefault(post => post.Id.Equals(id.Value)); if (postModelToUpdate != null && page == 1) { postModelToUpdate.Seen++; _db.Entry(postModelToUpdate).State = EntityState.Modified; _db.SaveChanges(); } var postModel = _postsService.GetPostWithComments(id.Value, sortParameters); if (postModel == null) { return(RedirectToAction("Index", "Posts")); } return(View(postModel)); }
/// <inheritdoc cref="IPostsService"/> public async Task <PostShowDto> GetPost(int postId, SortParametersDto sortParameters) { var postModel = new PostShowDto() { Post = await Where(post => post.Id == postId) .Include(x => x.Author) .Include(x => x.Author.Profile) .Include(x => x.PostTags) .FirstOrDefaultAsync() }; if (sortParameters == null || postModel.Post == null) { return(postModel); } var count = postModel.Post.Comments.Count; postModel.Comments.Comments = postModel.Post.Comments.Skip((sortParameters.CurrentPage - 1) * sortParameters.PageSize).Take(sortParameters.PageSize).ToList(); postModel.Comments.PageInfo = new PageInfo { PageNumber = sortParameters.CurrentPage, PageSize = sortParameters.PageSize, TotalItems = count }; postModel.Post.Comments = null; return(postModel); }
public async Task <ActionResult> Show(int?id, string sorts, int page = 1) { if (id == null) { return(RedirectToAction("Index", "Posts")); } var sortParameters = new SortParametersDto() { CurrentPage = page, PageSize = 10 }; var postModelToUpdate = await _postsService.FindAsync(id.Value); if (postModelToUpdate != null && page == 1) { postModelToUpdate.Seen++; await _postsService.UpdateAsync(postModelToUpdate); } var postModel = await _postsService.GetPost(id.Value, sortParameters); if (postModel == null) { return(RedirectToAction("Index", "Posts")); } return(View(postModel)); }
public CommentsViewModel GetPagedCommentsByPostId(int postId, string author, SortParametersDto sortParameters) { var store = new UserStore <ApplicationUser>(new ApplicationDbContext()); var userManager = new UserManager <ApplicationUser>(store); CommentsViewModel commentsViewModel = new CommentsViewModel { Comments = new List <CommentViewModel>() }; var comments = _db.Comments.Where(comment => comment.PostID.Equals(postId)).ToList(); comments.ForEach(comment => { ApplicationUser commentAuthor = userManager.FindByIdAsync(comment.Author).Result; CommentViewModel comm = new CommentViewModel { Profile = _db.Profiles.FirstOrDefault(pr => pr.ApplicationUser.Equals(comment.Author)) }; if (commentAuthor != null) { comment.Author = commentAuthor.UserName; } comm.Comment = comment; commentsViewModel.Comments.Add(comm); }); commentsViewModel.Comments = commentsViewModel.Comments.Skip((sortParameters.CurrentPage - 1) * sortParameters.PageSize).Take(sortParameters.PageSize).ToList(); commentsViewModel.PageInfo = new PageInfo { PageNumber = sortParameters.CurrentPage, PageSize = sortParameters.PageSize, TotalItems = comments.Count }; return(commentsViewModel); }
/// <inheritdoc cref="ICommentsService"/> public async Task <CommentsDto> GetCommentsForPost(int?postId, string authorId, SortParametersDto sortParameters) { var comments = Table; if (postId != null) { comments = comments.Where(comment => comment.PostId == postId); } if (!string.IsNullOrWhiteSpace(authorId)) { comments = comments.Where(comment => comment.AuthorId.Equals(authorId)); } var commentsDto = new CommentsDto() { Comments = await comments.Include(x => x.Author).ToListAsync(), }; var count = commentsDto.Comments.Count; if (sortParameters == null) { return(commentsDto); } commentsDto.Comments = commentsDto.Comments.Skip((sortParameters.CurrentPage - 1) * sortParameters.PageSize).Take(sortParameters.PageSize).ToList(); commentsDto.PageInfo = new PageInfo { PageNumber = sortParameters.CurrentPage, PageSize = sortParameters.PageSize, TotalItems = count }; return(commentsDto); }
/// <inheritdoc cref="IPostsService"/> public async Task <PostShowViewDto> GetPost(int postId, SortParametersDto sortParameters) { var post = await this.Repository.Table .Where(new PostSpecification(x => x.Id.Equals(postId)).Filter) .Include(x => x.Author) .ThenInclude(x => x.Profile) .Include(x => x.PostsTagsRelations) .ThenInclude(x => x.Tag) .FirstOrDefaultAsync(); var postModel = new PostShowViewDto { Tags = post.PostsTagsRelations.Select(x => new TagViewDto { Id = x.Tag.Id, Title = x.Tag.Title, }).ToList(), Comments = await this.commentsService.GetPagedCommentsByPostId(postId, sortParameters), }; post.PostsTagsRelations = null; postModel.Post = this.mapper.Map <Post, PostViewDto>(post); postModel.Post.Author = this.mapper.Map <ApplicationUser, ApplicationUserDto>(post.Author); postModel.Post.Author.Profile.User = null; return(postModel.Post == null ? null : postModel); }
public PostsViewModel GetCurrentUserPosts(string currentUserId, SortParametersDto sortParameters, string search) { BlogContext db = new BlogContext(); IList <PostViewModel> postModel = new List <PostViewModel>(); IEnumerable <Post> postsEnumerable = db.Posts.Where(post => post.Author.Equals(currentUserId)); if (!search.IsNullOrWhiteSpace()) { postsEnumerable = postsEnumerable.Where(post => post.Title.Equals(search)); } var posts = SortPosts(postsEnumerable, sortParameters).ToList(); var store = new UserStore <ApplicationUser>(new ApplicationDbContext()); var userManager = new UserManager <ApplicationUser>(store); posts.ForEach(item => { PostViewModel post = new PostViewModel { Profile = db.Profiles.FirstOrDefault(pr => pr.ApplicationUser.Equals(item.Author)) }; ApplicationUser user = userManager.FindByIdAsync(item.Author).Result; if (user != null) { item.Author = user.UserName; } post.Post = item; post.Post.PostTags = new List <Tag>(); var tags = db.Tags.Where(tag => tag.PostId.Equals(item.Id)).ToList(); foreach (var tag in tags) { post.Post.PostTags.Add(tag); } post.CommentsCount = db.Comments.Count(comment => comment.PostID.Equals(item.Id)); postModel.Add(post); }); PostsViewModel postsViewModel = new PostsViewModel(); if (!sortParameters.DisplayType.Equals("grid")) { postsViewModel.Posts = postModel.Skip((sortParameters.CurrentPage - 1) * sortParameters.PageSize) .Take(sortParameters.PageSize).ToList(); } else { postsViewModel.Posts = postModel; } postsViewModel.PageInfo = new PageInfo { PageNumber = sortParameters.CurrentPage, PageSize = sortParameters.PageSize, TotalItems = postModel.Count }; return(postsViewModel); }
public ActionResult Index(string search, string sortBy, string orderBy, int page = 1) { var sortParameters = new SortParametersDto() { OrderBy = orderBy ?? "asc", SortBy = sortBy ?? "Title", CurrentPage = page, PageSize = 10 }; var posts = _postsService.GetPosts(sortParameters, search); return(View(posts)); }
/// <inheritdoc cref="ICommentsService"/> public async Task <CommentsViewDto> GetPagedCommentsByPostId(int postId, SortParametersDto sortParameters) { var comments = await this.Repository.TableNoTracking .Where(new CommentSpecification(comment => comment.PostId.Equals(postId)).Filter) .Select(x => new Comment { Id = x.Id, CommentBody = x.CommentBody, CreatedAt = x.CreatedAt, Dislikes = x.Dislikes, Email = x.Email, Name = x.Name, Likes = x.Likes, PostId = x.PostId, UserId = x.UserId, User = x.User == null ? new ApplicationUser() : new ApplicationUser { Id = x.User.Id, Email = x.User.Email, FirstName = x.User.FirstName, LastName = x.User.LastName, PhoneNumber = x.User.PhoneNumber, }, }).ToListAsync(); var commentsViewModel = new CommentsViewDto { Comments = comments, }; if (sortParameters.CurrentPage == null || sortParameters.PageSize == null) { return(commentsViewModel); } commentsViewModel.Comments = commentsViewModel.Comments .Skip((sortParameters.CurrentPage.Value - 1) * sortParameters.PageSize.Value) .Take(sortParameters.PageSize.Value).ToList(); commentsViewModel.PageInfo = new PageInfo { PageNumber = sortParameters.CurrentPage.Value, PageSize = sortParameters.PageSize.Value, TotalItems = comments.Count, }; return(commentsViewModel); }
/// <inheritdoc cref="IPostsService"/> public async Task <PostsDto> GetUserPosts(string userId, SortParametersDto sortParameters, string search) { var postsEnumerable = Table; if (!string.IsNullOrWhiteSpace(userId)) { postsEnumerable = postsEnumerable.Where(post => post.AuthorId.Equals(userId)); } if (!string.IsNullOrWhiteSpace(search)) { postsEnumerable = postsEnumerable.Where(post => post.Title.Equals(search)); } postsEnumerable = postsEnumerable .Include(x => x.Author) .Include(x => x.Author.Profile) .Include(x => x.PostTags) .Include(x => x.Comments); IList <Post> postModel = sortParameters != null ? await SortPosts(postsEnumerable, sortParameters).ToListAsync() : await postsEnumerable.ToListAsync(); var postsViewModel = new PostsDto(); if (sortParameters == null) { return(postsViewModel); } if (!sortParameters.DisplayType.Equals("grid")) { postsViewModel.Posts = postModel .Skip((sortParameters.CurrentPage - 1) * sortParameters.PageSize) .Take(sortParameters.PageSize).ToList(); } else { postsViewModel.Posts = postModel; } postsViewModel.PageInfo = new PageInfo { PageNumber = sortParameters.CurrentPage, PageSize = sortParameters.PageSize, TotalItems = postModel.Count }; return(postsViewModel); }
public ActionResult MyPosts(string display, string sortBy, string orderBy, string search, int page = 1) { var sortParameters = new SortParametersDto() { OrderBy = orderBy ?? "asc", SortBy = sortBy ?? "Title", CurrentPage = page, PageSize = 10, DisplayType = display ?? "list" }; var posts = _postsService.GetCurrentUserPosts(User.Identity.GetUserId(), sortParameters, search); posts.DisplayType = display ?? "list"; return(View(posts)); }
/// <inheritdoc cref="IPostsService"/> public async Task <PostsDto> GetPosts(SortParametersDto sortParameters, string search, bool onlyWithComments = false) { var postsQueryable = GetAll(); if (!string.IsNullOrWhiteSpace(search)) { postsQueryable = postsQueryable.Where(post => post.Title.Equals(search)); } var postModel = await postsQueryable .Include(x => x.Author) .Include(x => x.Author.Profile) .Include(x => x.Comments) .Include(x => x.PostTags) .ToListAsync(); if (onlyWithComments) { postModel = postModel.Where(x => x.Comments.Count > 0).ToList(); } var postsDto = new PostsDto() { Posts = postModel }; if (sortParameters == null) { return(postsDto); } postsDto.Posts = SortPosts(postsDto.Posts.AsQueryable(), sortParameters).ToList(); if (sortParameters.DisplayType != null && !sortParameters.DisplayType.Equals("grid")) { postsDto.Posts = postModel .Skip((sortParameters.CurrentPage - 1) * sortParameters.PageSize) .Take(sortParameters.PageSize).ToList(); } postsDto.PageInfo = new PageInfo { PageNumber = sortParameters.CurrentPage, PageSize = sortParameters.PageSize, TotalItems = postModel.Count }; return(postsDto); }
public async Task <ActionResult> Show([FromRoute] int id) { var sortParameters = new SortParametersDto { CurrentPage = 1, PageSize = 10 }; var post = await _postsService.GetPost(id, sortParameters); if (post == null) { return(NotFound()); } return(Ok(_mapper.Map <PostWithPagedCommentsResponse>(post))); }
/// <summary> /// Order by query. /// </summary> /// <typeparam name="T">T.</typeparam> /// <param name="source">source.</param> /// <param name="sortParameters">sortParameters.</param> /// <returns>IQueryable.</returns> public static IQueryable <T> OrderBy <T>(this IQueryable <T> source, SortParametersDto sortParameters) { var expression = source.Expression; int count = 0; var parameter = Expression.Parameter(typeof(T), "x"); var selector = Expression.PropertyOrField(parameter, sortParameters.SortBy); var method = string.Equals(sortParameters.OrderBy, "desc", StringComparison.OrdinalIgnoreCase) ? (count == 0 ? "OrderByDescending" : "ThenByDescending") : (count == 0 ? "OrderBy" : "ThenBy"); expression = Expression.Call( typeof(Queryable), method, new[] { source.ElementType, selector.Type }, expression, Expression.Quote(Expression.Lambda(selector, parameter))); return(source.Provider.CreateQuery <T>(expression)); }
/// <inheritdoc cref="ITagsService"/> public async Task <List <Tag> > GetPopularTags(SortParametersDto sortParameters) { var tagsQueryable = GetAll(); var tagsModel = await tagsQueryable .ToListAsync(); if (sortParameters == null) { return(tagsModel); } tagsModel = SortTags(tagsModel.AsQueryable(), sortParameters).ToList(); tagsModel = tagsModel .Skip((sortParameters.CurrentPage - 1) * sortParameters.PageSize) .Take(sortParameters.PageSize).ToList(); return(tagsModel); }
/// <summary> /// Sorts the tags. /// </summary> /// <param name="tags">The tags.</param> /// <param name="sortParameters">The sort parameters.</param> /// <returns>IOrderedQueryable.</returns> private static IOrderedQueryable <Tag> SortTags(IQueryable <Tag> tags, SortParametersDto sortParameters) { if (sortParameters.SortBy.Equals("CreatedAt") && sortParameters.OrderBy.Equals("asc")) { return(tags.OrderBy(x => x.CreatedAt)); } if (sortParameters.SortBy.Equals("CreatedAt") && sortParameters.OrderBy.Equals("desc")) { return(tags.OrderByDescending(x => x.CreatedAt)); } if (sortParameters.SortBy.Equals("Title") && sortParameters.OrderBy.Equals("asc")) { return(tags.OrderBy(x => x.Title)); } if (sortParameters.SortBy.Equals("Title") && sortParameters.OrderBy.Equals("desc")) { return(tags.OrderByDescending(x => x.Title)); } else { return(tags.OrderBy(x => x.Id)); } }
public IOrderedEnumerable <Post> SortPosts(IEnumerable <Post> posts, SortParametersDto sortParameters) { if (sortParameters.SortBy.Equals("CreatedAt") && sortParameters.OrderBy.Equals("asc")) { return(posts.OrderBy(x => x.CreatedAt)); } if (sortParameters.SortBy.Equals("CreatedAt") && sortParameters.OrderBy.Equals("desc")) { return(posts.OrderByDescending(x => x.CreatedAt)); } if (sortParameters.SortBy.Equals("Title") && sortParameters.OrderBy.Equals("asc")) { return(posts.OrderBy(x => x.Title)); } if (sortParameters.SortBy.Equals("Title") && sortParameters.OrderBy.Equals("desc")) { return(posts.OrderByDescending(x => x.Title)); } if (sortParameters.SortBy.Equals("Author") && sortParameters.OrderBy.Equals("asc")) { return(posts.OrderBy(x => x.Author)); } if (sortParameters.SortBy.Equals("Author") && sortParameters.OrderBy.Equals("desc")) { return(posts.OrderByDescending(x => x.Author)); } if (sortParameters.SortBy.Equals("Likes") && sortParameters.OrderBy.Equals("asc")) { return(posts.OrderBy(x => x.Likes)); } if (sortParameters.SortBy.Equals("Likes") && sortParameters.OrderBy.Equals("desc")) { return(posts.OrderByDescending(x => x.Likes)); } if (sortParameters.SortBy.Equals("Dislikes") && sortParameters.OrderBy.Equals("asc")) { return(posts.OrderBy(x => x.Dislikes)); } if (sortParameters.SortBy.Equals("Dislikes") && sortParameters.OrderBy.Equals("desc")) { return(posts.OrderByDescending(x => x.Dislikes)); } else { return(posts.OrderBy(x => x.Id)); } /* * Expression<Func<Post, object>> sortExpression; * switch (sortParameters.SortBy) * { * case : * sortExpression = (x => x.CreatedAt); * break; * case "Title": * sortExpression = (x => x.Title); * break; * case "Author": * sortExpression = (x => x.Author); * break; * case "Likes": * sortExpression = (x => x.Likes); * break; * case "Dislikes": * sortExpression = (x => x.Dislikes); * break; * default: * sortExpression = (x => x.Id); * break; * } */ }