Skip to content

Commit

Permalink
Merge pull request #6985 from abpframework/maliming/async-repository
Browse files Browse the repository at this point in the history
Use async repository method in Blogging,CmsKit,Docs module.
  • Loading branch information
realLiangshiwei authored Jan 4, 2021
2 parents 379a215 + bb1b8f0 commit 637fdcc
Show file tree
Hide file tree
Showing 23 changed files with 103 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public EfCoreBlogRepository(IDbContextProvider<IBloggingDbContext> dbContextProv

public async Task<Blog> FindByShortNameAsync(string shortName)
{
return await DbSet.FirstOrDefaultAsync(p => p.ShortName == shortName);
return await (await GetDbSetAsync()).FirstOrDefaultAsync(p => p.ShortName == shortName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,35 @@ namespace Volo.Blogging.Comments
{
public class EfCoreCommentRepository : EfCoreRepository<IBloggingDbContext, Comment, Guid>, ICommentRepository
{
public EfCoreCommentRepository(IDbContextProvider<IBloggingDbContext> dbContextProvider)
public EfCoreCommentRepository(IDbContextProvider<IBloggingDbContext> dbContextProvider)
: base(dbContextProvider)
{
}

public async Task<List<Comment>> GetListOfPostAsync(Guid postId)
{
return await DbSet
return await (await GetDbSetAsync())
.Where(a => a.PostId == postId)
.OrderBy(a => a.CreationTime)
.ToListAsync();
}

public async Task<int> GetCommentCountOfPostAsync(Guid postId)
{
return await DbSet
return await (await GetDbSetAsync())
.CountAsync(a => a.PostId == postId);
}

public async Task<List<Comment>> GetRepliesOfComment(Guid id)
{
return await DbSet
return await (await GetDbSetAsync())
.Where(a => a.RepliedCommentId == id).ToListAsync();
}

public async Task DeleteOfPost(Guid id)
{
var recordsToDelete = DbSet.Where(pt => pt.PostId == id);
DbSet.RemoveRange(recordsToDelete);
var recordsToDelete = (await GetDbSetAsync()).Where(pt => pt.PostId == id);
(await GetDbSetAsync()).RemoveRange(recordsToDelete);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,24 @@ public EfCorePostRepository(IDbContextProvider<IBloggingDbContext> dbContextProv

public async Task<List<Post>> GetPostsByBlogId(Guid id)
{
return await DbSet.Where(p => p.BlogId == id).OrderByDescending(p=>p.CreationTime).ToListAsync();
return await (await GetDbSetAsync()).Where(p => p.BlogId == id).OrderByDescending(p=>p.CreationTime).ToListAsync();
}

public Task<bool> IsPostUrlInUseAsync(Guid blogId, string url, Guid? excludingPostId = null)
public async Task<bool> IsPostUrlInUseAsync(Guid blogId, string url, Guid? excludingPostId = null)
{
var query = DbSet.Where(p => blogId == p.BlogId && p.Url == url);
var query = (await GetDbSetAsync()).Where(p => blogId == p.BlogId && p.Url == url);

if (excludingPostId != null)
{
query = query.Where(p => excludingPostId != p.Id);
}

return query.AnyAsync();
return await query.AnyAsync();
}

public async Task<Post> GetPostByUrl(Guid blogId, string url)
{
var post = await DbSet.FirstOrDefaultAsync(p => p.BlogId == blogId && p.Url == url);
var post = await (await GetDbSetAsync()).FirstOrDefaultAsync(p => p.BlogId == blogId && p.Url == url);

if (post == null)
{
Expand All @@ -51,18 +51,18 @@ 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();
return await (await GetDbSetAsync()).Where(x=>x.BlogId==blogId).OrderByDescending(x => x.CreationTime).ToListAsync();
}
else
{
return await DbSet.Where(x => x.BlogId == blogId).OrderBy(x => x.CreationTime).ToListAsync();
return await (await GetDbSetAsync()).Where(x => x.BlogId == blogId).OrderBy(x => x.CreationTime).ToListAsync();
}

}

public override IQueryable<Post> WithDetails()
public override async Task<IQueryable<Post>> WithDetailsAsync()
{
return GetQueryable().IncludeDetails();
return (await GetQueryableAsync()).IncludeDetails();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,27 @@ public EfCoreTagRepository(IDbContextProvider<IBloggingDbContext> dbContextProvi

public async Task<List<Tag>> GetListAsync(Guid blogId)
{
return await DbSet.Where(t=>t.BlogId == blogId).ToListAsync();
return await (await GetDbSetAsync()).Where(t=>t.BlogId == blogId).ToListAsync();
}

public async Task<Tag> GetByNameAsync(Guid blogId, string name)
{
return await DbSet.FirstAsync(t=> t.BlogId == blogId && t.Name == name);
return await (await GetDbSetAsync()).FirstAsync(t=> t.BlogId == blogId && t.Name == name);
}

public async Task<Tag> FindByNameAsync(Guid blogId, string name)
{
return await DbSet.FirstOrDefaultAsync(t => t.BlogId == blogId && t.Name == name);
return await (await GetDbSetAsync()).FirstOrDefaultAsync(t => t.BlogId == blogId && t.Name == name);
}

public async Task<List<Tag>> GetListAsync(IEnumerable<Guid> ids)
{
return await DbSet.Where(t => ids.Contains(t.Id)).ToListAsync();
return await (await GetDbSetAsync()).Where(t => ids.Contains(t.Id)).ToListAsync();
}

public async Task DecreaseUsageCountOfTagsAsync(List<Guid> ids, CancellationToken cancellationToken = default)
{
var tags = await DbSet
var tags = await (await GetDbSetAsync())
.Where(t => ids.Any(id => id == t.Id))
.ToListAsync(GetCancellationToken(cancellationToken));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public MongoBlogRepository(IMongoDbContextProvider<IBloggingMongoDbContext> dbCo

public async Task<Blog> FindByShortNameAsync(string shortName)
{
return await GetMongoQueryable().FirstOrDefaultAsync(p => p.ShortName == shortName);
return await (await GetMongoQueryableAsync()).FirstOrDefaultAsync(p => p.ShortName == shortName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,27 @@ public MongoCommentRepository(IMongoDbContextProvider<IBloggingMongoDbContext> d

public async Task<List<Comment>> GetListOfPostAsync(Guid postId)
{
return await GetMongoQueryable()
return await (await GetMongoQueryableAsync())
.Where(a => a.PostId == postId)
.OrderBy(a => a.CreationTime)
.ToListAsync();
}

public async Task<int> GetCommentCountOfPostAsync(Guid postId)
{
return await GetMongoQueryable()
return await (await GetMongoQueryableAsync())
.CountAsync(a => a.PostId == postId);
}

public async Task<List<Comment>> GetRepliesOfComment(Guid id)
{
return await GetMongoQueryable()
return await (await GetMongoQueryableAsync())
.Where(a => a.RepliedCommentId == id).ToListAsync();
}

public async Task DeleteOfPost(Guid id)
{
var recordsToDelete = GetMongoQueryable().Where(pt => pt.PostId == id);
var recordsToDelete = (await GetMongoQueryableAsync()).Where(pt => pt.PostId == id);

foreach (var record in recordsToDelete)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@ public MongoPostRepository(IMongoDbContextProvider<IBloggingMongoDbContext> dbCo

public async Task<List<Post>> GetPostsByBlogId(Guid id)
{
return await GetMongoQueryable().Where(p => p.BlogId == id).OrderByDescending(p => p.CreationTime).ToListAsync();
return await (await GetMongoQueryableAsync()).Where(p => p.BlogId == id).OrderByDescending(p => p.CreationTime).ToListAsync();
}


public Task<bool> IsPostUrlInUseAsync(Guid blogId, string url, Guid? excludingPostId = null)
public async Task<bool> IsPostUrlInUseAsync(Guid blogId, string url, Guid? excludingPostId = null)
{
var query = GetMongoQueryable().Where(p => blogId == p.BlogId && p.Url == url);
var query = (await GetMongoQueryableAsync()).Where(p => blogId == p.BlogId && p.Url == url);

if (excludingPostId != null)
{
query = query.Where(p => excludingPostId != p.Id);
}

return query.AnyAsync();
return await query.AnyAsync();
}

public async Task<Post> GetPostByUrl(Guid blogId, string url)
{
var post = await GetMongoQueryable().FirstOrDefaultAsync(p => p.BlogId == blogId && p.Url == url);
var post = await (await GetMongoQueryableAsync()).FirstOrDefaultAsync(p => p.BlogId == blogId && p.Url == url);

if (post == null)
{
Expand All @@ -48,7 +48,7 @@ public async Task<Post> GetPostByUrl(Guid blogId, string url)

public async Task<List<Post>> GetOrderedList(Guid blogId, bool @descending = false)
{
var query = GetMongoQueryable().Where(x => x.BlogId == blogId);
var query = (await GetMongoQueryableAsync()).Where(x => x.BlogId == blogId);

if (!descending)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,27 @@ public MongoTagRepository(IMongoDbContextProvider<IBloggingMongoDbContext> dbCon

public async Task<List<Tag>> GetListAsync(Guid blogId)
{
return await GetMongoQueryable().Where(t => t.BlogId == blogId).ToListAsync();
return await (await GetMongoQueryableAsync()).Where(t => t.BlogId == blogId).ToListAsync();
}

public async Task<Tag> GetByNameAsync(Guid blogId, string name)
{
return await GetMongoQueryable().Where(t => t.BlogId == blogId && t.Name == name).FirstAsync();
return await (await GetMongoQueryableAsync()).Where(t => t.BlogId == blogId && t.Name == name).FirstAsync();
}

public async Task<Tag> FindByNameAsync(Guid blogId, string name)
{
return await GetMongoQueryable().Where(t => t.BlogId == blogId && t.Name == name).FirstOrDefaultAsync();
return await (await GetMongoQueryableAsync()).Where(t => t.BlogId == blogId && t.Name == name).FirstOrDefaultAsync();
}

public async Task<List<Tag>> GetListAsync(IEnumerable<Guid> ids)
{
return await GetMongoQueryable().Where(t => ids.Contains(t.Id)).ToListAsync();
return await (await GetMongoQueryableAsync()).Where(t => ids.Contains(t.Id)).ToListAsync();
}

public async Task DecreaseUsageCountOfTagsAsync(List<Guid> ids, CancellationToken cancellationToken = default)
{
var tags = await GetMongoQueryable()
var tags = await (await GetMongoQueryableAsync())
.Where(t => ids.Contains(t.Id))
.ToListAsync(GetCancellationToken(cancellationToken));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public MongoBlogUserRepository(IMongoDbContextProvider<IBloggingMongoDbContext>

public async Task<List<BlogUser>> GetUsersAsync(int maxCount, string filter, CancellationToken cancellationToken)
{
var query = GetMongoQueryable();
var query = await GetMongoQueryableAsync(cancellationToken);

if (!string.IsNullOrWhiteSpace(filter))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public async Task<List<CommentWithAuthorQueryResultItem>> GetListWithAuthorsAsyn
Check.NotNullOrWhiteSpace(entityType, nameof(entityType));
Check.NotNullOrWhiteSpace(entityId, nameof(entityId));

var query = from comment in DbSet
join user in DbContext.Set<CmsUser>() on comment.CreatorId equals user.Id
var query = from comment in (await GetDbSetAsync())
join user in (await GetDbContextAsync()).Set<CmsUser>() on comment.CreatorId equals user.Id
where entityType == comment.EntityType && entityId == comment.EntityId
orderby comment.CreationTime
select new CommentWithAuthorQueryResultItem
Expand All @@ -45,7 +45,7 @@ public async Task DeleteWithRepliesAsync(
Comment comment,
CancellationToken cancellationToken = default)
{
var replies = await DbSet
var replies = await (await GetDbSetAsync())
.Where(x => x.RepliedCommentId == comment.Id)
.ToListAsync(GetCancellationToken(cancellationToken));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public Task<Page> FindByUrlAsync(string url)
return FindAsync(x => x.Url == url);
}

public Task<bool> DoesExistAsync(string url)
public async Task<bool> DoesExistAsync(string url)
{
return DbSet.AnyAsync(x => x.Url == url);
return await (await GetDbSetAsync()).AnyAsync(x => x.Url == url);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public async Task<Rating> GetCurrentUserRatingAsync(string entityType, string en
Check.NotNullOrWhiteSpace(entityType, nameof(entityType));
Check.NotNullOrWhiteSpace(entityId, nameof(entityId));

var rating = await DbSet.FirstOrDefaultAsync(
var rating = await (await GetDbSetAsync()).FirstOrDefaultAsync(
r => r.EntityType == entityType && r.EntityId == entityId && r.CreatorId == userId,
GetCancellationToken(cancellationToken));

Expand All @@ -38,7 +38,7 @@ public async Task<List<RatingWithStarCountQueryResultItem>> GetGroupedStarCounts
Check.NotNullOrWhiteSpace(entityId, nameof(entityId));

var query = (
from rating in DbSet
from rating in (await GetDbSetAsync())
where rating.EntityType == entityType && rating.EntityId == entityId
group rating by rating.StarCount
into g
Expand All @@ -54,4 +54,4 @@ into g
return ratings;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public async Task<UserReaction> FindAsync(
Check.NotNullOrWhiteSpace(entityId, nameof(entityId));
Check.NotNullOrWhiteSpace(reactionName, nameof(reactionName));

return await DbSet
return await (await GetDbSetAsync())
.Where(x =>
x.CreatorId == userId &&
x.EntityType == entityType &&
Expand All @@ -48,7 +48,7 @@ public async Task<List<UserReaction>> GetListForUserAsync(
Check.NotNullOrWhiteSpace(entityType, nameof(entityType));
Check.NotNullOrWhiteSpace(entityId, nameof(entityId));

return await DbSet
return await (await GetDbSetAsync())
.Where(x =>
x.CreatorId == userId &&
x.EntityType == entityType &&
Expand All @@ -64,7 +64,7 @@ public async Task<List<ReactionSummaryQueryResultItem>> GetSummariesAsync(
Check.NotNullOrWhiteSpace(entityType, nameof(entityType));
Check.NotNullOrWhiteSpace(entityId, nameof(entityId));

return await DbSet
return await (await GetDbSetAsync())
.Where(x =>
x.EntityType == entityType &&
x.EntityId == entityId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public EfCoreTagRepository(IDbContextProvider<ICmsKitDbContext> dbContextProvide
{
}

public virtual Task<bool> AnyAsync(
public virtual async Task<bool> AnyAsync(
[NotNull] string entityType,
[NotNull] string name,
Guid? tenantId = null,
CancellationToken cancellationToken = default)
{
return DbSet.AnyAsync(x =>
return await (await GetDbSetAsync()).AnyAsync(x =>
x.EntityType == entityType &&
x.Name == name &&
x.TenantId == tenantId,
Expand Down Expand Up @@ -64,12 +64,12 @@ public virtual async Task<List<Tag>> GetAllRelatedTagsAsync(
Guid? tenantId = null,
CancellationToken cancellationToken = default)
{
var entityTagIds = await DbContext.Set<EntityTag>()
var entityTagIds = await (await GetDbContextAsync()).Set<EntityTag>()
.Where(q => q.EntityId == entityId && q.TenantId == tenantId)
.Select(q => q.TagId)
.ToListAsync(cancellationToken: GetCancellationToken(cancellationToken));

var query = DbSet
var query = (await GetDbSetAsync())
.Where(x => x.EntityType == entityType &&
x.TenantId == tenantId &&
entityTagIds.Contains(x.Id));
Expand Down
Loading

0 comments on commit 637fdcc

Please sign in to comment.