Created
March 19, 2017 16:10
-
-
Save fileman/e8dd95b3a58a80e652684731d87d4505 to your computer and use it in GitHub Desktop.
RESTier Soft Delete
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public partial class MyApi : EntityFrameworkApi<MyContext> | |
{ | |
//Other code | |
public static new IServiceCollection ConfigureApi(Type apiType, IServiceCollection services) | |
{ | |
return EntityFrameworkApi<MyContext>.ConfigureApi(apiType, services) | |
.AddService<ISubmitExecutor, SoftDeleteSubmitExecutor>(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SoftDeleteSubmitExecutor : ISubmitExecutor | |
{ | |
public async Task<SubmitResult> ExecuteSubmitAsync(SubmitContext context, CancellationToken cancellationToken) | |
{ | |
// EF SubmitExecutor Required | |
DbContext dbContext = context.GetApiService<DbContext>(); | |
// Soft Delete enable | |
// | |
// TODO Get deleted Entries from SubmitContext ChangeSet | |
//var deletedEntries = ((IList<DataModificationItem>)context.ChangeSet.Entries).Where(d => d.DataModificationItemAction == DataModificationItemAction.Remove); | |
//if (deletedEntries.Count() > 0) | |
//{ | |
//foreach (var entry in deletedEntries) | |
foreach (var entry in context.ChangeSet.Entries) | |
{ | |
var item = entry as DataModificationItem; | |
if (item != null && item.DataModificationItemAction == DataModificationItemAction.Remove) | |
{ | |
var entity = item.Resource; | |
// Try to get Entity Deleted property | |
var deleted = entity.GetType().GetProperties().Where(p => p.Name == "Deleted").FirstOrDefault(); | |
// If Entity has Deleted property set value to True and update EntityState to Modified instead of Deleted | |
if (deleted != null) | |
{ | |
deleted.SetValue(entity, true); | |
dbContext.Entry(entity).State = EntityState.Modified; | |
} | |
// Else nothing to do and delete it. | |
} | |
} | |
//} | |
// EF SubmitExecutor Required | |
await dbContext.SaveChangesAsync(cancellationToken); | |
// EF SubmitExecutor Required | |
return new SubmitResult(context.ChangeSet); | |
} | |
} |
OK, so I spent the day enhancing this code quite a bit. You can see it here. I've made it configurable with an HttpConfigurationExtension, so you can set it like this:
public static async void Register(HttpConfiguration config)
{
config.Select().Filter().Expand().OrderBy().MaxTop(100).Count().CheckSoftDeleteProperty("IsDeleted", "admin");
}
If you're OK with it, I'd like to publish this to NuGet as the start of a set of Restier Extensions that help make working with the framework easier. Please LMK what you think.
Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This looks great! I can see that you were trying to use a LINQ query to get back just the deleted items, instead of looping. I assume, because it's commented out, that you didn't get it to work. Would you mind providing some insight as to how you came to that conclusion?
Thanks!