Skip to content

Commit

Permalink
Merge pull request #6 from stzoran1/restoreMethods
Browse files Browse the repository at this point in the history
Restore methods
  • Loading branch information
AshkanAbd authored Aug 7, 2020
2 parents 1238963 + 2613702 commit 3af48dc
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 3 deletions.
Binary file added .vs/SoftDeletes/v16/.suo
Binary file not shown.
116 changes: 114 additions & 2 deletions SoftDeletes/Core/DbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace SoftDeletes.Core
{
public class DbContext : Microsoft.EntityFrameworkCore.DbContext
{
protected bool allowRestore = false;
protected DbContext()
{
}
Expand Down Expand Up @@ -82,9 +83,8 @@ protected virtual void SetModifiedEntitiesTimestamps()
foreach (var record in updatedRecords) {
if (record == null) continue;

if(record is ISoftDelete)
if(record is ISoftDelete && allowRestore == false)
{
//TODO: Check how to remove SoftDelete attribute and how this might affect preventing reverting SoftDelete
this.Entry(record).Property("DeletedAt").IsModified = false;
}
if (record is ITimestamps)
Expand All @@ -97,6 +97,8 @@ protected virtual void SetModifiedEntitiesTimestamps()
}

}

allowRestore = false;
}

/// <summary>
Expand Down Expand Up @@ -573,5 +575,115 @@ public void ForceRemoveRange(IEnumerable<object> entities)
x.SetForceDelete();
}
}

/// <summary>
/// Allow restoring entity from soft delete.
/// </summary>
/// <remarks>
/// <para>
/// NOTE: This method will restore entities that implements soft deletes by setting DeletedAt to NULL.
/// </para>
/// <para>
/// NOTE: After calling this method SaveChanges will be called.
/// </para>
/// </remarks>
/// <param name="entity"> The entity to restore. </param>
/// <returns>
/// The <see cref="int" /> for the result of SaveChanges.
/// </returns>
public virtual int Restore([NotNull] ISoftDelete entity)
{
allowRestore = true;

entity.DeletedAt = null;

int result = base.SaveChanges();

return result;
}

/// <summary>
/// Allow restoring entity from soft delete.
/// </summary>
/// <remarks>
/// <para>
/// NOTE: This method will restore entities that implements soft deletes by setting DeletedAt to NULL.
/// </para>
/// <para>
/// NOTE: After calling this method SaveChangesAsync will be called.
/// </para>
/// </remarks>
/// <param name="entity"> The entity to restore. </param>
/// <returns>
/// The <see cref="int" /> for the result of SaveChangesAsync.
/// </returns>
public virtual async Task<int> RestoreAsync([NotNull] ISoftDelete entity)
{
allowRestore = true;

entity.DeletedAt = null;

var result = await base.SaveChangesAsync();

return result;
}

/// <summary>
/// Allow restoring range of entities from soft delete.
/// </summary>
/// <remarks>
/// <para>
/// NOTE: This method will restore entities that implements soft deletes by setting DeletedAt to NULL.
/// </para>
/// <para>
/// NOTE: After calling this method SaveChanges will be called.
/// </para>
/// </remarks>
/// <param name="entities"> The entities to restore. </param>
/// <returns>
/// The <see cref="int" /> for the result of SaveChanges.
/// </returns>
public virtual int RestoreRange(IEnumerable<ISoftDelete> entities)
{
allowRestore = true;

foreach (var entity in entities)
{
entity.DeletedAt = null;
}

int result = base.SaveChanges();

return result;
}

/// <summary>
/// Allow restoring range of entities from soft delete.
/// </summary>
/// <remarks>
/// <para>
/// NOTE: This method will restore entities that implements soft deletes by setting DeletedAt to NULL.
/// </para>
/// <para>
/// NOTE: After calling this method SaveChangesAsync will be called.
/// </para>
/// </remarks>
/// <param name="entities"> The entities to restore. </param>
/// <returns>
/// The <see cref="int" /> for the result of SaveChangesAsync.
/// </returns>
public virtual async Task<int> RestoreRangeAsync(IEnumerable<ISoftDelete> entities)
{
allowRestore = true;

foreach (var entity in entities)
{
entity.DeletedAt = null;
}

int result = await base.SaveChangesAsync();

return result;
}
}
}
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"sdk": {
"version": "3.1.300"
"version": "3.1.400"
}
}

0 comments on commit 3af48dc

Please sign in to comment.