fengling-platform/Fengling.Platform.Infrastructure/RouteStore.cs
movingsam 6f1dbba4f0
All checks were successful
Publish Platform NuGet Packages / build (push) Successful in 24s
refactor(infrastructure): 将主键类型从 long 改为 string 并使用 Guid 生成唯一 ID
- 修改 GwServiceInstance 和 GwTenantRoute 的 Id 类型为 string
- 使用 Guid.CreateVersion7().ToString("N") 生成默认唯一标识值
- 更新 IInstanceStore、IRouteManager、IRouteStore 接口中的 FindByIdAsync 方法签名,使用 string? 替代 long?
- 调整 InstanceStore、RouteManager 和 RouteStore 中相应方法实现,支持新的 Id 类型
- 保证相关存储及查询接口兼容新的字符串形式主键
2026-03-01 11:23:12 +08:00

109 lines
3.9 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<GwTenantRoute> _routes;
public RouteStore(TContext context)
{
_context = context;
_routes = context.GwTenantRoutes;
}
public void Dispose() { }
public virtual Task<GwTenantRoute?> FindByIdAsync(string? id, CancellationToken cancellationToken = default)
{
if (id == null) return Task.FromResult<GwTenantRoute?>(null);
return _routes.FirstOrDefaultAsync(r => r.Id == id, cancellationToken);
}
public virtual Task<GwTenantRoute?> FindByTenantCodeAsync(string tenantCode, CancellationToken cancellationToken = default)
{
return _routes.FirstOrDefaultAsync(r => r.TenantCode == tenantCode && !r.IsDeleted, cancellationToken);
}
public virtual Task<GwTenantRoute?> FindByClusterIdAsync(string clusterId, CancellationToken cancellationToken = default)
{
return _routes.FirstOrDefaultAsync(r => r.ClusterId == clusterId && !r.IsDeleted, cancellationToken);
}
public virtual async Task<IList<GwTenantRoute>> GetAllAsync(CancellationToken cancellationToken = default)
{
return await _routes.Where(r => !r.IsDeleted).ToListAsync(cancellationToken);
}
public virtual async Task<IList<GwTenantRoute>> GetPagedAsync(int page, int pageSize, string? tenantCode = null,
string? serviceName = null, RouteStatus? status = null, CancellationToken cancellationToken = default)
{
var query = _routes.AsQueryable();
if (!string.IsNullOrEmpty(tenantCode))
query = query.Where(r => r.TenantCode.Contains(tenantCode));
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? tenantCode = null, string? serviceName = null,
RouteStatus? status = null, CancellationToken cancellationToken = default)
{
var query = _routes.AsQueryable();
if (!string.IsNullOrEmpty(tenantCode))
query = query.Where(r => r.TenantCode.Contains(tenantCode));
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(GwTenantRoute route, CancellationToken cancellationToken = default)
{
_routes.Add(route);
await _context.SaveChangesAsync(cancellationToken);
return IdentityResult.Success;
}
public virtual async Task<IdentityResult> UpdateAsync(GwTenantRoute route, CancellationToken cancellationToken = default)
{
route.UpdatedTime = DateTime.UtcNow;
_routes.Update(route);
await _context.SaveChangesAsync(cancellationToken);
return IdentityResult.Success;
}
public virtual async Task<IdentityResult> DeleteAsync(GwTenantRoute route, CancellationToken cancellationToken = default)
{
// 软删除
route.IsDeleted = true;
route.UpdatedTime = DateTime.UtcNow;
_routes.Update(route);
await _context.SaveChangesAsync(cancellationToken);
return IdentityResult.Success;
}
}