fengling-platform/Fengling.Platform.Infrastructure/ClusterStore.cs
movingsam a6558137af feat(03-gateway-infrastructure-update): update Infrastructure layer for GwCluster
- Updated PlatformDbContext: removed GwTenant/GwServiceInstance DbSets, added GwCluster with EF Core config
- Created IClusterStore interface with CRUD and Destination management methods
- Created ClusterStore<TContext> implementation with soft delete and embedded Destinations support
- Deleted obsolete IInstanceStore and InstanceStore (replaced by IClusterStore)
- Updated Extensions.cs and GatewayExtensions.cs to register IClusterStore

Plan 03 of Phase 03 complete.
2026-03-03 15:46:57 +08:00

154 lines
5.8 KiB
C#

using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Fengling.Platform.Domain.AggregatesModel.GatewayAggregate;
namespace Fengling.Platform.Infrastructure;
/// <summary>
/// 集群存储实现
/// </summary>
public class ClusterStore<TContext> : IClusterStore
where TContext : PlatformDbContext
{
private readonly TContext _context;
private readonly DbSet<GwCluster> _clusters;
public ClusterStore(TContext context)
{
_context = context;
_clusters = context.GwClusters;
}
public void Dispose() { }
public virtual Task<GwCluster?> FindByIdAsync(string? id, CancellationToken cancellationToken = default)
{
if (id == null) return Task.FromResult<GwCluster?>(null);
return _clusters.FirstOrDefaultAsync(c => c.Id == id, cancellationToken);
}
public virtual Task<GwCluster?> FindByClusterIdAsync(string clusterId, CancellationToken cancellationToken = default)
{
return _clusters.FirstOrDefaultAsync(c => c.ClusterId == clusterId && !c.IsDeleted, cancellationToken);
}
public virtual async Task<IList<GwCluster>> GetAllAsync(CancellationToken cancellationToken = default)
{
return await _clusters.Where(c => !c.IsDeleted).ToListAsync(cancellationToken);
}
public virtual async Task<IList<GwCluster>> GetPagedAsync(int page, int pageSize, string? clusterId = null,
string? name = null, int? status = null, CancellationToken cancellationToken = default)
{
var query = _clusters.AsQueryable();
if (!string.IsNullOrEmpty(clusterId))
query = query.Where(c => c.ClusterId.Contains(clusterId));
if (!string.IsNullOrEmpty(name))
query = query.Where(c => c.Name.Contains(name));
if (status.HasValue)
query = query.Where(c => c.Status == status.Value);
return await query
.Where(c => !c.IsDeleted)
.OrderByDescending(c => c.CreatedTime)
.Skip((page - 1) * pageSize)
.Take(pageSize)
.ToListAsync(cancellationToken);
}
public virtual async Task<int> GetCountAsync(string? clusterId = null, string? name = null,
int? status = null, CancellationToken cancellationToken = default)
{
var query = _clusters.AsQueryable();
if (!string.IsNullOrEmpty(clusterId))
query = query.Where(c => c.ClusterId.Contains(clusterId));
if (!string.IsNullOrEmpty(name))
query = query.Where(c => c.Name.Contains(name));
if (status.HasValue)
query = query.Where(c => c.Status == status.Value);
return await query.Where(c => !c.IsDeleted).CountAsync(cancellationToken);
}
public virtual async Task<IdentityResult> CreateAsync(GwCluster cluster, CancellationToken cancellationToken = default)
{
_clusters.Add(cluster);
await _context.SaveChangesAsync(cancellationToken);
return IdentityResult.Success;
}
public virtual async Task<IdentityResult> UpdateAsync(GwCluster cluster, CancellationToken cancellationToken = default)
{
cluster.UpdatedTime = DateTime.UtcNow;
_clusters.Update(cluster);
await _context.SaveChangesAsync(cancellationToken);
return IdentityResult.Success;
}
public virtual async Task<IdentityResult> DeleteAsync(GwCluster cluster, CancellationToken cancellationToken = default)
{
// 软删除
cluster.IsDeleted = true;
cluster.UpdatedTime = DateTime.UtcNow;
_clusters.Update(cluster);
await _context.SaveChangesAsync(cancellationToken);
return IdentityResult.Success;
}
public virtual async Task<GwCluster?> AddDestinationAsync(string clusterId, GwDestination destination, CancellationToken cancellationToken = default)
{
var cluster = await _clusters.FirstOrDefaultAsync(c => c.ClusterId == clusterId && !c.IsDeleted, cancellationToken);
if (cluster == null) return null;
cluster.Destinations.Add(destination);
cluster.UpdatedTime = DateTime.UtcNow;
await _context.SaveChangesAsync(cancellationToken);
return cluster;
}
public virtual async Task<GwCluster?> UpdateDestinationAsync(string clusterId, string destinationId, GwDestination destination, CancellationToken cancellationToken = default)
{
var cluster = await _clusters
.Include(c => c.Destinations)
.FirstOrDefaultAsync(c => c.ClusterId == clusterId && !c.IsDeleted, cancellationToken);
if (cluster == null) return null;
var existingDest = cluster.Destinations.FirstOrDefault(d => d.DestinationId == destinationId);
if (existingDest == null) return null;
existingDest.Address = destination.Address;
existingDest.Health = destination.Health;
existingDest.Weight = destination.Weight;
existingDest.HealthStatus = destination.HealthStatus;
existingDest.Status = destination.Status;
cluster.UpdatedTime = DateTime.UtcNow;
await _context.SaveChangesAsync(cancellationToken);
return cluster;
}
public virtual async Task<GwCluster?> RemoveDestinationAsync(string clusterId, string destinationId, CancellationToken cancellationToken = default)
{
var cluster = await _clusters
.Include(c => c.Destinations)
.FirstOrDefaultAsync(c => c.ClusterId == clusterId && !c.IsDeleted, cancellationToken);
if (cluster == null) return null;
var destination = cluster.Destinations.FirstOrDefault(d => d.DestinationId == destinationId);
if (destination == null) return null;
cluster.Destinations.Remove(destination);
cluster.UpdatedTime = DateTime.UtcNow;
await _context.SaveChangesAsync(cancellationToken);
return cluster;
}
}