|
|
|
@ -1,4 +1,5 @@ |
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Linq; |
|
|
|
using System.Linq.Expressions; |
|
|
|
using System.Threading; |
|
|
|
@ -6,6 +7,7 @@ using System.Threading.Tasks; |
|
|
|
using MongoDB.Driver; |
|
|
|
using Volo.Abp.Domain.Entities; |
|
|
|
using Volo.Abp.MongoDB; |
|
|
|
using Volo.Abp.MultiTenancy; |
|
|
|
|
|
|
|
namespace Volo.Abp.Domain.Repositories.MongoDB |
|
|
|
{ |
|
|
|
@ -74,7 +76,7 @@ namespace Volo.Abp.Domain.Repositories.MongoDB |
|
|
|
|
|
|
|
protected override IQueryable<TEntity> GetQueryable() |
|
|
|
{ |
|
|
|
return Collection.AsQueryable(); |
|
|
|
return ApplyDataFilters(Collection.AsQueryable()); |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual FilterDefinition<TEntity> CreateEntityFilter(TEntity entity) |
|
|
|
@ -117,24 +119,24 @@ namespace Volo.Abp.Domain.Repositories.MongoDB |
|
|
|
return entity; |
|
|
|
} |
|
|
|
|
|
|
|
public virtual void Delete(TKey id, bool autoSave = false) |
|
|
|
public virtual async Task<TEntity> FindAsync(TKey id, CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
Collection.DeleteOne(CreateEntityFilter(id)); |
|
|
|
return await Collection.Find(CreateEntityFilter(id, true)).FirstOrDefaultAsync(cancellationToken); |
|
|
|
} |
|
|
|
|
|
|
|
public virtual Task DeleteAsync(TKey id, bool autoSave = false, CancellationToken cancellationToken = default) |
|
|
|
public virtual TEntity Find(TKey id) |
|
|
|
{ |
|
|
|
return Collection.DeleteOneAsync(CreateEntityFilter(id), cancellationToken); |
|
|
|
return Collection.Find(CreateEntityFilter(id, true)).FirstOrDefault(); |
|
|
|
} |
|
|
|
|
|
|
|
public virtual async Task<TEntity> FindAsync(TKey id, CancellationToken cancellationToken = default) |
|
|
|
public virtual void Delete(TKey id, bool autoSave = false) |
|
|
|
{ |
|
|
|
return await Collection.Find(CreateEntityFilter(id)).FirstOrDefaultAsync(cancellationToken); |
|
|
|
Collection.DeleteOne(CreateEntityFilter(id)); |
|
|
|
} |
|
|
|
|
|
|
|
public virtual TEntity Find(TKey id) |
|
|
|
public virtual Task DeleteAsync(TKey id, bool autoSave = false, CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
return Collection.Find(CreateEntityFilter(id)).FirstOrDefault(); |
|
|
|
return Collection.DeleteOneAsync(CreateEntityFilter(id), cancellationToken); |
|
|
|
} |
|
|
|
|
|
|
|
protected override FilterDefinition<TEntity> CreateEntityFilter(TEntity entity) |
|
|
|
@ -142,9 +144,33 @@ namespace Volo.Abp.Domain.Repositories.MongoDB |
|
|
|
return Builders<TEntity>.Filter.Eq(e => e.Id, entity.Id); |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual FilterDefinition<TEntity> CreateEntityFilter(TKey id) |
|
|
|
protected virtual FilterDefinition<TEntity> CreateEntityFilter(TKey id, bool applyFilters = false) |
|
|
|
{ |
|
|
|
return Builders<TEntity>.Filter.Eq(e => e.Id, id); |
|
|
|
var filters = new List<FilterDefinition<TEntity>> |
|
|
|
{ |
|
|
|
Builders<TEntity>.Filter.Eq(e => e.Id, id) |
|
|
|
}; |
|
|
|
|
|
|
|
if (applyFilters) |
|
|
|
{ |
|
|
|
AddGlobalFilters(filters); |
|
|
|
} |
|
|
|
|
|
|
|
return Builders<TEntity>.Filter.And(filters); |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual void AddGlobalFilters(List<FilterDefinition<TEntity>> filters) |
|
|
|
{ |
|
|
|
if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)) && DataFilter.IsEnabled<ISoftDelete>()) |
|
|
|
{ |
|
|
|
filters.Add(Builders<TEntity>.Filter.Eq(e => ((ISoftDelete) e).IsDeleted, false)); |
|
|
|
} |
|
|
|
|
|
|
|
if (typeof(IMultiTenant).IsAssignableFrom(typeof(TEntity))) |
|
|
|
{ |
|
|
|
var tenantId = CurrentTenant.Id; |
|
|
|
filters.Add(Builders<TEntity>.Filter.Eq(e => ((IMultiTenant) e).TenantId, tenantId)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |