Initial commit - MIDAS App educação financeira para apostadores (FastAPI + Next.js)
This commit is contained in:
59
backend/app/routers/bankroll.py
Normal file
59
backend/app/routers/bankroll.py
Normal file
@@ -0,0 +1,59 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional
|
||||
from app.database import get_db
|
||||
from app.models.user import User
|
||||
from app.models.bankroll import Bankroll
|
||||
from app.utils.auth import get_current_user
|
||||
|
||||
router = APIRouter(prefix="/api/bankroll", tags=["bankroll"])
|
||||
|
||||
class BankrollUpdate(BaseModel):
|
||||
monthly_budget: Optional[float] = None
|
||||
weekly_limit: Optional[float] = None
|
||||
daily_limit: Optional[float] = None
|
||||
bet_max_pct: Optional[float] = None
|
||||
|
||||
@router.get("")
|
||||
async def get_bankroll(user: User = Depends(get_current_user), db: AsyncSession = Depends(get_db)):
|
||||
br = (await db.execute(select(Bankroll).where(Bankroll.user_id == user.id))).scalar_one_or_none()
|
||||
if not br:
|
||||
raise HTTPException(404, "Bankroll not configured")
|
||||
return {
|
||||
"monthly_budget": float(br.monthly_budget), "weekly_limit": float(br.weekly_limit or 0),
|
||||
"daily_limit": float(br.daily_limit or 0), "bet_max_pct": float(br.bet_max_pct or 5),
|
||||
"month_spent": float(br.month_spent or 0), "week_spent": float(br.week_spent or 0),
|
||||
"day_spent": float(br.day_spent or 0), "current_balance": float(br.current_balance or 0)
|
||||
}
|
||||
|
||||
@router.put("")
|
||||
async def update_bankroll(req: BankrollUpdate, user: User = Depends(get_current_user), db: AsyncSession = Depends(get_db)):
|
||||
br = (await db.execute(select(Bankroll).where(Bankroll.user_id == user.id))).scalar_one_or_none()
|
||||
if not br:
|
||||
raise HTTPException(404, "Bankroll not configured")
|
||||
if req.monthly_budget is not None: br.monthly_budget = req.monthly_budget
|
||||
if req.weekly_limit is not None: br.weekly_limit = req.weekly_limit
|
||||
if req.daily_limit is not None: br.daily_limit = req.daily_limit
|
||||
if req.bet_max_pct is not None: br.bet_max_pct = req.bet_max_pct
|
||||
await db.commit()
|
||||
return {"status": "updated"}
|
||||
|
||||
@router.get("/check")
|
||||
async def check_bankroll(amount: float = Query(...), user: User = Depends(get_current_user), db: AsyncSession = Depends(get_db)):
|
||||
br = (await db.execute(select(Bankroll).where(Bankroll.user_id == user.id))).scalar_one_or_none()
|
||||
if not br:
|
||||
return {"allowed": True, "warnings": []}
|
||||
warnings = []
|
||||
monthly_remaining = float(br.monthly_budget) - float(br.month_spent or 0)
|
||||
if amount > monthly_remaining:
|
||||
warnings.append(f"Excede limite mensal (restam R${monthly_remaining:.2f})")
|
||||
if br.weekly_limit and amount > (float(br.weekly_limit) - float(br.week_spent or 0)):
|
||||
warnings.append("Excede limite semanal")
|
||||
if br.daily_limit and amount > (float(br.daily_limit) - float(br.day_spent or 0)):
|
||||
warnings.append("Excede limite diário")
|
||||
max_bet = float(br.monthly_budget) * float(br.bet_max_pct or 5) / 100
|
||||
if amount > max_bet:
|
||||
warnings.append(f"Excede % máximo por aposta (max R${max_bet:.2f})")
|
||||
return {"allowed": len(warnings) == 0, "warnings": warnings}
|
||||
Reference in New Issue
Block a user