diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/BlogManager.cs b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/BlogManager.cs index e92ababf15..1d2e2f5b07 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/BlogManager.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/BlogManager.cs @@ -1,8 +1,6 @@ -using System; -using System.Threading.Tasks; +using System.Threading.Tasks; using JetBrains.Annotations; using Volo.Abp.Domain.Services; -using Volo.Abp.MultiTenancy; namespace Volo.CmsKit.Blogs { @@ -19,7 +17,7 @@ namespace Volo.CmsKit.Blogs { await CheckSlugAsync(slug); - return new Blog(GuidGenerator.Create(), name, slug, CurrentTenant.GetId()); + return new Blog(GuidGenerator.Create(), name, slug, CurrentTenant.Id); } public virtual async Task UpdateAsync([NotNull] Blog blog, [NotNull] string name, [NotNull] string slug) diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/IBlogRepository.cs b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/IBlogRepository.cs index 26b150d93d..b340b9e256 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/IBlogRepository.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/IBlogRepository.cs @@ -23,7 +23,7 @@ namespace Volo.CmsKit.Blogs Task GetBySlugAsync(string slug, CancellationToken cancellationToken = default); - Task ExistsAsync(Guid blogId, CancellationToken cancellationToken = default); + Task ExistsAsync(Guid id, CancellationToken cancellationToken = default); Task SlugExistsAsync(string slug, CancellationToken cancellationToken = default); } diff --git a/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Blogs/EfCoreBlogRepository.cs b/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Blogs/EfCoreBlogRepository.cs index c39dee2685..bfcb6d3cab 100644 --- a/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Blogs/EfCoreBlogRepository.cs +++ b/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Blogs/EfCoreBlogRepository.cs @@ -1,9 +1,11 @@ -using Microsoft.EntityFrameworkCore; -using System; -using System.Security.Cryptography.X509Certificates; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Dynamic.Core; using System.Threading; using System.Threading.Tasks; using JetBrains.Annotations; +using Microsoft.EntityFrameworkCore; using Volo.Abp; using Volo.Abp.Domain.Repositories.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore; @@ -17,15 +19,47 @@ namespace Volo.CmsKit.Blogs { } - public virtual async Task ExistsAsync(Guid blogId, CancellationToken cancellationToken = default) + public virtual async Task ExistsAsync(Guid id, CancellationToken cancellationToken = default) { - return await (await GetQueryableAsync()).AnyAsync(x => x.Id == blogId, GetCancellationToken(cancellationToken)); + return await (await GetQueryableAsync()).AnyAsync(x => x.Id == id, GetCancellationToken(cancellationToken)); } - public virtual Task GetBySlugAsync([NotNull]string slug, CancellationToken cancellationToken = default) + public virtual async Task SlugExistsAsync(string slug, CancellationToken cancellationToken = default) + { + return await (await GetDbSetAsync()).AnyAsync(x => x.Slug == slug, GetCancellationToken(cancellationToken)); + } + + public virtual async Task> GetListAsync( + string filter = null, + string sorting = null, + int maxResultCount = int.MaxValue, + int skipCount = 0, + CancellationToken cancellationToken = default) + { + var query = await GetListQueryAsync(filter); + + return await query.OrderBy(sorting ?? "creationTime desc") + .PageBy(skipCount, maxResultCount) + .ToListAsync(GetCancellationToken(cancellationToken)); + } + + public virtual async Task GetCountAsync(string filter = null, CancellationToken cancellationToken = default) + { + var query = await GetListQueryAsync(filter); + + return await query.LongCountAsync(GetCancellationToken(cancellationToken)); + } + + public virtual Task GetBySlugAsync([NotNull] string slug, CancellationToken cancellationToken = default) { Check.NotNullOrEmpty(slug, nameof(slug)); return GetAsync(x => x.Slug == slug, cancellationToken: GetCancellationToken(cancellationToken)); } + + protected virtual async Task> GetListQueryAsync(string filter = null) + { + return (await GetDbSetAsync()) + .WhereIf(!filter.IsNullOrWhiteSpace(), b => b.Name.Contains(filter)); + } } } diff --git a/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Blogs/MongoBlogRepository.cs b/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Blogs/MongoBlogRepository.cs index 0cd46a130c..32119610bb 100644 --- a/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Blogs/MongoBlogRepository.cs +++ b/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Blogs/MongoBlogRepository.cs @@ -1,8 +1,12 @@ -using MongoDB.Driver.Core.Operations; -using System; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Dynamic.Core; using System.Threading; using System.Threading.Tasks; using JetBrains.Annotations; +using MongoDB.Driver; +using MongoDB.Driver.Linq; using Volo.Abp; using Volo.Abp.Domain.Repositories.MongoDB; using Volo.Abp.MongoDB; @@ -16,18 +20,54 @@ namespace Volo.CmsKit.MongoDB.Blogs { } - public virtual async Task ExistsAsync(Guid blogId, CancellationToken cancellationToken = default) + public virtual async Task ExistsAsync(Guid id, CancellationToken cancellationToken = default) { - return await AsyncExecuter.AnyAsync( - await GetQueryableAsync(), - x => x.Id == blogId, - cancellationToken); + var token = GetCancellationToken(cancellationToken); + return await (await GetMongoQueryableAsync(token)).AnyAsync(x => x.Id == id, token); } - public virtual Task GetBySlugAsync([NotNull]string slug, CancellationToken cancellationToken = default) + public virtual async Task SlugExistsAsync(string slug, CancellationToken cancellationToken = default) + { + var token = GetCancellationToken(cancellationToken); + return await (await GetMongoQueryableAsync(token)).AnyAsync(x => x.Slug == slug, token); + } + + public virtual async Task> GetListAsync( + string filter = null, + string sorting = null, + int maxResultCount = int.MaxValue, + int skipCount = 0, + CancellationToken cancellationToken = default) + { + var token = GetCancellationToken(cancellationToken); + + var query = await GetListQueryAsync(filter, token); + + return await query.OrderBy(sorting ?? "creationTime desc") + .As>() + .PageBy>(skipCount, maxResultCount) + .ToListAsync(token); + } + + public virtual async Task GetCountAsync(string filter = null, CancellationToken cancellationToken = default) + { + var token = GetCancellationToken(cancellationToken); + + var query = await GetListQueryAsync(filter, token); + + return await query.As>().LongCountAsync(token); + } + + public virtual Task GetBySlugAsync([NotNull] string slug, CancellationToken cancellationToken = default) { Check.NotNullOrEmpty(slug, nameof(slug)); return GetAsync(x => x.Slug == slug, cancellationToken: cancellationToken); } + + protected virtual async Task> GetListQueryAsync(string filter = null, CancellationToken cancellationToken = default) + { + return (await GetMongoQueryableAsync(GetCancellationToken(cancellationToken))) + .WhereIf(!filter.IsNullOrWhiteSpace(), b => b.Name.Contains(filter)); + } } }