Fix bug where store was not properly deleted

This commit is contained in:
nicolas.dorier
2018-07-19 22:46:55 +09:00
parent 3f48a478af
commit 883cd41232
2 changed files with 23 additions and 5 deletions

View File

@@ -114,17 +114,34 @@ namespace BTCPayServer.Services.Stores
public async Task RemoveStoreUser(string storeId, string userId)
{
bool delete = false;
using (var ctx = _ContextFactory.CreateContext())
{
var userStore = new UserStore() { StoreDataId = storeId, ApplicationUserId = userId };
ctx.UserStore.Add(userStore);
ctx.Entry<UserStore>(userStore).State = EntityState.Deleted;
await ctx.SaveChangesAsync();
delete = await ctx.UserStore.Where(u => u.StoreDataId == storeId && u.Role == StoreRoles.Owner).CountAsync() == 0;
}
await DeleteStoreIfOrphan(storeId);
}
private async Task DeleteStoreIfOrphan(string storeId)
{
using (var ctx = _ContextFactory.CreateContext())
{
if (ctx.Database.SupportDropForeignKey())
{
if (await ctx.UserStore.Where(u => u.StoreDataId == storeId && u.Role == StoreRoles.Owner).CountAsync() == 0)
{
var store = await ctx.Stores.FindAsync(storeId);
if (store != null)
{
ctx.Stores.Remove(store);
await ctx.SaveChangesAsync();
}
}
}
}
if (delete)
await DeleteStore(storeId);
}
public async Task<StoreData> CreateStore(string ownerId, string name)
@@ -162,6 +179,7 @@ namespace BTCPayServer.Services.Stores
ctx.UserStore.Remove(storeUser);
await ctx.SaveChangesAsync();
}
await DeleteStoreIfOrphan(storeId);
}
public async Task UpdateStore(StoreData store)