141 lines
4.0 KiB
Python
141 lines
4.0 KiB
Python
"""Wallet entry routes."""
|
|||
|
|
|
||
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
||
|
|
from sqlalchemy.orm import Session
|
||
|
|
from app.database import get_db
|
||
|
|
from app.models import User, WalletEntry, EntryType
|
||
|
|
from app.schemas import (
|
||
|
|
WalletEntryCreate,
|
||
|
|
WalletEntryUpdate,
|
||
|
|
WalletEntryResponse,
|
||
|
|
DashboardMetrics
|
||
|
|
)
|
||
|
|
from app.auth import get_current_user
|
||
|
|
|
||
|
|
router = APIRouter(prefix="/wallet", tags=["wallet"])
|
||
|
|
|
||
|
|
|
||
|
|
@router.post("/entries", response_model=WalletEntryResponse)
|
||
|
|
def create_entry(
|
||
|
|
entry_data: WalletEntryCreate,
|
||
|
|
current_user: User = Depends(get_current_user),
|
||
|
|
db: Session = Depends(get_db)
|
||
|
|
):
|
||
|
|
"""Create a new wallet entry."""
|
||
|
|
new_entry = WalletEntry(
|
||
|
|
user_id=current_user.id,
|
||
|
|
amount=entry_data.amount,
|
||
|
|
entry_type=entry_data.entry_type,
|
||
|
|
description=entry_data.description
|
||
|
|
)
|
||
|
|
db.add(new_entry)
|
||
|
|
db.commit()
|
||
|
|
db.refresh(new_entry)
|
||
|
|
return new_entry
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/entries", response_model=list[WalletEntryResponse])
|
||
|
|
def get_entries(
|
||
|
|
current_user: User = Depends(get_current_user),
|
||
|
|
db: Session = Depends(get_db),
|
||
|
|
skip: int = 0,
|
||
|
|
limit: int = 100
|
||
|
|
):
|
||
|
|
"""Get all wallet entries for the current user."""
|
||
|
|
entries = db.query(WalletEntry).filter(
|
||
|
|
WalletEntry.user_id == current_user.id
|
||
|
|
).order_by(WalletEntry.created_at.desc()).offset(skip).limit(limit).all()
|
||
|
|
return entries
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/entries/{entry_id}", response_model=WalletEntryResponse)
|
||
|
|
def get_entry(
|
||
|
|
entry_id: int,
|
||
|
|
current_user: User = Depends(get_current_user),
|
||
|
|
db: Session = Depends(get_db)
|
||
|
|
):
|
||
|
|
"""Get a specific wallet entry."""
|
||
|
|
entry = db.query(WalletEntry).filter(
|
||
|
|
(WalletEntry.id == entry_id) & (WalletEntry.user_id == current_user.id)
|
||
|
|
).first()
|
||
|
|
|
||
|
|
if not entry:
|
||
|
|
raise HTTPException(
|
||
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
||
|
|
detail="Entry not found"
|
||
|
|
)
|
||
|
|
return entry
|
||
|
|
|
||
|
|
|
||
|
|
@router.put("/entries/{entry_id}", response_model=WalletEntryResponse)
|
||
|
|
def update_entry(
|
||
|
|
entry_id: int,
|
||
|
|
entry_data: WalletEntryUpdate,
|
||
|
|
current_user: User = Depends(get_current_user),
|
||
|
|
db: Session = Depends(get_db)
|
||
|
|
):
|
||
|
|
"""Update a wallet entry."""
|
||
|
|
entry = db.query(WalletEntry).filter(
|
||
|
|
(WalletEntry.id == entry_id) & (WalletEntry.user_id == current_user.id)
|
||
|
|
).first()
|
||
|
|
|
||
|
|
if not entry:
|
||
|
|
raise HTTPException(
|
||
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
||
|
|
detail="Entry not found"
|
||
|
|
)
|
||
|
|
|
||
|
|
if entry_data.amount is not None:
|
||
|
|
entry.amount = entry_data.amount
|
||
|
|
if entry_data.entry_type is not None:
|
||
|
|
entry.entry_type = entry_data.entry_type
|
||
|
|
if entry_data.description is not None:
|
||
|
|
entry.description = entry_data.description
|
||
|
|
|
||
|
|
db.commit()
|
||
|
|
db.refresh(entry)
|
||
|
|
return entry
|
||
|
|
|
||
|
|
|
||
|
|
@router.delete("/entries/{entry_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||
|
|
def delete_entry(
|
||
|
|
entry_id: int,
|
||
|
|
current_user: User = Depends(get_current_user),
|
||
|
|
db: Session = Depends(get_db)
|
||
|
|
):
|
||
|
|
"""Delete a wallet entry."""
|
||
|
|
entry = db.query(WalletEntry).filter(
|
||
|
|
(WalletEntry.id == entry_id) & (WalletEntry.user_id == current_user.id)
|
||
|
|
).first()
|
||
|
|
|
||
|
|
if not entry:
|
||
|
|
raise HTTPException(
|
||
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
||
|
|
detail="Entry not found"
|
||
|
|
)
|
||
|
|
|
||
|
|
db.delete(entry)
|
||
|
|
db.commit()
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/metrics", response_model=DashboardMetrics)
|
||
|
|
def get_metrics(
|
||
|
|
current_user: User = Depends(get_current_user),
|
||
|
|
db: Session = Depends(get_db)
|
||
|
|
):
|
||
|
|
"""Get dashboard metrics for the current user."""
|
||
|
|
entries = db.query(WalletEntry).filter(
|
||
|
|
WalletEntry.user_id == current_user.id
|
||
|
|
).all()
|
||
|
|
|
||
|
|
total_income = sum(e.amount for e in entries if e.entry_type == EntryType.INCOME)
|
||
|
|
total_expenses = sum(e.amount for e in entries if e.entry_type == EntryType.EXPENSE)
|
||
|
|
total_balance = total_income - total_expenses
|
||
|
|
|
||
|
|
return {
|
||
|
|
"total_balance": total_balance,
|
||
|
|
"total_income": total_income,
|
||
|
|
"total_expenses": total_expenses,
|
||
|
|
"entry_count": len(entries)
|
||
|
|
}
|