fengling-platform/Fengling.Platform.Infrastructure/RouteStore.cs
Kimi CLI b66b231917
All checks were successful
Publish Platform NuGet Packages / build (push) Successful in 26s
refactor: replace GwTenantRoute with GwRoute, change Id type to string
- Remove GwTenantRoute (old tenant-specific route entity)
- Add GwRoute with string Id (Guid.CreateVersion7)
- Update IRouteManager and IRouteStore interfaces
- Update PlatformDbContext configuration for new schema
- GwRoute is now global, tenant-specific routing moved to GwDestination.TenantCode

BREAKING CHANGE: Database schema change requires table recreation
2026-03-08 15:21:43 +08:00

98 lines
3.3 KiB
C#

using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Fengling.Platform.Domain.AggregatesModel.GatewayAggregate;
namespace Fengling.Platform.Infrastructure;
/// <summary>
/// 路由存储实现
/// </summary>
public class RouteStore<TContext> : IRouteStore
where TContext : PlatformDbContext
{
private readonly TContext _context;
private readonly DbSet<GwRoute> _routes;
public RouteStore(TContext context)
{
_context = context;
_routes = context.GwRoutes;
}
public void Dispose() { }
public virtual Task<GwRoute?> FindByIdAsync(string? id, CancellationToken cancellationToken = default)
{
if (id == null) return Task.FromResult<GwRoute?>(null);
return _routes.FirstOrDefaultAsync(r => r.Id == id, cancellationToken);
}
public virtual Task<GwRoute?> FindByClusterIdAsync(string clusterId, CancellationToken cancellationToken = default)
{
return _routes.FirstOrDefaultAsync(r => r.ClusterId == clusterId && !r.IsDeleted, cancellationToken);
}
public virtual async Task<IList<GwRoute>> GetAllAsync(CancellationToken cancellationToken = default)
{
return await _routes.Where(r => !r.IsDeleted).ToListAsync(cancellationToken);
}
public virtual async Task<IList<GwRoute>> GetPagedAsync(int page, int pageSize,
string? serviceName = null, RouteStatus? status = null, CancellationToken cancellationToken = default)
{
var query = _routes.AsQueryable();
if (!string.IsNullOrEmpty(serviceName))
query = query.Where(r => r.ServiceName.Contains(serviceName));
if (status.HasValue)
query = query.Where(r => r.Status == (int)status.Value);
return await query
.Where(r => !r.IsDeleted)
.OrderByDescending(r => r.CreatedTime)
.Skip((page - 1) * pageSize)
.Take(pageSize)
.ToListAsync(cancellationToken);
}
public virtual async Task<int> GetCountAsync(string? serviceName = null,
RouteStatus? status = null, CancellationToken cancellationToken = default)
{
var query = _routes.AsQueryable();
if (!string.IsNullOrEmpty(serviceName))
query = query.Where(r => r.ServiceName.Contains(serviceName));
if (status.HasValue)
query = query.Where(r => r.Status == (int)status.Value);
return await query.Where(r => !r.IsDeleted).CountAsync(cancellationToken);
}
public virtual async Task<IdentityResult> CreateAsync(GwRoute route, CancellationToken cancellationToken = default)
{
_routes.Add(route);
await _context.SaveChangesAsync(cancellationToken);
return IdentityResult.Success;
}
public virtual async Task<IdentityResult> UpdateAsync(GwRoute route, CancellationToken cancellationToken = default)
{
route.UpdatedTime = DateTime.UtcNow;
_routes.Update(route);
await _context.SaveChangesAsync(cancellationToken);
return IdentityResult.Success;
}
public virtual async Task<IdentityResult> DeleteAsync(GwRoute route, CancellationToken cancellationToken = default)
{
// 软删除
route.IsDeleted = true;
route.UpdatedTime = DateTime.UtcNow;
_routes.Update(route);
await _context.SaveChangesAsync(cancellationToken);
return IdentityResult.Success;
}
}