From 05e7ffdfd9edbd71816240e2d4a71d1ff549e961 Mon Sep 17 00:00:00 2001 From: gahusb Date: Thu, 19 Mar 2026 23:02:11 +0900 Subject: [PATCH] =?UTF-8?q?=EB=A7=A4=EB=8F=84=20=ED=9E=88=EC=8A=A4?= =?UTF-8?q?=ED=86=A0=EB=A6=AC=20=EC=88=98=EC=A0=95=20API=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20(PUT=20/api/portfolio/sell-history/:id)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 4.6 --- stock-lab/app/db.py | 13 +++++++++++++ stock-lab/app/main.py | 11 ++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/stock-lab/app/db.py b/stock-lab/app/db.py index e9588fa..78701e2 100644 --- a/stock-lab/app/db.py +++ b/stock-lab/app/db.py @@ -242,6 +242,19 @@ def get_sell_history(broker: str = None, days: int = None) -> List[Dict[str, Any return [dict(r) for r in rows] +def update_sell_history(record_id: int, data: Dict[str, Any]) -> Dict[str, Any] | None: + fields = ["broker", "ticker", "name", "quantity", "avg_price", "sell_price", + "buy_amount", "sell_amount", "realized_profit", "realized_rate", "sold_at"] + set_clause = ", ".join(f"{f} = ?" for f in fields) + values = [data[f] for f in fields] + [record_id] + with _conn() as conn: + cur = conn.execute(f"UPDATE sell_history SET {set_clause} WHERE id = ?", values) + if cur.rowcount == 0: + return None + row = conn.execute("SELECT * FROM sell_history WHERE id = ?", (record_id,)).fetchone() + return dict(row) + + def delete_sell_history(record_id: int) -> bool: with _conn() as conn: cur = conn.execute("DELETE FROM sell_history WHERE id = ?", (record_id,)) diff --git a/stock-lab/app/main.py b/stock-lab/app/main.py index 485c1a0..5bd3653 100644 --- a/stock-lab/app/main.py +++ b/stock-lab/app/main.py @@ -15,7 +15,7 @@ from .db import ( update_portfolio_item, delete_portfolio_item, upsert_broker_cash, get_all_broker_cash, get_broker_cash, delete_broker_cash, upsert_asset_snapshot, get_asset_snapshots, - add_sell_history, get_sell_history, delete_sell_history, + add_sell_history, get_sell_history, update_sell_history, delete_sell_history, ) from .scraper import fetch_market_news, fetch_major_indices, fetch_overseas_news from .price_fetcher import get_current_prices @@ -392,6 +392,15 @@ def create_sell_history(req: SellHistoryRequest): return record +@app.put("/api/portfolio/sell-history/{record_id}") +def modify_sell_history(record_id: int, req: SellHistoryRequest): + """매도 기록 수정""" + record = update_sell_history(record_id, req.model_dump()) + if record is None: + return JSONResponse(status_code=404, content={"error": "not found"}) + return record + + @app.delete("/api/portfolio/sell-history/{record_id}") def remove_sell_history(record_id: int): """매도 기록 삭제"""