32 lines
942 B
Python
32 lines
942 B
Python
from fastapi import APIRouter, Depends
|
|
from sqlalchemy.orm import Session
|
|
from typing import Optional
|
|
|
|
from app.db import get_db
|
|
from app.models import Payment
|
|
from app.schemas import PaymentCreate, PaymentResponse
|
|
|
|
router = APIRouter()
|
|
|
|
@router.post("/payment", response_model=PaymentResponse)
|
|
async def create_payment(
|
|
payment: PaymentCreate,
|
|
db: Session = Depends(get_db),
|
|
user_id: Optional[int] = None,
|
|
):
|
|
"""
|
|
Create a new payment record.
|
|
|
|
Args:
|
|
payment (PaymentCreate): The payment data to create.
|
|
db (Session, optional): The database session. Defaults to Depends(get_db).
|
|
user_id (Optional[int], optional): The user ID associated with the payment. Defaults to None.
|
|
|
|
Returns:
|
|
PaymentResponse: The created payment record.
|
|
"""
|
|
db_payment = Payment(**payment.dict(), user_id=user_id)
|
|
db.add(db_payment)
|
|
db.commit()
|
|
db.refresh(db_payment)
|
|
return db_payment |