Stage 05.6 - order draft logic improvements
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
# app/src/storage/repositories/order_drafts.py
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
@@ -34,7 +36,7 @@ class OrderDraftRepository:
|
||||
with connection.cursor() as cursor:
|
||||
cursor.execute(
|
||||
'''
|
||||
SELECT created_at, symbol, side, order_type, quantity::text, status
|
||||
SELECT id, created_at, symbol, side, order_type, quantity::text, status
|
||||
FROM order_drafts
|
||||
ORDER BY created_at DESC, id DESC
|
||||
LIMIT %s
|
||||
@@ -47,20 +49,63 @@ class OrderDraftRepository:
|
||||
for row in rows:
|
||||
items.append(
|
||||
{
|
||||
"created_at": str(row[0]),
|
||||
"symbol": str(row[1]),
|
||||
"side": str(row[2]),
|
||||
"order_type": str(row[3]),
|
||||
"quantity": str(row[4]),
|
||||
"status": str(row[5]),
|
||||
"id": str(row[0]),
|
||||
"created_at": str(row[1]),
|
||||
"symbol": str(row[2]),
|
||||
"side": str(row[3]),
|
||||
"order_type": str(row[4]),
|
||||
"quantity": str(row[5]),
|
||||
"status": str(row[6]),
|
||||
}
|
||||
)
|
||||
return items
|
||||
|
||||
def get_draft_by_id(self, draft_id: str) -> dict[str, str] | None:
|
||||
with get_connection() as connection:
|
||||
with connection.cursor() as cursor:
|
||||
cursor.execute(
|
||||
'''
|
||||
SELECT id, created_at, symbol, side, order_type, quantity::text, status, payload_json
|
||||
FROM order_drafts
|
||||
WHERE id = %s
|
||||
LIMIT 1
|
||||
''',
|
||||
(draft_id,),
|
||||
)
|
||||
row = cursor.fetchone()
|
||||
|
||||
if not row:
|
||||
return None
|
||||
|
||||
payload_raw = row[7]
|
||||
payload: dict[str, Any] = {}
|
||||
|
||||
if isinstance(payload_raw, dict):
|
||||
payload = payload_raw
|
||||
elif payload_raw:
|
||||
try:
|
||||
payload = json.loads(str(payload_raw))
|
||||
except Exception:
|
||||
payload = {}
|
||||
|
||||
price = payload.get("price")
|
||||
price_text = str(price) if price not in (None, "") else ""
|
||||
|
||||
return {
|
||||
"id": str(row[0]),
|
||||
"created_at": str(row[1]),
|
||||
"symbol": str(row[2]),
|
||||
"side": str(row[3]),
|
||||
"order_type": str(row[4]),
|
||||
"quantity": str(row[5]),
|
||||
"status": str(row[6]),
|
||||
"price": price_text,
|
||||
}
|
||||
|
||||
def count_drafts(self) -> int:
|
||||
with get_connection() as connection:
|
||||
with connection.cursor() as cursor:
|
||||
cursor.execute("SELECT COUNT(*) FROM order_drafts")
|
||||
row = cursor.fetchone()
|
||||
|
||||
return int(row[0]) if row else 0
|
||||
return int(row[0]) if row else 0
|
||||
Reference in New Issue
Block a user