AssetManager.API/AssetManager.Data/DatabaseService.cs

52 lines
1.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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)
);
}
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();
}
}