Обновление приложения
This commit is contained in:
64
backend/app/api/v1/accounts.py
Normal file
64
backend/app/api/v1/accounts.py
Normal file
@@ -0,0 +1,64 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
from app.auth.dependencies import get_current_user
|
||||
from app.database import get_db
|
||||
from app.models.account import Account, AccountType
|
||||
from app.models.user import User
|
||||
from app.schemas.account import AccountCreate, AccountUpdate, AccountOut, AccountTypeOut
|
||||
|
||||
router = APIRouter(prefix="/accounts", tags=["accounts"])
|
||||
|
||||
|
||||
@router.get("/types", response_model=list[AccountTypeOut])
|
||||
def list_types(db: Session = Depends(get_db), _=Depends(get_current_user)):
|
||||
return db.query(AccountType).order_by(AccountType.name).all()
|
||||
|
||||
|
||||
@router.get("", response_model=list[AccountOut])
|
||||
def list_accounts(db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
|
||||
return db.query(Account).filter(Account.user_id == current_user.id).order_by(Account.name).all()
|
||||
|
||||
|
||||
@router.get("/{account_id}", response_model=AccountOut)
|
||||
def get_account(account_id: int, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
|
||||
obj = db.get(Account, account_id)
|
||||
if not obj or obj.user_id != current_user.id:
|
||||
raise HTTPException(status_code=404, detail="Account not found")
|
||||
return obj
|
||||
|
||||
|
||||
@router.post("", response_model=AccountOut, status_code=status.HTTP_201_CREATED)
|
||||
def create_account(body: AccountCreate, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
|
||||
obj = Account(
|
||||
**body.model_dump(),
|
||||
user_id=current_user.id,
|
||||
balance=body.initial_balance,
|
||||
)
|
||||
db.add(obj)
|
||||
db.commit()
|
||||
db.refresh(obj)
|
||||
return obj
|
||||
|
||||
|
||||
@router.patch("/{account_id}", response_model=AccountOut)
|
||||
def update_account(
|
||||
account_id: int, body: AccountUpdate,
|
||||
db: Session = Depends(get_db), current_user: User = Depends(get_current_user)
|
||||
):
|
||||
obj = db.get(Account, account_id)
|
||||
if not obj or obj.user_id != current_user.id:
|
||||
raise HTTPException(status_code=404, detail="Account not found")
|
||||
for field, value in body.model_dump(exclude_none=True).items():
|
||||
setattr(obj, field, value)
|
||||
db.commit()
|
||||
db.refresh(obj)
|
||||
return obj
|
||||
|
||||
|
||||
@router.delete("/{account_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
def delete_account(account_id: int, db: Session = Depends(get_db), current_user: User = Depends(get_current_user)):
|
||||
obj = db.get(Account, account_id)
|
||||
if not obj or obj.user_id != current_user.id:
|
||||
raise HTTPException(status_code=404, detail="Account not found")
|
||||
db.delete(obj)
|
||||
db.commit()
|
||||
Reference in New Issue
Block a user