public async Task <bool> Update(Cluster cluster) { var dbCluster = await DbContext.Set <Cluster>() .Include(c => c.Metadata) .Include(c => c.Destinations) .Include(c => c.SessionAffinity).ThenInclude(s => s.Settings) .Include(c => c.HttpRequest) .Include(c => c.HttpClient) .Include(c => c.HealthCheck).ThenInclude(h => h.Active) .Include(c => c.HealthCheck).ThenInclude(h => h.Passive) .FirstAsync(c => c.Id == cluster.Id); using (var tran = DbContext.Database.BeginTransaction()) { try { if (dbCluster.HealthCheck != null) { DbContext.Remove(dbCluster.HealthCheck); } if (dbCluster.Destinations != null) { DbContext.RemoveRange(dbCluster.Destinations); } if (dbCluster.HttpClient != null) { DbContext.Remove(dbCluster.HttpClient); } if (dbCluster.HttpRequest != null) { DbContext.Remove(dbCluster.HttpRequest); } if (dbCluster.SessionAffinity != null) { DbContext.Remove(dbCluster.SessionAffinity); } if (dbCluster.Metadata != null) { DbContext.RemoveRange(dbCluster.Metadata); } await DbContext.SaveChangesAsync(); if (cluster.HealthCheck != null) { cluster.HealthCheck.Id = 0; dbCluster.HealthCheck = cluster.HealthCheck; } if (cluster.Destinations != null) { cluster.Destinations.ForEach(d => d.Id = 0); dbCluster.Destinations = cluster.Destinations; } if (cluster.HttpClient != null) { cluster.HttpClient.Id = 0; dbCluster.HttpClient = cluster.HttpClient; } if (cluster.HttpRequest != null) { cluster.HttpRequest.Id = 0; dbCluster.HttpRequest = cluster.HttpRequest; } if (cluster.SessionAffinity != null) { cluster.SessionAffinity.Id = 0; dbCluster.SessionAffinity = cluster.SessionAffinity; } if (cluster.Metadata != null) { cluster.Metadata.ForEach(d => d.Id = 0); dbCluster.Metadata = cluster.Metadata; } dbCluster.LoadBalancingPolicy = cluster.LoadBalancingPolicy; DbContext.Update(dbCluster); await DbContext.SaveChangesAsync(); await tran.CommitAsync(); _logger.LogInformation($"Update Cluster: {cluster.Id} Success."); ReloadConfig(); return(true); } catch (Exception ex) { await tran.RollbackAsync(); _logger.LogError(ex, ex.Message); return(false); } } }
public async Task <bool> Update(ProxyRoute proxyRoute) { var dbRoute = await DbContext.Set <ProxyRoute>() .Include(r => r.Match).ThenInclude(m => m.Headers) .Include(r => r.Metadata) .Include(r => r.Transforms) .FirstAsync(r => r.Id == proxyRoute.Id); using (var tran = DbContext.Database.BeginTransaction()) { try { if (dbRoute.Match != null) { DbContext.Remove(dbRoute.Match); } if (dbRoute.Transforms != null) { DbContext.RemoveRange(dbRoute.Transforms); } if (dbRoute.Metadata != null) { DbContext.RemoveRange(dbRoute.Metadata); } await DbContext.SaveChangesAsync(); if (proxyRoute.Match != null) { proxyRoute.Match.Id = 0; if (proxyRoute.Match.Headers != null) { proxyRoute.Match.Headers.ForEach(d => d.Id = 0); } dbRoute.Match = proxyRoute.Match; } if (proxyRoute.Transforms != null) { proxyRoute.Transforms.ForEach(d => d.Id = 0); dbRoute.Transforms = proxyRoute.Transforms; } if (proxyRoute.Metadata != null) { proxyRoute.Metadata.ForEach(d => d.Id = 0); dbRoute.Metadata = proxyRoute.Metadata; } dbRoute.RouteId = proxyRoute.RouteId; dbRoute.ClusterId = proxyRoute.ClusterId; dbRoute.Order = proxyRoute.Order; dbRoute.AuthorizationPolicy = proxyRoute.AuthorizationPolicy; dbRoute.CorsPolicy = proxyRoute.CorsPolicy; DbContext.Update(dbRoute); await DbContext.SaveChangesAsync(); await tran.CommitAsync(); _logger.LogInformation($"Update ProxyRoute: {proxyRoute.RouteId} Success."); ReloadConfig(); return(true); } catch (Exception ex) { await tran.RollbackAsync(); _logger.LogError(ex, ex.Message); return(false); } } }