From 58b77f1d09b010cf1d7ff4494bd259010eb9649a Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Thu, 13 Sep 2018 13:41:35 +0300 Subject: [PATCH] Bloggin module improvements --- .../Volo/Blogging/Posts/BlogUserDto.cs | 24 ++++++++++++ .../Volo/Blogging/Posts/PostWithDetailsDto.cs | 2 + .../Volo/Blogging/Posts/PostAppService.cs | 37 +++++++++++++++---- .../Volo/Blogging/Tagging/ITagRepository.cs | 2 + .../Blogging/Tagging/EfCoreTagRepository.cs | 5 +++ .../AbpBloggingWebAutoMapperProfile.cs | 1 - 6 files changed, 62 insertions(+), 9 deletions(-) create mode 100644 modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/BlogUserDto.cs diff --git a/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/BlogUserDto.cs b/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/BlogUserDto.cs new file mode 100644 index 0000000000..b150c82953 --- /dev/null +++ b/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/BlogUserDto.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Volo.Abp.Application.Dtos; + +namespace Volo.Blogging.Posts +{ + public class BlogUserDto : EntityDto + { + public Guid? TenantId { get; set; } + + public string UserName { get; set; } + + public string Email { get; set; } + + public bool EmailConfirmed { get; set; } + + public string PhoneNumber { get; set; } + + public bool PhoneNumberConfirmed { get; set; } + + public Dictionary ExtraProperties { get; set; } + } +} diff --git a/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/PostWithDetailsDto.cs b/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/PostWithDetailsDto.cs index 456c197643..6ed5d57c2a 100644 --- a/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/PostWithDetailsDto.cs +++ b/modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/PostWithDetailsDto.cs @@ -21,6 +21,8 @@ namespace Volo.Blogging.Posts public int CommentCount { get; set; } + public BlogUserDto Writer { get; set; } + public List Tags { get; set; } } } diff --git a/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Posts/PostAppService.cs b/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Posts/PostAppService.cs index a7d6014a49..5a6e68d542 100644 --- a/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Posts/PostAppService.cs +++ b/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Posts/PostAppService.cs @@ -41,30 +41,51 @@ namespace Volo.Blogging.Posts { var posts = _postRepository.GetPostsByBlogId(id); - var postDtos = new List( + var tag = tagName.IsNullOrWhiteSpace()? null: await _tagRepository.GetByNameAsync(tagName); + + var allPostDtos = new List( ObjectMapper.Map, List>(posts)); - foreach (var postDto in postDtos) + var filteredPostDtos = new List(); + + var userDictionary = new Dictionary(); + + foreach (var postDto in allPostDtos) { postDto.Tags = await GetTagsOfPost(postDto.Id); + if (tag != null && postDto.Tags.All(t => t.Id != tag.Id)) + { + continue; + } + + filteredPostDtos.Add(postDto); + } + + foreach (var postDto in filteredPostDtos) + { postDto.CommentCount = await _commentRepository.GetCommentCountOfPostAsync(postDto.Id); if (postDto.CreatorId.HasValue) { var creatorUser = await UserLookupService.FindByIdAsync(postDto.CreatorId.Value); - //TODO: Check if creatorUser is null! - Logger.LogWarning($"Creator of post {postDto.Id} is {creatorUser.UserName}"); + + if (creatorUser != null && !userDictionary.ContainsKey(creatorUser.Id)) + { + userDictionary.Add(creatorUser.Id, ObjectMapper.Map(creatorUser)); + } } } - if (!tagName.IsNullOrWhiteSpace()) + foreach (var postDto in filteredPostDtos) { - var tag = await _tagRepository.GetByNameAsync(tagName); - postDtos = postDtos.Where(p => p.Tags.Any(t => t.Id == tag.Id)).ToList(); + if (postDto.CreatorId.HasValue && userDictionary.ContainsKey((Guid) postDto.CreatorId)) + { + postDto.Writer = userDictionary[(Guid)postDto.CreatorId]; + } } - return new ListResultDto(postDtos); + return new ListResultDto(allPostDtos); } public async Task GetForReadingAsync(GetPostInput input) diff --git a/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Tagging/ITagRepository.cs b/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Tagging/ITagRepository.cs index 538212b37d..19332c49f9 100644 --- a/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Tagging/ITagRepository.cs +++ b/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Tagging/ITagRepository.cs @@ -11,6 +11,8 @@ namespace Volo.Blogging.Tagging Task GetByNameAsync(string name); + Task FindByNameAsync(string name); + Task> GetListAsync(IEnumerable ids); void DecreaseUsageCountOfTags(List id); diff --git a/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Tagging/EfCoreTagRepository.cs b/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Tagging/EfCoreTagRepository.cs index 5ec949dc51..db58846444 100644 --- a/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Tagging/EfCoreTagRepository.cs +++ b/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Tagging/EfCoreTagRepository.cs @@ -22,6 +22,11 @@ namespace Volo.Blogging.Tagging } public async Task GetByNameAsync(string name) + { + return await DbSet.FirstAsync(t=>t.Name == name); + } + + public async Task FindByNameAsync(string name) { return await DbSet.FirstOrDefaultAsync(t=>t.Name == name); } diff --git a/modules/blogging/src/Volo.Blogging.Web/AbpBloggingWebAutoMapperProfile.cs b/modules/blogging/src/Volo.Blogging.Web/AbpBloggingWebAutoMapperProfile.cs index 3a95f05aab..ccd4a02953 100644 --- a/modules/blogging/src/Volo.Blogging.Web/AbpBloggingWebAutoMapperProfile.cs +++ b/modules/blogging/src/Volo.Blogging.Web/AbpBloggingWebAutoMapperProfile.cs @@ -16,7 +16,6 @@ namespace Volo.Blogging { CreateMap().Ignore(x=>x.Tags); CreateMap(); - CreateMap(); CreateMap(); CreateMap(); }