Browse Source

Add CancellationToken optional parameter to the Blogging module repository methods

pull/7359/head
liangshiwei 6 years ago
parent
commit
3da17ba365
  1. 3
      modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Blogs/IBlogRepository.cs
  2. 11
      modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Comments/ICommentRepository.cs
  3. 9
      modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Posts/IPostRepository.cs
  4. 8
      modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Tagging/ITagRepository.cs
  5. 5
      modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Blogs/EfCoreBlogRepository.cs
  6. 17
      modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Comments/EfCoreCommentRepository.cs
  7. 19
      modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Posts/EfCorePostRepository.cs
  8. 16
      modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Tagging/EfCoreTagRepository.cs
  9. 5
      modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Blogs/MongoBlogRepository.cs
  10. 25
      modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Comments/MongoCommentRepository.cs
  11. 23
      modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Posts/MongoPostRepository.cs
  12. 18
      modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Tagging/MongoTagRepository.cs
  13. 6
      modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Users/MongoBlogUserRepository.cs

3
modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Blogs/IBlogRepository.cs

@ -1,4 +1,5 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
@ -6,6 +7,6 @@ namespace Volo.Blogging.Blogs
{
public interface IBlogRepository : IBasicRepository<Blog, Guid>
{
Task<Blog> FindByShortNameAsync(string shortName);
Task<Blog> FindByShortNameAsync(string shortName, CancellationToken cancellationToken = default);
}
}

11
modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Comments/ICommentRepository.cs

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
@ -7,14 +8,12 @@ namespace Volo.Blogging.Comments
{
public interface ICommentRepository : IBasicRepository<Comment, Guid>
{
Task<List<Comment>> GetListOfPostAsync(
Guid postId
);
Task<List<Comment>> GetListOfPostAsync(Guid postId, CancellationToken cancellationToken = default);
Task<int> GetCommentCountOfPostAsync(Guid postId);
Task<int> GetCommentCountOfPostAsync(Guid postId, CancellationToken cancellationToken = default);
Task<List<Comment>> GetRepliesOfComment(Guid id);
Task<List<Comment>> GetRepliesOfComment(Guid id, CancellationToken cancellationToken = default);
Task DeleteOfPost(Guid id);
Task DeleteOfPost(Guid id, CancellationToken cancellationToken = default);
}
}

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

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
@ -7,12 +8,12 @@ namespace Volo.Blogging.Posts
{
public interface IPostRepository : IBasicRepository<Post, Guid>
{
Task<List<Post>> GetPostsByBlogId(Guid id);
Task<List<Post>> GetPostsByBlogId(Guid id, CancellationToken cancellationToken = default);
Task<bool> IsPostUrlInUseAsync(Guid blogId, string url, Guid? excludingPostId = null);
Task<bool> IsPostUrlInUseAsync(Guid blogId, string url, Guid? excludingPostId = null, CancellationToken cancellationToken = default);
Task<Post> GetPostByUrl(Guid blogId, string url);
Task<Post> GetPostByUrl(Guid blogId, string url, CancellationToken cancellationToken = default);
Task<List<Post>> GetOrderedList(Guid blogId,bool descending = false);
Task<List<Post>> GetOrderedList(Guid blogId,bool descending = false, CancellationToken cancellationToken = default);
}
}

8
modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Tagging/ITagRepository.cs

@ -8,13 +8,13 @@ namespace Volo.Blogging.Tagging
{
public interface ITagRepository : IBasicRepository<Tag, Guid>
{
Task<List<Tag>> GetListAsync(Guid blogId);
Task<List<Tag>> GetListAsync(Guid blogId, CancellationToken cancellationToken = default);
Task<Tag> GetByNameAsync(Guid blogId, string name);
Task<Tag> GetByNameAsync(Guid blogId, string name, CancellationToken cancellationToken = default);
Task<Tag> FindByNameAsync(Guid blogId, string name);
Task<Tag> FindByNameAsync(Guid blogId, string name, CancellationToken cancellationToken = default);
Task<List<Tag>> GetListAsync(IEnumerable<Guid> ids);
Task<List<Tag>> GetListAsync(IEnumerable<Guid> ids, CancellationToken cancellationToken = default);
Task DecreaseUsageCountOfTagsAsync(List<Guid> id, CancellationToken cancellationToken = default);
}

5
modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Blogs/EfCoreBlogRepository.cs

@ -1,4 +1,5 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
@ -15,9 +16,9 @@ namespace Volo.Blogging.Blogs
}
public async Task<Blog> FindByShortNameAsync(string shortName)
public async Task<Blog> FindByShortNameAsync(string shortName, CancellationToken cancellationToken = default)
{
return await (await GetDbSetAsync()).FirstOrDefaultAsync(p => p.ShortName == shortName);
return await (await GetDbSetAsync()).FirstOrDefaultAsync(p => p.ShortName == shortName, GetCancellationToken(cancellationToken));
}
}
}

17
modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Comments/EfCoreCommentRepository.cs

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
@ -16,29 +17,29 @@ namespace Volo.Blogging.Comments
{
}
public async Task<List<Comment>> GetListOfPostAsync(Guid postId)
public async Task<List<Comment>> GetListOfPostAsync(Guid postId, CancellationToken cancellationToken = default)
{
return await (await GetDbSetAsync())
.Where(a => a.PostId == postId)
.OrderBy(a => a.CreationTime)
.ToListAsync();
.ToListAsync(GetCancellationToken(cancellationToken));
}
public async Task<int> GetCommentCountOfPostAsync(Guid postId)
public async Task<int> GetCommentCountOfPostAsync(Guid postId, CancellationToken cancellationToken = default)
{
return await (await GetDbSetAsync())
.CountAsync(a => a.PostId == postId);
.CountAsync(a => a.PostId == postId, GetCancellationToken(cancellationToken));
}
public async Task<List<Comment>> GetRepliesOfComment(Guid id)
public async Task<List<Comment>> GetRepliesOfComment(Guid id, CancellationToken cancellationToken = default)
{
return await (await GetDbSetAsync())
.Where(a => a.RepliedCommentId == id).ToListAsync();
.Where(a => a.RepliedCommentId == id).ToListAsync(GetCancellationToken(cancellationToken));
}
public async Task DeleteOfPost(Guid id)
public async Task DeleteOfPost(Guid id, CancellationToken cancellationToken = default)
{
var recordsToDelete = (await GetDbSetAsync()).Where(pt => pt.PostId == id);
var recordsToDelete = await (await GetDbSetAsync()).Where(pt => pt.PostId == id).ToListAsync(GetCancellationToken(cancellationToken));
(await GetDbSetAsync()).RemoveRange(recordsToDelete);
}
}

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

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Domain.Entities;
@ -18,12 +19,12 @@ namespace Volo.Blogging.Posts
}
public async Task<List<Post>> GetPostsByBlogId(Guid id)
public async Task<List<Post>> GetPostsByBlogId(Guid id, CancellationToken cancellationToken = default)
{
return await (await GetDbSetAsync()).Where(p => p.BlogId == id).OrderByDescending(p=>p.CreationTime).ToListAsync();
return await (await GetDbSetAsync()).Where(p => p.BlogId == id).OrderByDescending(p=>p.CreationTime).ToListAsync(GetCancellationToken(cancellationToken));
}
public async Task<bool> IsPostUrlInUseAsync(Guid blogId, string url, Guid? excludingPostId = null)
public async Task<bool> IsPostUrlInUseAsync(Guid blogId, string url, Guid? excludingPostId = null, CancellationToken cancellationToken = default)
{
var query = (await GetDbSetAsync()).Where(p => blogId == p.BlogId && p.Url == url);
@ -32,12 +33,12 @@ namespace Volo.Blogging.Posts
query = query.Where(p => excludingPostId != p.Id);
}
return await query.AnyAsync();
return await query.AnyAsync(GetCancellationToken(cancellationToken));
}
public async Task<Post> GetPostByUrl(Guid blogId, string url)
public async Task<Post> GetPostByUrl(Guid blogId, string url, CancellationToken cancellationToken = default)
{
var post = await (await GetDbSetAsync()).FirstOrDefaultAsync(p => p.BlogId == blogId && p.Url == url);
var post = await (await GetDbSetAsync()).FirstOrDefaultAsync(p => p.BlogId == blogId && p.Url == url, GetCancellationToken(cancellationToken));
if (post == null)
{
@ -47,15 +48,15 @@ namespace Volo.Blogging.Posts
return post;
}
public async Task<List<Post>> GetOrderedList(Guid blogId,bool descending = false)
public async Task<List<Post>> GetOrderedList(Guid blogId,bool descending = false, CancellationToken cancellationToken = default)
{
if (!descending)
{
return await (await GetDbSetAsync()).Where(x=>x.BlogId==blogId).OrderByDescending(x => x.CreationTime).ToListAsync();
return await (await GetDbSetAsync()).Where(x=>x.BlogId==blogId).OrderByDescending(x => x.CreationTime).ToListAsync(GetCancellationToken(cancellationToken));
}
else
{
return await (await GetDbSetAsync()).Where(x => x.BlogId == blogId).OrderBy(x => x.CreationTime).ToListAsync();
return await (await GetDbSetAsync()).Where(x => x.BlogId == blogId).OrderBy(x => x.CreationTime).ToListAsync(GetCancellationToken(cancellationToken));
}
}

16
modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Tagging/EfCoreTagRepository.cs

@ -17,24 +17,24 @@ namespace Volo.Blogging.Tagging
{
}
public async Task<List<Tag>> GetListAsync(Guid blogId)
public async Task<List<Tag>> GetListAsync(Guid blogId, CancellationToken cancellationToken = default)
{
return await (await GetDbSetAsync()).Where(t=>t.BlogId == blogId).ToListAsync();
return await (await GetDbSetAsync()).Where(t=>t.BlogId == blogId).ToListAsync(GetCancellationToken(cancellationToken));
}
public async Task<Tag> GetByNameAsync(Guid blogId, string name)
public async Task<Tag> GetByNameAsync(Guid blogId, string name, CancellationToken cancellationToken = default)
{
return await (await GetDbSetAsync()).FirstAsync(t=> t.BlogId == blogId && t.Name == name);
return await (await GetDbSetAsync()).FirstAsync(t=> t.BlogId == blogId && t.Name == name, GetCancellationToken(cancellationToken));
}
public async Task<Tag> FindByNameAsync(Guid blogId, string name)
public async Task<Tag> FindByNameAsync(Guid blogId, string name, CancellationToken cancellationToken = default)
{
return await (await GetDbSetAsync()).FirstOrDefaultAsync(t => t.BlogId == blogId && t.Name == name);
return await (await GetDbSetAsync()).FirstOrDefaultAsync(t => t.BlogId == blogId && t.Name == name, GetCancellationToken(cancellationToken));
}
public async Task<List<Tag>> GetListAsync(IEnumerable<Guid> ids)
public async Task<List<Tag>> GetListAsync(IEnumerable<Guid> ids, CancellationToken cancellationToken = default)
{
return await (await GetDbSetAsync()).Where(t => ids.Contains(t.Id)).ToListAsync();
return await (await GetDbSetAsync()).Where(t => ids.Contains(t.Id)).ToListAsync(GetCancellationToken(cancellationToken));
}
public async Task DecreaseUsageCountOfTagsAsync(List<Guid> ids, CancellationToken cancellationToken = default)

5
modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Blogs/MongoBlogRepository.cs

@ -1,4 +1,5 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Driver.Linq;
using Volo.Abp.Domain.Repositories.MongoDB;
@ -13,9 +14,9 @@ namespace Volo.Blogging.Blogs
{
}
public async Task<Blog> FindByShortNameAsync(string shortName)
public async Task<Blog> FindByShortNameAsync(string shortName, CancellationToken cancellationToken = default)
{
return await (await GetMongoQueryableAsync()).FirstOrDefaultAsync(p => p.ShortName == shortName);
return await (await GetMongoQueryableAsync(GetCancellationToken(cancellationToken))).FirstOrDefaultAsync(p => p.ShortName == shortName, GetCancellationToken(cancellationToken));
}
}
}

25
modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Comments/MongoCommentRepository.cs

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
@ -15,33 +16,33 @@ namespace Volo.Blogging.Comments
{
}
public async Task<List<Comment>> GetListOfPostAsync(Guid postId)
public async Task<List<Comment>> GetListOfPostAsync(Guid postId, CancellationToken cancellationToken = default)
{
return await (await GetMongoQueryableAsync())
return await (await GetMongoQueryableAsync(GetCancellationToken(cancellationToken)))
.Where(a => a.PostId == postId)
.OrderBy(a => a.CreationTime)
.ToListAsync();
.ToListAsync(GetCancellationToken(cancellationToken));
}
public async Task<int> GetCommentCountOfPostAsync(Guid postId)
public async Task<int> GetCommentCountOfPostAsync(Guid postId, CancellationToken cancellationToken = default)
{
return await (await GetMongoQueryableAsync())
.CountAsync(a => a.PostId == postId);
return await (await GetMongoQueryableAsync(GetCancellationToken(cancellationToken)))
.CountAsync(a => a.PostId == postId, GetCancellationToken(cancellationToken));
}
public async Task<List<Comment>> GetRepliesOfComment(Guid id)
public async Task<List<Comment>> GetRepliesOfComment(Guid id, CancellationToken cancellationToken = default)
{
return await (await GetMongoQueryableAsync())
.Where(a => a.RepliedCommentId == id).ToListAsync();
return await (await GetMongoQueryableAsync(GetCancellationToken(cancellationToken)))
.Where(a => a.RepliedCommentId == id).ToListAsync(GetCancellationToken(cancellationToken));
}
public async Task DeleteOfPost(Guid id)
public async Task DeleteOfPost(Guid id, CancellationToken cancellationToken = default)
{
var recordsToDelete = (await GetMongoQueryableAsync()).Where(pt => pt.PostId == id);
var recordsToDelete = (await GetMongoQueryableAsync(GetCancellationToken(cancellationToken))).Where(pt => pt.PostId == id);
foreach (var record in recordsToDelete)
{
await DeleteAsync(record);
await DeleteAsync(record, cancellationToken: GetCancellationToken(cancellationToken));
}
}
}

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

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
@ -16,27 +17,27 @@ namespace Volo.Blogging.Posts
{
}
public async Task<List<Post>> GetPostsByBlogId(Guid id)
public async Task<List<Post>> GetPostsByBlogId(Guid id, CancellationToken cancellationToken = default)
{
return await (await GetMongoQueryableAsync()).Where(p => p.BlogId == id).OrderByDescending(p => p.CreationTime).ToListAsync();
return await (await GetMongoQueryableAsync(GetCancellationToken(cancellationToken))).Where(p => p.BlogId == id).OrderByDescending(p => p.CreationTime).ToListAsync(GetCancellationToken(cancellationToken));
}
public async Task<bool> IsPostUrlInUseAsync(Guid blogId, string url, Guid? excludingPostId = null)
public async Task<bool> IsPostUrlInUseAsync(Guid blogId, string url, Guid? excludingPostId = null, CancellationToken cancellationToken = default)
{
var query = (await GetMongoQueryableAsync()).Where(p => blogId == p.BlogId && p.Url == url);
var query = (await GetMongoQueryableAsync(GetCancellationToken(cancellationToken))).Where(p => blogId == p.BlogId && p.Url == url);
if (excludingPostId != null)
{
query = query.Where(p => excludingPostId != p.Id);
}
return await query.AnyAsync();
return await query.AnyAsync(GetCancellationToken(cancellationToken));
}
public async Task<Post> GetPostByUrl(Guid blogId, string url)
public async Task<Post> GetPostByUrl(Guid blogId, string url, CancellationToken cancellationToken = default)
{
var post = await (await GetMongoQueryableAsync()).FirstOrDefaultAsync(p => p.BlogId == blogId && p.Url == url);
var post = await (await GetMongoQueryableAsync(GetCancellationToken(cancellationToken))).FirstOrDefaultAsync(p => p.BlogId == blogId && p.Url == url, GetCancellationToken(cancellationToken));
if (post == null)
{
@ -46,16 +47,16 @@ namespace Volo.Blogging.Posts
return post;
}
public async Task<List<Post>> GetOrderedList(Guid blogId, bool @descending = false)
public async Task<List<Post>> GetOrderedList(Guid blogId, bool @descending = false, CancellationToken cancellationToken = default)
{
var query = (await GetMongoQueryableAsync()).Where(x => x.BlogId == blogId);
var query = (await GetMongoQueryableAsync(GetCancellationToken(cancellationToken))).Where(x => x.BlogId == blogId);
if (!descending)
{
return await query.OrderBy(x => x.CreationTime).ToListAsync();
return await query.OrderBy(x => x.CreationTime).ToListAsync(GetCancellationToken(cancellationToken));
}
return await query.OrderByDescending(x => x.CreationTime).ToListAsync();
return await query.OrderByDescending(x => x.CreationTime).ToListAsync(GetCancellationToken(cancellationToken));
}
}
}

18
modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Tagging/MongoTagRepository.cs

@ -18,29 +18,29 @@ namespace Volo.Blogging.Tagging
{
}
public async Task<List<Tag>> GetListAsync(Guid blogId)
public async Task<List<Tag>> GetListAsync(Guid blogId, CancellationToken cancellationToken = default)
{
return await (await GetMongoQueryableAsync()).Where(t => t.BlogId == blogId).ToListAsync();
return await (await GetMongoQueryableAsync(GetCancellationToken(cancellationToken))).Where(t => t.BlogId == blogId).ToListAsync(GetCancellationToken(cancellationToken));
}
public async Task<Tag> GetByNameAsync(Guid blogId, string name)
public async Task<Tag> GetByNameAsync(Guid blogId, string name, CancellationToken cancellationToken = default)
{
return await (await GetMongoQueryableAsync()).Where(t => t.BlogId == blogId && t.Name == name).FirstAsync();
return await (await GetMongoQueryableAsync(GetCancellationToken(cancellationToken))).Where(t => t.BlogId == blogId && t.Name == name).FirstAsync(GetCancellationToken(cancellationToken));
}
public async Task<Tag> FindByNameAsync(Guid blogId, string name)
public async Task<Tag> FindByNameAsync(Guid blogId, string name, CancellationToken cancellationToken = default)
{
return await (await GetMongoQueryableAsync()).Where(t => t.BlogId == blogId && t.Name == name).FirstOrDefaultAsync();
return await (await GetMongoQueryableAsync(GetCancellationToken(cancellationToken))).Where(t => t.BlogId == blogId && t.Name == name).FirstOrDefaultAsync(GetCancellationToken(cancellationToken));
}
public async Task<List<Tag>> GetListAsync(IEnumerable<Guid> ids)
public async Task<List<Tag>> GetListAsync(IEnumerable<Guid> ids, CancellationToken cancellationToken = default)
{
return await (await GetMongoQueryableAsync()).Where(t => ids.Contains(t.Id)).ToListAsync();
return await (await GetMongoQueryableAsync(GetCancellationToken(cancellationToken))).Where(t => ids.Contains(t.Id)).ToListAsync(GetCancellationToken(cancellationToken));
}
public async Task DecreaseUsageCountOfTagsAsync(List<Guid> ids, CancellationToken cancellationToken = default)
{
var tags = await (await GetMongoQueryableAsync())
var tags = await (await GetMongoQueryableAsync(GetCancellationToken(cancellationToken)))
.Where(t => ids.Contains(t.Id))
.ToListAsync(GetCancellationToken(cancellationToken));

6
modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Users/MongoBlogUserRepository.cs

@ -15,16 +15,16 @@ namespace Volo.Blogging.Users
{
}
public async Task<List<BlogUser>> GetUsersAsync(int maxCount, string filter, CancellationToken cancellationToken)
public async Task<List<BlogUser>> GetUsersAsync(int maxCount, string filter, CancellationToken cancellationToken = default)
{
var query = await GetMongoQueryableAsync(cancellationToken);
var query = await GetMongoQueryableAsync(GetCancellationToken(cancellationToken));
if (!string.IsNullOrWhiteSpace(filter))
{
query = query.Where(x => x.UserName.Contains(filter));
}
return await query.Take(maxCount).ToListAsync(cancellationToken);
return await query.Take(maxCount).ToListAsync(GetCancellationToken(cancellationToken));
}
}
}

Loading…
Cancel
Save