138 lines
3.6 KiB
Python
138 lines
3.6 KiB
Python
from typing import Any, List
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.auth import get_current_active_user
|
|
from app.crud import payment as crud_payment
|
|
from app.db.session import get_db
|
|
from app.models.user import User
|
|
from app.schemas.payment import Payment, PaymentCreate, PaymentUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("", response_model=List[Payment])
|
|
def read_payments(
|
|
db: Session = Depends(get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
current_user: User = Depends(get_current_active_user)
|
|
) -> Any:
|
|
"""
|
|
Retrieve payments for the current user.
|
|
"""
|
|
payments = crud_payment.get_payments(
|
|
db, user_id=current_user.id, skip=skip, limit=limit
|
|
)
|
|
return payments
|
|
|
|
|
|
@router.get("/invoice/{invoice_id}", response_model=List[Payment])
|
|
def read_payments_for_invoice(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
invoice_id: int,
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
current_user: User = Depends(get_current_active_user)
|
|
) -> Any:
|
|
"""
|
|
Retrieve payments for a specific invoice.
|
|
"""
|
|
payments = crud_payment.get_payments_for_invoice(
|
|
db, user_id=current_user.id, invoice_id=invoice_id, skip=skip, limit=limit
|
|
)
|
|
return payments
|
|
|
|
|
|
@router.post("", response_model=Payment, status_code=status.HTTP_201_CREATED)
|
|
def create_payment(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
payment_in: PaymentCreate,
|
|
current_user: User = Depends(get_current_active_user)
|
|
) -> Any:
|
|
"""
|
|
Create new payment for the current user.
|
|
"""
|
|
try:
|
|
payment = crud_payment.create_payment(
|
|
db, user_id=current_user.id, payment_in=payment_in
|
|
)
|
|
return payment
|
|
except ValueError as e:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail=str(e),
|
|
)
|
|
|
|
|
|
@router.get("/{payment_id}", response_model=Payment)
|
|
def read_payment(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
payment_id: int,
|
|
current_user: User = Depends(get_current_active_user)
|
|
) -> Any:
|
|
"""
|
|
Get payment by ID.
|
|
"""
|
|
payment = crud_payment.get_payment(
|
|
db, user_id=current_user.id, payment_id=payment_id
|
|
)
|
|
if not payment:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Payment not found",
|
|
)
|
|
return payment
|
|
|
|
|
|
@router.patch("/{payment_id}", response_model=Payment)
|
|
def update_payment(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
payment_id: int,
|
|
payment_in: PaymentUpdate,
|
|
current_user: User = Depends(get_current_active_user)
|
|
) -> Any:
|
|
"""
|
|
Update a payment.
|
|
"""
|
|
payment = crud_payment.get_payment(
|
|
db, user_id=current_user.id, payment_id=payment_id
|
|
)
|
|
if not payment:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Payment not found",
|
|
)
|
|
payment = crud_payment.update_payment(
|
|
db, user_id=current_user.id, db_payment=payment, payment_in=payment_in
|
|
)
|
|
return payment
|
|
|
|
|
|
@router.delete("/{payment_id}", response_model=Payment)
|
|
def delete_payment(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
payment_id: int,
|
|
current_user: User = Depends(get_current_active_user)
|
|
) -> Any:
|
|
"""
|
|
Delete a payment.
|
|
"""
|
|
payment = crud_payment.get_payment(
|
|
db, user_id=current_user.id, payment_id=payment_id
|
|
)
|
|
if not payment:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Payment not found",
|
|
)
|
|
payment = crud_payment.delete_payment(
|
|
db, user_id=current_user.id, payment_id=payment_id
|
|
)
|
|
return payment |