|
|
|
@ -12,6 +12,7 @@ using Volo.Abp.Domain.Entities; |
|
|
|
using Volo.Abp.Domain.Repositories.MongoDB; |
|
|
|
using Volo.Abp.MongoDB; |
|
|
|
using Volo.CmsKit.Blogs; |
|
|
|
using Volo.CmsKit.MarkedItems; |
|
|
|
using Volo.CmsKit.Tags; |
|
|
|
using Volo.CmsKit.Users; |
|
|
|
|
|
|
|
@ -19,10 +20,15 @@ namespace Volo.CmsKit.MongoDB.Blogs; |
|
|
|
|
|
|
|
public class MongoBlogPostRepository : MongoDbRepository<CmsKitMongoDbContext, BlogPost, Guid>, IBlogPostRepository |
|
|
|
{ |
|
|
|
private readonly MarkedItemManager _markedItemManager; |
|
|
|
private EntityTagManager _entityTagManager; |
|
|
|
public MongoBlogPostRepository(IMongoDbContextProvider<CmsKitMongoDbContext> dbContextProvider, EntityTagManager entityTagManager) : base( |
|
|
|
public MongoBlogPostRepository( |
|
|
|
IMongoDbContextProvider<CmsKitMongoDbContext> dbContextProvider, |
|
|
|
MarkedItemManager markedItemManager, |
|
|
|
EntityTagManager entityTagManager) : base( |
|
|
|
dbContextProvider) |
|
|
|
{ |
|
|
|
_markedItemManager = markedItemManager; |
|
|
|
_entityTagManager = entityTagManager; |
|
|
|
} |
|
|
|
|
|
|
|
@ -48,15 +54,19 @@ public class MongoBlogPostRepository : MongoDbRepository<CmsKitMongoDbContext, B |
|
|
|
Guid? blogId = null, |
|
|
|
Guid? authorId = null, |
|
|
|
Guid? tagId = null, |
|
|
|
Guid? favoriteUserId = null, |
|
|
|
BlogPostStatus? statusFilter = null, |
|
|
|
CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
cancellationToken = GetCancellationToken(cancellationToken); |
|
|
|
|
|
|
|
var entityIdFilters = await GetEntityIdsByTagId(tagId, cancellationToken); |
|
|
|
var tagFilteredEntityIds = await GetEntityIdsByTagId(tagId, cancellationToken); |
|
|
|
|
|
|
|
var favoriteUserFilteredEntityIds = await GetFavoriteEntityIdsByUserId(favoriteUserId, cancellationToken); |
|
|
|
|
|
|
|
return await (await GetMongoQueryableAsync(cancellationToken)) |
|
|
|
.WhereIf<BlogPost, IMongoQueryable<BlogPost>>(entityIdFilters.Any(), x => entityIdFilters.Contains(x.Id)) |
|
|
|
.WhereIf<BlogPost, IMongoQueryable<BlogPost>>(tagFilteredEntityIds.Any(), x => tagFilteredEntityIds.Contains(x.Id)) |
|
|
|
.WhereIf<BlogPost, IMongoQueryable<BlogPost>>(favoriteUserFilteredEntityIds.Any(), x => favoriteUserFilteredEntityIds.Contains(x.Id)) |
|
|
|
.WhereIf<BlogPost, IMongoQueryable<BlogPost>>(!string.IsNullOrWhiteSpace(filter), x => x.Title.Contains(filter) || x.Slug.Contains(filter)) |
|
|
|
.WhereIf<BlogPost, IMongoQueryable<BlogPost>>(blogId.HasValue, x => x.BlogId == blogId) |
|
|
|
.WhereIf<BlogPost, IMongoQueryable<BlogPost>>(authorId.HasValue, x => x.AuthorId == authorId) |
|
|
|
@ -69,6 +79,7 @@ public class MongoBlogPostRepository : MongoDbRepository<CmsKitMongoDbContext, B |
|
|
|
Guid? blogId = null, |
|
|
|
Guid? authorId = null, |
|
|
|
Guid? tagId = null, |
|
|
|
Guid? favoriteUserId = null, |
|
|
|
BlogPostStatus? statusFilter = null, |
|
|
|
int maxResultCount = int.MaxValue, |
|
|
|
int skipCount = 0, |
|
|
|
@ -79,12 +90,15 @@ public class MongoBlogPostRepository : MongoDbRepository<CmsKitMongoDbContext, B |
|
|
|
var dbContext = await GetDbContextAsync(cancellationToken); |
|
|
|
var blogPostQueryable = await GetQueryableAsync(); |
|
|
|
|
|
|
|
var entityIdFilters = await GetEntityIdsByTagId(tagId, cancellationToken); |
|
|
|
var tagFilteredEntityIds = await GetEntityIdsByTagId(tagId, cancellationToken); |
|
|
|
|
|
|
|
var favoriteUserFilteredEntityIds = await GetFavoriteEntityIdsByUserId(favoriteUserId, cancellationToken); |
|
|
|
|
|
|
|
var usersQueryable = dbContext.Collection<CmsUser>().AsQueryable(); |
|
|
|
|
|
|
|
var queryable = blogPostQueryable |
|
|
|
.WhereIf(entityIdFilters.Any(), x => entityIdFilters.Contains(x.Id)) |
|
|
|
.WhereIf(tagFilteredEntityIds.Any(), x => tagFilteredEntityIds.Contains(x.Id)) |
|
|
|
.WhereIf(favoriteUserFilteredEntityIds.Any(), x => favoriteUserFilteredEntityIds.Contains(x.Id)) |
|
|
|
.WhereIf(blogId.HasValue, x => x.BlogId == blogId) |
|
|
|
.WhereIf(!string.IsNullOrWhiteSpace(filter), x => x.Title.Contains(filter) || x.Slug.Contains(filter)) |
|
|
|
.WhereIf(authorId.HasValue, x => x.AuthorId == authorId) |
|
|
|
@ -132,6 +146,28 @@ public class MongoBlogPostRepository : MongoDbRepository<CmsKitMongoDbContext, B |
|
|
|
return entityIdFilters; |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual async Task<List<Guid>> GetFavoriteEntityIdsByUserId(Guid? userId, CancellationToken cancellationToken) |
|
|
|
{ |
|
|
|
var entityIdFilters = new List<Guid>(); |
|
|
|
if (!userId.HasValue) |
|
|
|
{ |
|
|
|
return entityIdFilters; |
|
|
|
} |
|
|
|
|
|
|
|
var entityIds = |
|
|
|
await _markedItemManager.GetEntityIdsFilteredByUserAsync(userId.Value, BlogPostConsts.EntityType, CurrentTenant.Id, cancellationToken); |
|
|
|
|
|
|
|
foreach (var entityId in entityIds) |
|
|
|
{ |
|
|
|
if (Guid.TryParse(entityId, out var parsedEntityId)) |
|
|
|
{ |
|
|
|
entityIdFilters.Add(parsedEntityId); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return entityIdFilters; |
|
|
|
} |
|
|
|
|
|
|
|
public virtual async Task<bool> SlugExistsAsync(Guid blogId, [NotNull] string slug, |
|
|
|
CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
|