feat: backend core — models, auth, CRUD, tests
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
import uuid
|
||||
|
||||
from fastapi import APIRouter, Depends, Query, status
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.auth.dependencies import get_current_user
|
||||
from app.database import get_session
|
||||
from app.models.transaction import TransactionType
|
||||
from app.models.user import User
|
||||
from app.schemas.transaction import (
|
||||
PaginatedTransactions,
|
||||
TransactionCreate,
|
||||
TransactionResponse,
|
||||
TransactionUpdate,
|
||||
)
|
||||
from app.services import transaction_service
|
||||
|
||||
router = APIRouter(prefix="/transactions", tags=["transactions"])
|
||||
|
||||
|
||||
@router.get("", response_model=PaginatedTransactions)
|
||||
async def list_transactions(
|
||||
month: str | None = Query(None, description="Filter by month (YYYY-MM)"),
|
||||
category_id: uuid.UUID | None = Query(None),
|
||||
type: TransactionType | None = Query(None),
|
||||
page: int = Query(1, ge=1),
|
||||
per_page: int = Query(20, ge=1, le=100),
|
||||
session: AsyncSession = Depends(get_session),
|
||||
current_user: User = Depends(get_current_user),
|
||||
) -> PaginatedTransactions:
|
||||
items, total = await transaction_service.list_transactions(
|
||||
session,
|
||||
current_user.id,
|
||||
month=month,
|
||||
category_id=category_id,
|
||||
type_filter=type,
|
||||
page=page,
|
||||
per_page=per_page,
|
||||
)
|
||||
return PaginatedTransactions(
|
||||
items=[TransactionResponse.model_validate(t) for t in items],
|
||||
total=total,
|
||||
page=page,
|
||||
per_page=per_page,
|
||||
)
|
||||
|
||||
|
||||
@router.post("", response_model=TransactionResponse, status_code=status.HTTP_201_CREATED)
|
||||
async def create_transaction(
|
||||
data: TransactionCreate,
|
||||
session: AsyncSession = Depends(get_session),
|
||||
current_user: User = Depends(get_current_user),
|
||||
) -> TransactionResponse:
|
||||
tx = await transaction_service.create_transaction(session, current_user.id, data)
|
||||
return TransactionResponse.model_validate(tx)
|
||||
|
||||
|
||||
@router.get("/{transaction_id}", response_model=TransactionResponse)
|
||||
async def get_transaction(
|
||||
transaction_id: uuid.UUID,
|
||||
session: AsyncSession = Depends(get_session),
|
||||
current_user: User = Depends(get_current_user),
|
||||
) -> TransactionResponse:
|
||||
tx = await transaction_service.get_transaction(session, current_user.id, transaction_id)
|
||||
return TransactionResponse.model_validate(tx)
|
||||
|
||||
|
||||
@router.put("/{transaction_id}", response_model=TransactionResponse)
|
||||
async def update_transaction(
|
||||
transaction_id: uuid.UUID,
|
||||
data: TransactionUpdate,
|
||||
session: AsyncSession = Depends(get_session),
|
||||
current_user: User = Depends(get_current_user),
|
||||
) -> TransactionResponse:
|
||||
tx = await transaction_service.update_transaction(
|
||||
session, current_user.id, transaction_id, data
|
||||
)
|
||||
return TransactionResponse.model_validate(tx)
|
||||
|
||||
|
||||
@router.delete("/{transaction_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
async def delete_transaction(
|
||||
transaction_id: uuid.UUID,
|
||||
session: AsyncSession = Depends(get_session),
|
||||
current_user: User = Depends(get_current_user),
|
||||
) -> None:
|
||||
await transaction_service.delete_transaction(session, current_user.id, transaction_id)
|
||||
Reference in New Issue
Block a user