
- Set up project structure with FastAPI and SQLite - Implement user authentication with JWT - Create database models for users, events, bets, and transactions - Add API endpoints for user management - Add API endpoints for events and betting functionality - Add wallet management for deposits and withdrawals - Configure Alembic for database migrations - Add linting with Ruff - Add documentation in README
91 lines
2.5 KiB
Python
91 lines
2.5 KiB
Python
from typing import Any, Optional
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app import crud, models, schemas
|
|
from app.api import deps
|
|
from app.models.transaction import TransactionType
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/balance", response_model=float)
|
|
def get_balance(
|
|
current_user: models.User = Depends(deps.get_current_active_user),
|
|
) -> float:
|
|
"""
|
|
Get current user's balance.
|
|
"""
|
|
return current_user.balance
|
|
|
|
|
|
@router.get("/transactions", response_model=list[schemas.Transaction])
|
|
def read_transactions(
|
|
db: Session = Depends(deps.get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
transaction_type: Optional[TransactionType] = None,
|
|
current_user: models.User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Retrieve transactions for current user.
|
|
"""
|
|
transactions = crud.get_user_transactions(
|
|
db, user_id=current_user.id, skip=skip, limit=limit,
|
|
transaction_type=transaction_type,
|
|
)
|
|
return transactions
|
|
|
|
|
|
@router.post("/deposit", response_model=schemas.Transaction)
|
|
def deposit_funds(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
transaction_in: schemas.TransactionCreate,
|
|
current_user: models.User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Deposit funds into user's account.
|
|
"""
|
|
if transaction_in.amount <= 0:
|
|
raise HTTPException(
|
|
status_code=400, detail="Deposit amount must be greater than 0",
|
|
)
|
|
|
|
transaction = crud.create_deposit(
|
|
db, user_id=current_user.id, transaction_in=transaction_in,
|
|
)
|
|
return transaction
|
|
|
|
|
|
@router.post("/withdraw", response_model=schemas.Transaction)
|
|
def withdraw_funds(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
transaction_in: schemas.TransactionCreate,
|
|
current_user: models.User = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Withdraw funds from user's account.
|
|
"""
|
|
if transaction_in.amount <= 0:
|
|
raise HTTPException(
|
|
status_code=400, detail="Withdrawal amount must be greater than 0",
|
|
)
|
|
|
|
if current_user.balance < transaction_in.amount:
|
|
raise HTTPException(
|
|
status_code=400, detail="Insufficient balance for this withdrawal",
|
|
)
|
|
|
|
transaction = crud.create_withdrawal(
|
|
db, user_id=current_user.id, transaction_in=transaction_in,
|
|
)
|
|
|
|
if not transaction:
|
|
raise HTTPException(
|
|
status_code=400, detail="Could not process withdrawal",
|
|
)
|
|
|
|
return transaction |