feat: add strategy set CRUD operations (API + CLI)

This commit is contained in:
2026-04-03 08:37:04 +00:00
parent 908068d9aa
commit 1b0991c9cf
12 changed files with 510 additions and 5 deletions
+28
View File
@@ -385,6 +385,34 @@ class PostgresPortfolioRepository:
return None
return StrategySet(id=row[0], name=row[1], config=row[2], nick=row[3], created_at=row[4])
def list_strategy_sets(self) -> list[StrategySet]:
self.ensure_schema()
with self._connect().cursor() as cur:
cur.execute(
"SELECT id, name, config, nick, created_at FROM strategy_sets ORDER BY created_at DESC"
)
rows = cur.fetchall()
return [StrategySet(id=row[0], name=row[1], config=row[2], nick=row[3], created_at=row[4]) for row in rows]
def delete_strategy_set(self, id: str) -> None:
self.ensure_schema()
with self._connect().cursor() as cur:
# 1. Check if this strategy set is referenced by any portfolio
cur.execute(
"""
SELECT COUNT(*) FROM portfolios
WHERE active_strategy_id = %s
OR avabile_strategies @> ARRAY[%s]
""",
(id, id),
)
count = cur.fetchone()[0]
if count > 0:
raise ValueError(f"Strategy set is in use by {count} portfolio(s) and cannot be deleted")
# 2. If not in use, proceed with deletion
cur.execute("DELETE FROM strategy_sets WHERE id = %s", (id,))
def save(
self,
portfolio: Portfolio,