feat: add strategy set CRUD operations (API + CLI)
This commit is contained in:
@@ -47,6 +47,10 @@ class PortfolioRepository(Protocol):
|
||||
|
||||
def get_strategy_set(self, id: str) -> StrategySet | None: ...
|
||||
|
||||
def list_strategy_sets(self) -> list[StrategySet]: ...
|
||||
|
||||
def delete_strategy_set(self, id: str) -> None: ...
|
||||
|
||||
def save(
|
||||
self,
|
||||
portfolio: Portfolio,
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user