Browse Source

Merge pull request #2920 from abpframework/akin/Added-orderedPostList-into-PostAppService

Akin/added ordered post list into post app service
pull/2953/head
Yunus Emre Kalkan 6 years ago
committed by GitHub
parent
commit
5ca6d003df
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/IPostAppService.cs
  2. 27
      modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Posts/PostAppService.cs
  3. 2
      modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Posts/IPostRepository.cs
  4. 13
      modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Posts/EfCorePostRepository.cs
  5. 8
      modules/blogging/src/Volo.Blogging.HttpApi/Volo/Blogging/PostsController.cs
  6. 10
      modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Posts/MongoPostRepository.cs

2
modules/blogging/src/Volo.Blogging.Application.Contracts/Volo/Blogging/Posts/IPostAppService.cs

@ -9,6 +9,8 @@ namespace Volo.Blogging.Posts
{
Task<ListResultDto<PostWithDetailsDto>> GetListByBlogIdAndTagName(Guid blogId, string tagName);
Task<ListResultDto<PostWithDetailsDto>> GetTimeOrderedListAsync(Guid blogId);
Task<PostWithDetailsDto> GetForReadingAsync(GetPostInput input);
Task<PostWithDetailsDto> GetAsync(Guid id);

27
modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Posts/PostAppService.cs

@ -44,11 +44,6 @@ namespace Volo.Blogging.Posts
postDtos = await FilterPostsByTag(postDtos, tag);
}
foreach (var postDto in postDtos)
{
postDto.CommentCount = await _commentRepository.GetCommentCountOfPostAsync(postDto.Id);
}
foreach (var postDto in postDtos)
{
if (postDto.CreatorId.HasValue)
@ -72,6 +67,24 @@ namespace Volo.Blogging.Posts
return new ListResultDto<PostWithDetailsDto>(postDtos);
}
public async Task<ListResultDto<PostWithDetailsDto>> GetTimeOrderedListAsync(Guid blogId)
{
var posts = await _postRepository.GetOrderedList(blogId);
var postDtos = new List<PostWithDetailsDto>(ObjectMapper.Map<List<Post>, List<PostWithDetailsDto>>(posts));
foreach (var postDto in postDtos)
{
var creatorUser = await UserLookupService.FindByIdAsync(postDto.CreatorId.Value);
if (creatorUser != null)
{
postDto.Writer = ObjectMapper.Map<BlogUser, BlogUserDto>(creatorUser);
}
}
return new ListResultDto<PostWithDetailsDto>(postDtos);
}
public async Task<PostWithDetailsDto> GetForReadingAsync(GetPostInput input)
{
var post = await _postRepository.GetPostByUrl(input.BlogId, input.Url);
@ -172,7 +185,7 @@ namespace Volo.Blogging.Posts
{
var postList = await _postRepository.GetListAsync();
if (postList.Where(p => p.Url == url).WhereIf(existingPost != null, p => existingPost.Id != p.Id).Any())
if (postList.Where(p => p.Url == url).WhereIf(existingPost != null, p => existingPost.Id != p.Id).Any())
{
return url + "-" + Guid.NewGuid().ToString().Substring(0, 5);
}
@ -252,7 +265,7 @@ namespace Volo.Blogging.Posts
private Task<List<PostWithDetailsDto>> FilterPostsByTag(IEnumerable<PostWithDetailsDto> allPostDtos, Tag tag)
{
var filteredPostDtos = allPostDtos.Where(p => p.Tags?.Any(t => t.Id == tag.Id) ?? false).ToList();
return Task.FromResult(filteredPostDtos);
}
}

2
modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Posts/IPostRepository.cs

@ -10,5 +10,7 @@ namespace Volo.Blogging.Posts
Task<List<Post>> GetPostsByBlogId(Guid id);
Task<Post> GetPostByUrl(Guid blogId, string url);
Task<List<Post>> GetOrderedList(Guid blogId,bool descending = false);
}
}

13
modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Posts/EfCorePostRepository.cs

@ -35,6 +35,19 @@ namespace Volo.Blogging.Posts
return post;
}
public async Task<List<Post>> GetOrderedList(Guid blogId,bool descending = false)
{
if (!descending)
{
return await DbSet.Where(x=>x.BlogId==blogId).OrderByDescending(x => x.CreationTime).ToListAsync();
}
else
{
return await DbSet.Where(x => x.BlogId == blogId).OrderBy(x => x.CreationTime).ToListAsync();
}
}
public override IQueryable<Post> WithDetails()
{
return GetQueryable().IncludeDetails();

8
modules/blogging/src/Volo.Blogging.HttpApi/Volo/Blogging/PostsController.cs

@ -27,6 +27,13 @@ namespace Volo.Blogging
return _postAppService.GetListByBlogIdAndTagName(blogId, tagName);
}
[HttpGet]
[Route("{blogId}/all/by-time")]
public Task<ListResultDto<PostWithDetailsDto>> GetTimeOrderedListAsync(Guid blogId)
{
return _postAppService.GetTimeOrderedListAsync(blogId);
}
[HttpGet]
[Route("read")]
public Task<PostWithDetailsDto> GetForReadingAsync(GetPostInput input)
@ -60,5 +67,6 @@ namespace Volo.Blogging
{
return _postAppService.DeleteAsync(id);
}
}
}

10
modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Posts/MongoPostRepository.cs

@ -32,5 +32,15 @@ namespace Volo.Blogging.Posts
return post;
}
public async Task<List<Post>> GetOrderedList(Guid blogId, bool @descending = false)
{
if (!descending)
{
return await GetMongoQueryable().Where(x => x.BlogId == blogId).OrderByDescending(x => x.CreationTime).ToListAsync();
}
return await GetMongoQueryable().Where(x => x.BlogId == blogId).OrderBy(x => x.CreationTime).ToListAsync();
}
}
}

Loading…
Cancel
Save