Add 1-year BTC daily price history chart
New GET /history endpoint fetches 365 days of BTC/EUR data from CoinGecko, deduplicates by date, and joins the user's purchases. BTCHistoryChart component renders the price line with orange dot markers on purchase dates and a dashed cyan avg buy price line. Tooltip shows purchase details on marked dates. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
from fastapi import APIRouter, Depends
|
||||
from sqlalchemy.orm import Session
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from ..database import get_db
|
||||
from .. import models
|
||||
from ..dependencies import get_current_user
|
||||
from ..services.btc import get_btc_history_eur
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/history")
|
||||
def get_history(
|
||||
db: Session = Depends(get_db),
|
||||
current_user: models.User = Depends(get_current_user),
|
||||
):
|
||||
raw = get_btc_history_eur()
|
||||
|
||||
# Convert timestamps and deduplicate (keep last price per date)
|
||||
seen = {}
|
||||
for ts, price in raw:
|
||||
date = datetime.fromtimestamp(ts / 1000, tz=timezone.utc).strftime("%Y-%m-%d")
|
||||
seen[date] = round(price, 2)
|
||||
|
||||
prices = [{"date": d, "price": p} for d, p in seen.items()]
|
||||
|
||||
purchases_db = db.query(models.Purchase).filter(
|
||||
models.Purchase.user_id == current_user.id
|
||||
).all()
|
||||
|
||||
purchases = [
|
||||
{
|
||||
"date": p.created_at.strftime("%Y-%m-%d"),
|
||||
"amount_eur": round(p.amount_eur, 2),
|
||||
"price_eur": round(p.price_eur, 2),
|
||||
}
|
||||
for p in purchases_db
|
||||
]
|
||||
|
||||
return {"prices": prices, "purchases": purchases}
|
||||
Reference in New Issue
Block a user