79 lines
2.3 KiB
C#
79 lines
2.3 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,
|
|
Description = request.description,
|
|
Tags = request.tags != null ? string.Join(",", request.tags) : null,
|
|
RiskLevel = request.riskLevel,
|
|
Config = request.parameters != null ? System.Text.Json.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 ? System.Text.Json.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;
|
|
}
|
|
}
|