public async Task <ListResultDto <PostWithDetailsDto> > GetTimeOrderedListAsync(Guid blogId) { var postCacheItems = await PostsCache.GetOrAddAsync( blogId.ToString(), async() => await GetTimeOrderedPostsAsync(blogId), () => new DistributedCacheEntryOptions { AbsoluteExpiration = DateTimeOffset.Now.AddHours(1) } ); var postsWithDetails = ObjectMapper.Map <List <PostCacheItem>, List <PostWithDetailsDto> >(postCacheItems); foreach (var post in postsWithDetails) { if (post.CreatorId.HasValue) { var creatorUser = await UserLookupService.FindByIdAsync(post.CreatorId.Value); if (creatorUser != null) { post.Writer = ObjectMapper.Map <BlogUser, BlogUserDto>(creatorUser); } } } return(new ListResultDto <PostWithDetailsDto>(postsWithDetails)); }
public async Task <List <CommentWithRepliesDto> > GetHierarchicalListOfPostAsync(Guid postId) { var comments = await GetListOfPostAsync(postId); var userDictionary = new Dictionary <Guid, BlogUserDto>(); foreach (var commentDto in comments) { if (commentDto.CreatorId.HasValue) { var creatorUser = await UserLookupService.FindByIdAsync(commentDto.CreatorId.Value); if (creatorUser != null && !userDictionary.ContainsKey(creatorUser.Id)) { userDictionary.Add(creatorUser.Id, ObjectMapper.Map <BlogUser, BlogUserDto>(creatorUser)); } } } foreach (var commentDto in comments) { if (commentDto.CreatorId.HasValue && userDictionary.ContainsKey((Guid)commentDto.CreatorId)) { commentDto.Writer = userDictionary[(Guid)commentDto.CreatorId]; } } var hierarchicalComments = new List <CommentWithRepliesDto>(); foreach (var commentDto in comments) { var parent = hierarchicalComments.Find(c => c.Comment.Id == commentDto.RepliedCommentId); if (parent != null) { parent.Replies.Add(commentDto); } else { hierarchicalComments.Add(new CommentWithRepliesDto() { Comment = commentDto }); } } hierarchicalComments = hierarchicalComments.OrderByDescending(c => c.Comment.CreationTime).ToList(); return(hierarchicalComments); }
public async Task <PostWithDetailsDto> GetAsync(Guid id) { var post = await PostRepository.GetAsync(id); var postDto = ObjectMapper.Map <Post, PostWithDetailsDto>(post); postDto.Tags = await GetTagsOfPostAsync(postDto.Id); if (postDto.CreatorId.HasValue) { var creatorUser = await UserLookupService.FindByIdAsync(postDto.CreatorId.Value); postDto.Writer = ObjectMapper.Map <BlogUser, BlogUserDto>(creatorUser); } return(postDto); }
public async Task <PostWithDetailsDto> GetForReadingAsync(GetPostInput input) { var post = await PostRepository.GetPostByUrl(input.BlogId, input.Url); post.IncreaseReadCount(); var postDto = ObjectMapper.Map <Post, PostWithDetailsDto>(post); postDto.Tags = await GetTagsOfPostAsync(postDto.Id); if (postDto.CreatorId.HasValue) { var creatorUser = await UserLookupService.FindByIdAsync(postDto.CreatorId.Value); postDto.Writer = ObjectMapper.Map <BlogUser, BlogUserDto>(creatorUser); } return(postDto); }
public async Task <ListResultDto <PostWithDetailsDto> > GetListByBlogIdAndTagNameAsync(Guid id, string tagName) { var posts = await PostRepository.GetPostsByBlogId(id); var tag = tagName.IsNullOrWhiteSpace() ? null : await TagRepository.FindByNameAsync(id, tagName); var userDictionary = new Dictionary <Guid, BlogUserDto>(); var postDtos = new List <PostWithDetailsDto>(ObjectMapper.Map <List <Post>, List <PostWithDetailsDto> >(posts)); foreach (var postDto in postDtos) { postDto.Tags = await GetTagsOfPostAsync(postDto.Id); } if (tag != null) { postDtos = await FilterPostsByTagAsync(postDtos, tag); } foreach (var postDto in postDtos) { if (postDto.CreatorId.HasValue) { if (!userDictionary.ContainsKey(postDto.CreatorId.Value)) { var creatorUser = await UserLookupService.FindByIdAsync(postDto.CreatorId.Value); if (creatorUser != null) { userDictionary[creatorUser.Id] = ObjectMapper.Map <BlogUser, BlogUserDto>(creatorUser); } } if (userDictionary.ContainsKey(postDto.CreatorId.Value)) { postDto.Writer = userDictionary[(Guid)postDto.CreatorId]; } } } return(new ListResultDto <PostWithDetailsDto>(postDtos)); }
public virtual async Task <BlogPostDto> CreateAsync(CreateBlogPostDto input) { var author = await UserLookupService.GetByIdAsync(CurrentUser.GetId()); var blog = await BlogRepository.GetAsync(input.BlogId); var blogPost = await BlogPostManager.CreateAsync( author, blog, input.Title, input.Slug, BlogPostStatus.Draft, input.ShortDescription, input.Content, input.CoverImageMediaId ); await BlogPostRepository.InsertAsync(blogPost); return(ObjectMapper.Map <BlogPost, BlogPostDto>(blogPost)); }