|
| 1 | +from typing import List |
| 2 | +import uuid |
| 3 | + |
| 4 | +from fastapi import APIRouter, Depends, HTTPException, status, Query |
| 5 | +from sqlalchemy.orm import Session |
| 6 | +from sqlalchemy.exc import IntegrityError |
| 7 | + |
| 8 | +from app.core.database import get_db |
| 9 | +from app.models.transaction import Transaction |
| 10 | +from app.schemas.transaction import TransactionCreate, TransactionUpdate, TransactionResponse |
| 11 | + |
| 12 | +router = APIRouter() |
| 13 | + |
| 14 | + |
| 15 | +def retrieve_transaction(transaction_id: uuid.UUID, db: Session) -> Transaction: |
| 16 | + """Gets a single transaction or raise if non existent. |
| 17 | +
|
| 18 | + Raises: |
| 19 | + HTTPException (404 Not Found) if the transaction does not exist. |
| 20 | + """ |
| 21 | + transaction = db.query(Transaction).filter(Transaction.id == transaction_id).one_or_none() |
| 22 | + |
| 23 | + if transaction is None: |
| 24 | + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Transaction with ID {transaction_id} not found") |
| 25 | + |
| 26 | + return transaction |
| 27 | + |
| 28 | + |
| 29 | +@router.post( |
| 30 | + "/", |
| 31 | + response_model=TransactionResponse, |
| 32 | + status_code=status.HTTP_201_CREATED, |
| 33 | +) |
| 34 | +def create_transaction( |
| 35 | + data: TransactionCreate, |
| 36 | + db: Session = Depends(get_db) |
| 37 | +) -> TransactionResponse: |
| 38 | + |
| 39 | + transaction = Transaction( |
| 40 | + amount=data.amount, |
| 41 | + method=data.method, |
| 42 | + date=data.date, |
| 43 | + description=data.description, |
| 44 | + ) |
| 45 | + db.add(transaction) |
| 46 | + try: |
| 47 | + db.commit() |
| 48 | + db.refresh(transaction) |
| 49 | + except IntegrityError as e: |
| 50 | + db.rollback() |
| 51 | + raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Database constraint violation") from e |
| 52 | + |
| 53 | + return transaction.to_response() |
| 54 | + |
| 55 | + |
| 56 | +@router.get( |
| 57 | + "/{transaction_id}", |
| 58 | + response_model=TransactionResponse, |
| 59 | + status_code=status.HTTP_200_OK, |
| 60 | +) |
| 61 | +def get_single_transaction( |
| 62 | + transaction_id: uuid.UUID, |
| 63 | + db: Session = Depends(get_db) |
| 64 | +) -> TransactionResponse: |
| 65 | + |
| 66 | + transaction = retrieve_transaction(transaction_id, db) |
| 67 | + return transaction.to_response() |
| 68 | + |
| 69 | + |
| 70 | +@router.get( |
| 71 | + "/", |
| 72 | + response_model=List[TransactionResponse], |
| 73 | + status_code=status.HTTP_200_OK, |
| 74 | +) |
| 75 | +def get_all_transactions( |
| 76 | + db: Session = Depends(get_db), |
| 77 | + offset: int = Query(0, ge=0, description="Offset must be 0 or greater (defaults to 0)"), |
| 78 | + limit: int = Query(10, ge=1, le=100, description="Limit must be between 1 and 100 (defaults to 10)"), |
| 79 | +) -> List[TransactionResponse]: |
| 80 | + |
| 81 | + transactions = db.query(Transaction).offset(offset).limit(limit).all() |
| 82 | + return [t.to_response() for t in transactions] |
| 83 | + |
| 84 | + |
| 85 | +@router.patch( |
| 86 | + "/{transaction_id}", |
| 87 | + response_model=TransactionResponse, |
| 88 | + status_code=status.HTTP_200_OK, |
| 89 | +) |
| 90 | +def update_transaction( |
| 91 | + transaction_id: uuid.UUID, |
| 92 | + data: TransactionUpdate, |
| 93 | + db: Session = Depends(get_db), |
| 94 | +) -> TransactionResponse: |
| 95 | + |
| 96 | + transaction = retrieve_transaction(transaction_id, db) |
| 97 | + |
| 98 | + update_data = data.model_dump(exclude_unset=True) |
| 99 | + |
| 100 | + field_mapping = { |
| 101 | + "amount": "amount", |
| 102 | + "description": "description", |
| 103 | + "method": "method", |
| 104 | + "date": "date" |
| 105 | + } |
| 106 | + |
| 107 | + for schema_field, db_field in field_mapping.items(): |
| 108 | + if schema_field in update_data: |
| 109 | + setattr(transaction, db_field, update_data[schema_field]) |
| 110 | + |
| 111 | + db.commit() |
| 112 | + db.refresh(transaction) |
| 113 | + |
| 114 | + return transaction.to_response() |
| 115 | + |
| 116 | + |
| 117 | +@router.delete( |
| 118 | + "/{transaction_id}", |
| 119 | + status_code=status.HTTP_204_NO_CONTENT |
| 120 | +) |
| 121 | +def delete_transaction( |
| 122 | + transaction_id: uuid.UUID, |
| 123 | + db: Session = Depends(get_db), |
| 124 | +) -> None: |
| 125 | + |
| 126 | + transaction = retrieve_transaction(transaction_id, db) |
| 127 | + |
| 128 | + try: |
| 129 | + db.delete(transaction) |
| 130 | + db.commit() |
| 131 | + except IntegrityError as e: |
| 132 | + db.rollback() |
| 133 | + raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="Cannot delete transaction due to related records") from e |
0 commit comments