P0 - 安全修复: - 移除硬编码 API Key,启动时校验必填环境变量 P1 - 高优先级: - Entity 拆分:Position.cs, Transaction.cs 独立文件 - Controller Facade 封装:IPortfolioFacade 减少依赖注入 P2 - 中优先级: - Repository 抽象:IPortfolioRepository, IMarketDataRepository - MarketDataService 拆分:组合模式整合 Tencent/Tiingo/OKX P3 - 低优先级: - DTO 命名规范:统一 PascalCase - 单元测试框架:xUnit + Moq + FluentAssertions
80 lines
2.3 KiB
C#
Executable File
80 lines
2.3 KiB
C#
Executable File
using AssetManager.Data;
|
|
using AssetManager.Models.DTOs;
|
|
using SqlSugar;
|
|
using System.Text.Json;
|
|
|
|
namespace AssetManager.Services;
|
|
|
|
public class StrategyService : IStrategyService
|
|
{
|
|
private readonly ISqlSugarClient _db;
|
|
|
|
public StrategyService(ISqlSugarClient db)
|
|
{
|
|
_db = db;
|
|
}
|
|
|
|
public Strategy CreateStrategy(CreateStrategyRequest request, string userId)
|
|
{
|
|
var strategy = new Strategy
|
|
{
|
|
Id = Guid.NewGuid().ToString(),
|
|
UserId = userId,
|
|
Alias = request.Name,
|
|
Type = request.Type,
|
|
Description = request.Description,
|
|
Tags = request.Tags != null ? string.Join(",", request.Tags) : null,
|
|
RiskLevel = request.RiskLevel,
|
|
Config = request.Parameters != null ? JsonSerializer.Serialize(request.Parameters) : null,
|
|
CreatedAt = DateTime.Now,
|
|
UpdatedAt = DateTime.Now
|
|
};
|
|
|
|
_db.Insertable(strategy).ExecuteCommand();
|
|
return strategy;
|
|
}
|
|
|
|
public List<Strategy> GetStrategies(string userId)
|
|
{
|
|
return _db.Queryable<Strategy>()
|
|
.Where(it => it.UserId == userId)
|
|
.ToList();
|
|
}
|
|
|
|
public Strategy GetStrategyById(string id, string userId)
|
|
{
|
|
var strategy = _db.Queryable<Strategy>()
|
|
.Where(it => it.Id == id && it.UserId == userId)
|
|
.First();
|
|
|
|
if (strategy == null)
|
|
{
|
|
throw new Exception("Strategy not found or access denied");
|
|
}
|
|
|
|
return strategy;
|
|
}
|
|
|
|
public Strategy UpdateStrategy(string id, UpdateStrategyRequest request, string userId)
|
|
{
|
|
var strategy = GetStrategyById(id, userId);
|
|
|
|
strategy.Alias = request.Name;
|
|
strategy.Type = request.Type;
|
|
strategy.Description = request.Description;
|
|
strategy.Tags = request.Tags != null ? string.Join(",", request.Tags) : null;
|
|
strategy.RiskLevel = request.RiskLevel;
|
|
strategy.Config = request.Parameters != null ? JsonSerializer.Serialize(request.Parameters) : null;
|
|
strategy.UpdatedAt = DateTime.Now;
|
|
|
|
_db.Updateable(strategy).ExecuteCommand();
|
|
return strategy;
|
|
}
|
|
|
|
public bool DeleteStrategy(string id, string userId)
|
|
{
|
|
var strategy = GetStrategyById(id, userId);
|
|
return _db.Deleteable(strategy).ExecuteCommand() > 0;
|
|
}
|
|
}
|