Обновление приложения

This commit is contained in:
2026-04-28 22:37:19 +03:00
parent 7d00cf062e
commit 52594b5dc6
99 changed files with 5736 additions and 1052 deletions

View File

@@ -0,0 +1,61 @@
from datetime import date
from fastapi import APIRouter, Depends, HTTPException, Query, status
from sqlalchemy.orm import Session
from app.auth.dependencies import get_current_user, require_role
from app.database import get_db
from app.models.currency import ExchangeRate
from app.schemas.currency import ExchangeRateCreate, ExchangeRateOut
router = APIRouter(prefix="/exchange-rates", tags=["exchange-rates"])
require_accountant = require_role("accountant")
@router.get("", response_model=list[ExchangeRateOut])
def list_rates(
currency_from_id: int | None = None,
currency_to_id: int | None = None,
rate_date: date | None = None,
limit: int = Query(50, le=500),
db: Session = Depends(get_db),
_=Depends(get_current_user),
):
q = db.query(ExchangeRate)
if currency_from_id:
q = q.filter(ExchangeRate.currency_from_id == currency_from_id)
if currency_to_id:
q = q.filter(ExchangeRate.currency_to_id == currency_to_id)
if rate_date:
q = q.filter(ExchangeRate.rate_date == rate_date)
return q.order_by(ExchangeRate.rate_date.desc()).limit(limit).all()
@router.post("", response_model=ExchangeRateOut, status_code=status.HTTP_201_CREATED)
def create_rate(body: ExchangeRateCreate, db: Session = Depends(get_db), _=Depends(require_accountant)):
if body.currency_from_id == body.currency_to_id:
raise HTTPException(status_code=400, detail="Cannot set rate between the same currency")
existing = db.query(ExchangeRate).filter(
ExchangeRate.currency_from_id == body.currency_from_id,
ExchangeRate.currency_to_id == body.currency_to_id,
ExchangeRate.rate_date == body.rate_date,
).first()
if existing:
for field, value in body.model_dump(exclude_none=True).items():
setattr(existing, field, value)
db.commit()
db.refresh(existing)
return existing
obj = ExchangeRate(**body.model_dump())
db.add(obj)
db.commit()
db.refresh(obj)
return obj
@router.delete("/{rate_id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_rate(rate_id: int, db: Session = Depends(get_db), _=Depends(require_accountant)):
obj = db.get(ExchangeRate, rate_id)
if not obj:
raise HTTPException(status_code=404, detail="Exchange rate not found")
db.delete(obj)
db.commit()