- DatabaseService.InitializeDatabase() 添加 PortfolioNavHistory - 删除 migrations/ 目录,使用 CodeFirst 自动建表
53 lines
1.2 KiB
C#
Executable File
53 lines
1.2 KiB
C#
Executable File
using SqlSugar;
|
||
|
||
namespace AssetManager.Data;
|
||
|
||
public class DatabaseService
|
||
{
|
||
private readonly ISqlSugarClient _db;
|
||
|
||
public DatabaseService(ISqlSugarClient db)
|
||
{
|
||
_db = db;
|
||
}
|
||
|
||
public void InitializeDatabase()
|
||
{
|
||
_db.CodeFirst.InitTables(
|
||
typeof(User),
|
||
typeof(Strategy),
|
||
typeof(Portfolio),
|
||
typeof(Position),
|
||
typeof(Transaction),
|
||
typeof(TiingoTicker),
|
||
typeof(MarketPriceCache),
|
||
typeof(MarketKlineCache),
|
||
typeof(PortfolioNavHistory)
|
||
);
|
||
}
|
||
|
||
public ISqlSugarClient GetDb()
|
||
{
|
||
return _db;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据ID获取投资组合(校验用户ID)
|
||
/// </summary>
|
||
public Portfolio? GetPortfolioById(string id, string userId)
|
||
{
|
||
return _db.Queryable<Portfolio>()
|
||
.First(p => p.Id == id && p.UserId == userId);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据投资组合ID获取持仓列表
|
||
/// </summary>
|
||
public List<Position> GetPositionsByPortfolioId(string portfolioId)
|
||
{
|
||
return _db.Queryable<Position>()
|
||
.Where(p => p.PortfolioId == portfolioId)
|
||
.ToList();
|
||
}
|
||
}
|