from fastapi import APIRouter, Depends, HTTPException, status, Query from sqlalchemy.orm import Session from typing import List from app.schemas.transaction import TransactionCreate, TransactionResponse, FiatTransactionCreate, FiatTransactionResponse from app.services.transaction import TransactionService from app.services.auth import get_current_user from app.db.session import get_db from app.models.user import User router = APIRouter(prefix="/transactions", tags=["Transactions"]) @router.post("/crypto/send", response_model=TransactionResponse) def send_crypto( transaction_data: TransactionCreate, current_user: User = Depends(get_current_user), db: Session = Depends(get_db) ): transaction_service = TransactionService(db) transaction = transaction_service.send_crypto(current_user, transaction_data) return transaction @router.post("/fiat/deposit", response_model=FiatTransactionResponse) def deposit_fiat( transaction_data: FiatTransactionCreate, current_user: User = Depends(get_current_user), db: Session = Depends(get_db) ): transaction_service = TransactionService(db) transaction = transaction_service.deposit_fiat(current_user, transaction_data) return transaction @router.post("/fiat/withdraw", response_model=FiatTransactionResponse) def withdraw_fiat( transaction_data: FiatTransactionCreate, current_user: User = Depends(get_current_user), db: Session = Depends(get_db) ): transaction_service = TransactionService(db) transaction = transaction_service.withdraw_fiat(current_user, transaction_data) return transaction @router.get("/crypto", response_model=List[TransactionResponse]) def get_crypto_transactions( limit: int = Query(50, ge=1, le=100), offset: int = Query(0, ge=0), current_user: User = Depends(get_current_user), db: Session = Depends(get_db) ): transaction_service = TransactionService(db) transactions = transaction_service.get_user_transactions(current_user, limit, offset) return transactions @router.get("/fiat", response_model=List[FiatTransactionResponse]) def get_fiat_transactions( limit: int = Query(50, ge=1, le=100), offset: int = Query(0, ge=0), current_user: User = Depends(get_current_user), db: Session = Depends(get_db) ): transaction_service = TransactionService(db) transactions = transaction_service.get_user_fiat_transactions(current_user, limit, offset) return transactions @router.get("/crypto/{transaction_id}", response_model=TransactionResponse) def get_crypto_transaction( transaction_id: int, current_user: User = Depends(get_current_user), db: Session = Depends(get_db) ): transaction_service = TransactionService(db) transaction = transaction_service.get_transaction_by_id(transaction_id, current_user) if not transaction: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="Transaction not found" ) return transaction