- admin.py: remove unused get_current_user import - feed.py, flock.py, other.py: add IntegrityError handling on POST/PUT endpoints; duplicate submissions now return 409 instead of crashing with a 500 error - stats.py: extract magic numbers into named module-level constants (DAYS_ROLLING, DAYS_SHORT, PRECISION_AVG, PRECISION_HEN, PRECISION_COST); add return type annotations to _total_feed_cost and _total_other_cost; normalize both helpers to always return Decimal so budget_stats no longer needs Decimal(str(...)) workarounds; simplify _cpe/_cpd helpers - dashboard.js: read --green CSS variable at runtime instead of hardcoding the hex value so chart color stays in sync with the stylesheet - docker-compose.yml: add healthcheck to api service (polls /api/health every 30s) so Docker knows when the API is unhealthy; add password strength guidance comment above the db service Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
90 lines
2.7 KiB
Python
90 lines
2.7 KiB
Python
from datetime import date
|
|
from typing import Optional
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy import select
|
|
from sqlalchemy.exc import IntegrityError
|
|
from sqlalchemy.orm import Session
|
|
|
|
from database import get_db
|
|
from models import OtherPurchase, User
|
|
from schemas import OtherPurchaseCreate, OtherPurchaseUpdate, OtherPurchaseOut
|
|
from auth import get_current_user
|
|
|
|
router = APIRouter(prefix="/api/other", tags=["other"])
|
|
|
|
|
|
@router.get("", response_model=list[OtherPurchaseOut])
|
|
def list_other_purchases(
|
|
start: Optional[date] = None,
|
|
end: Optional[date] = None,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
q = (
|
|
select(OtherPurchase)
|
|
.where(OtherPurchase.user_id == current_user.id)
|
|
.order_by(OtherPurchase.date.desc())
|
|
)
|
|
if start:
|
|
q = q.where(OtherPurchase.date >= start)
|
|
if end:
|
|
q = q.where(OtherPurchase.date <= end)
|
|
return db.scalars(q).all()
|
|
|
|
|
|
@router.post("", response_model=OtherPurchaseOut, status_code=201)
|
|
def create_other_purchase(
|
|
body: OtherPurchaseCreate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
record = OtherPurchase(**body.model_dump(), user_id=current_user.id)
|
|
db.add(record)
|
|
try:
|
|
db.commit()
|
|
except IntegrityError:
|
|
db.rollback()
|
|
raise HTTPException(status_code=409, detail=f"An other purchase for {body.date} already exists.")
|
|
db.refresh(record)
|
|
return record
|
|
|
|
|
|
@router.put("/{record_id}", response_model=OtherPurchaseOut)
|
|
def update_other_purchase(
|
|
record_id: int,
|
|
body: OtherPurchaseUpdate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
record = db.scalars(
|
|
select(OtherPurchase)
|
|
.where(OtherPurchase.id == record_id, OtherPurchase.user_id == current_user.id)
|
|
).first()
|
|
if not record:
|
|
raise HTTPException(status_code=404, detail="Record not found")
|
|
for field, value in body.model_dump(exclude_none=True).items():
|
|
setattr(record, field, value)
|
|
try:
|
|
db.commit()
|
|
except IntegrityError:
|
|
db.rollback()
|
|
raise HTTPException(status_code=409, detail="An other purchase for that date already exists.")
|
|
db.refresh(record)
|
|
return record
|
|
|
|
|
|
@router.delete("/{record_id}", status_code=204)
|
|
def delete_other_purchase(
|
|
record_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
):
|
|
record = db.scalars(
|
|
select(OtherPurchase)
|
|
.where(OtherPurchase.id == record_id, OtherPurchase.user_id == current_user.id)
|
|
).first()
|
|
if not record:
|
|
raise HTTPException(status_code=404, detail="Record not found")
|
|
db.delete(record)
|
|
db.commit()
|