Bloggin module improvements

pull/441/head
Yunus Emre Kalkan 6 years ago
parent d6eb1d8d2e
commit 58b77f1d09

@ -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<Guid>
{
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<string, object> ExtraProperties { get; set; }
}
}

@ -21,6 +21,8 @@ namespace Volo.Blogging.Posts
public int CommentCount { get; set; }
public BlogUserDto Writer { get; set; }
public List<TagDto> Tags { get; set; }
}
}

@ -41,30 +41,51 @@ namespace Volo.Blogging.Posts
{
var posts = _postRepository.GetPostsByBlogId(id);
var postDtos = new List<PostWithDetailsDto>(
var tag = tagName.IsNullOrWhiteSpace()? null: await _tagRepository.GetByNameAsync(tagName);
var allPostDtos = new List<PostWithDetailsDto>(
ObjectMapper.Map<List<Post>, List<PostWithDetailsDto>>(posts));
foreach (var postDto in postDtos)
var filteredPostDtos = new List<PostWithDetailsDto>();
var userDictionary = new Dictionary<Guid, BlogUserDto>();
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<BlogUser, BlogUserDto>(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<PostWithDetailsDto>(postDtos);
return new ListResultDto<PostWithDetailsDto>(allPostDtos);
}
public async Task<PostWithDetailsDto> GetForReadingAsync(GetPostInput input)

@ -11,6 +11,8 @@ namespace Volo.Blogging.Tagging
Task<Tag> GetByNameAsync(string name);
Task<Tag> FindByNameAsync(string name);
Task<List<Tag>> GetListAsync(IEnumerable<Guid> ids);
void DecreaseUsageCountOfTags(List<Guid> id);

@ -22,6 +22,11 @@ namespace Volo.Blogging.Tagging
}
public async Task<Tag> GetByNameAsync(string name)
{
return await DbSet.FirstAsync(t=>t.Name == name);
}
public async Task<Tag> FindByNameAsync(string name)
{
return await DbSet.FirstOrDefaultAsync(t=>t.Name == name);
}

@ -16,7 +16,6 @@ namespace Volo.Blogging
{
CreateMap<PostWithDetailsDto, EditPostViewModel>().Ignore(x=>x.Tags);
CreateMap<NewModel.CreatePostViewModel, CreatePostDto>();
CreateMap<IndexModel.BlogIndexViewModel, CreateBlogDto>();
CreateMap<CreateModel.BlogCreateModalView, CreateBlogDto>();
CreateMap<BlogDto, EditModel.BlogEditViewModel>();
}

Loading…
Cancel
Save