- New other_purchases table (date, total, notes) - /api/other CRUD endpoints - Budget stats now include other costs in cost/egg and cost/dozen math - Budget page: new Log Other Purchases form, stat cards for other costs, combined Purchase History table showing feed and other entries together Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
from datetime import date
|
|
from typing import Optional
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy import select
|
|
from sqlalchemy.orm import Session
|
|
|
|
from database import get_db
|
|
from models import OtherPurchase
|
|
from schemas import OtherPurchaseCreate, OtherPurchaseUpdate, OtherPurchaseOut
|
|
|
|
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),
|
|
):
|
|
q = select(OtherPurchase).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)):
|
|
record = OtherPurchase(**body.model_dump())
|
|
db.add(record)
|
|
db.commit()
|
|
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),
|
|
):
|
|
record = db.get(OtherPurchase, record_id)
|
|
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)
|
|
db.commit()
|
|
db.refresh(record)
|
|
return record
|
|
|
|
|
|
@router.delete("/{record_id}", status_code=204)
|
|
def delete_other_purchase(record_id: int, db: Session = Depends(get_db)):
|
|
record = db.get(OtherPurchase, record_id)
|
|
if not record:
|
|
raise HTTPException(status_code=404, detail="Record not found")
|
|
db.delete(record)
|
|
db.commit()
|