重构组合服务接口和实现,添加用户ID参数以实现多租户隔离 更新组合控制器,从JWT令牌中提取用户ID并验证权限 完善组合服务数据库操作,包括组合创建、查询和交易处理 更新README文档,补充组合API详细说明
73 lines
1.9 KiB
C#
73 lines
1.9 KiB
C#
using AssetManager.Data;
|
|
using AssetManager.Models.DTOs;
|
|
using SqlSugar;
|
|
|
|
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,
|
|
Config = System.Text.Json.JsonSerializer.Serialize(request.parameters),
|
|
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.Config = System.Text.Json.JsonSerializer.Serialize(request.parameters);
|
|
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;
|
|
}
|
|
}
|