From b63accc8b8d20b96c7e4c905fa2406e5370c25ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Sat, 14 Apr 2018 13:40:53 +0300 Subject: [PATCH] mongodb rollback soft delete on delete. --- .../Repositories/MongoDB/MongoDbRepository.cs | 43 ++++++++++++++----- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs b/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs index 5571fd71b6..2acee959e5 100644 --- a/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs +++ b/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs @@ -107,9 +107,20 @@ namespace Volo.Abp.Domain.Repositories.MongoDB { ApplyAbpConceptsForDeletedEntity(entity); - Collection.DeleteOne( - CreateEntityFilter(entity) - ); + if (entity is ISoftDelete softDeleteEntity) + { + softDeleteEntity.IsDeleted = true; + Collection.ReplaceOne( + CreateEntityFilter(entity), + entity + ); + } + else + { + Collection.DeleteOne( + CreateEntityFilter(entity) + ); + } } public override async Task DeleteAsync( @@ -119,10 +130,22 @@ namespace Volo.Abp.Domain.Repositories.MongoDB { ApplyAbpConceptsForDeletedEntity(entity); - await Collection.DeleteOneAsync( - CreateEntityFilter(entity), - GetCancellationToken(cancellationToken) - ); + if (entity is ISoftDelete softDeleteEntity) + { + softDeleteEntity.IsDeleted = true; + await Collection.ReplaceOneAsync( + CreateEntityFilter(entity), + entity, + cancellationToken: GetCancellationToken(cancellationToken) + ); + } + else + { + await Collection.DeleteOneAsync( + CreateEntityFilter(entity), + GetCancellationToken(cancellationToken) + ); + } } public override void Delete(Expression> predicate, bool autoSave = false) @@ -169,8 +192,7 @@ namespace Volo.Abp.Domain.Repositories.MongoDB protected virtual FilterDefinition CreateEntityFilter(TEntity entity) { throw new NotImplementedException( - $"{nameof(CreateEntityFilter)} is not implemented for MongoDB by default. " + - $"It should be overrided and implemented by the deriving class!" + $"{nameof(CreateEntityFilter)} is not implemented for MongoDB by default. It should be overrided and implemented by the deriving class!" ); } @@ -222,7 +244,8 @@ namespace Volo.Abp.Domain.Repositories.MongoDB AuditPropertySetter.SetDeletionProperties(entity); } - protected virtual void TriggerDomainEvents(object entity) //TODO: TriggerDomainEventsAsync..? + //TODO: TriggerDomainEventsAsync..? + protected virtual void TriggerDomainEvents(object entity) { var generatesDomainEventsEntity = entity as IGeneratesDomainEvents; if (generatesDomainEventsEntity == null)