Add full-stack BTC portfolio web app
Multi-user FastAPI + React app with JWT auth, SQLite storage, and CoinGecko price integration. Dockerized with docker-compose. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
from fastapi import APIRouter, Depends
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..database import get_db
|
||||
from .. import models
|
||||
from ..dependencies import get_current_user
|
||||
from ..services.btc import get_btc_price_eur
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/stats")
|
||||
def get_stats(
|
||||
db: Session = Depends(get_db),
|
||||
current_user: models.User = Depends(get_current_user),
|
||||
):
|
||||
purchases = db.query(models.Purchase).filter(models.Purchase.user_id == current_user.id).all()
|
||||
|
||||
total_invested = sum(p.amount_eur for p in purchases)
|
||||
total_btc = sum(p.amount_eur / p.price_eur for p in purchases) if purchases else 0.0
|
||||
average_price = total_invested / total_btc if total_btc > 0 else 0.0
|
||||
current_price = get_btc_price_eur()
|
||||
portfolio_value = total_btc * current_price
|
||||
profit_loss = portfolio_value - total_invested
|
||||
|
||||
return {
|
||||
"total_invested": round(total_invested, 2),
|
||||
"total_btc": round(total_btc, 8),
|
||||
"average_price": round(average_price, 2),
|
||||
"current_price": round(current_price, 2),
|
||||
"portfolio_value": round(portfolio_value, 2),
|
||||
"profit_loss": round(profit_loss, 2),
|
||||
}
|
||||
Reference in New Issue
Block a user