Files
derekcandClaude Opus 5.5 c9833eb810 Upgrade dependencies and patch known vulnerabilities
Bump fastapi (starlette 1.8.0), uvicorn, python-multipart, pydantic-settings,
pytz, email-validator, and slowapi. Move nginx and mysql image pins to
current digests. Rename deprecated HTTP_422 status constant.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-17 00:06:28 -07:00

100 lines
3.4 KiB
Python

from fastapi import APIRouter, Depends, HTTPException, Query, status
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select
from app.dependencies import get_db, get_current_user
from app.models.user import User
from app.models.entry import Entry, EntryType
from app.schemas.entry import EntryCreate, EntryResponse, BottleStats
router = APIRouter(prefix="/api/entries", tags=["entries"])
def _calc_stats(entries: list[Entry]) -> BottleStats:
adds = [e for e in entries if e.entry_type == EntryType.add]
removes = [e for e in entries if e.entry_type == EntryType.remove]
total_add_shots = sum(e.amount_shots for e in adds)
total_remove_shots = sum(e.amount_shots for e in removes)
current_total = total_add_shots - total_remove_shots
# Weighted average proof across all add entries
weighted_proof_sum = sum(e.proof * e.amount_shots for e in adds if e.proof is not None)
proof_shot_total = sum(e.amount_shots for e in adds if e.proof is not None)
estimated_proof = (weighted_proof_sum / proof_shot_total) if proof_shot_total > 0 else None
return BottleStats(
total_add_entries=len(adds),
total_shots_added=round(total_add_shots, 2),
current_total_shots=round(current_total, 2),
estimated_proof=round(estimated_proof, 1) if estimated_proof is not None else None,
)
@router.get("", response_model=list[EntryResponse])
async def list_entries(
limit: int = Query(default=200, ge=1, le=1000),
offset: int = Query(default=0, ge=0),
db: AsyncSession = Depends(get_db),
current_user: User = Depends(get_current_user),
):
result = await db.execute(
select(Entry)
.where(Entry.user_id == current_user.id)
.order_by(Entry.date.desc(), Entry.created_at.desc())
.limit(limit)
.offset(offset)
)
return result.scalars().all()
@router.post("", response_model=EntryResponse, status_code=status.HTTP_201_CREATED)
async def create_entry(
body: EntryCreate,
db: AsyncSession = Depends(get_db),
current_user: User = Depends(get_current_user),
):
if body.entry_type == EntryType.add and not body.bourbon_name:
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_CONTENT, detail="bourbon_name is required for add entries")
entry = Entry(
user_id=current_user.id,
entry_type=body.entry_type,
date=body.date,
bourbon_name=body.bourbon_name,
proof=body.proof,
amount_shots=body.amount_shots,
notes=body.notes,
)
db.add(entry)
await db.commit()
await db.refresh(entry)
return entry
@router.delete("/{entry_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_entry(
entry_id: int,
db: AsyncSession = Depends(get_db),
current_user: User = Depends(get_current_user),
):
result = await db.execute(
select(Entry).where(Entry.id == entry_id, Entry.user_id == current_user.id)
)
entry = result.scalar_one_or_none()
if not entry:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Entry not found")
await db.delete(entry)
await db.commit()
@router.get("/stats", response_model=BottleStats)
async def get_stats(
db: AsyncSession = Depends(get_db),
current_user: User = Depends(get_current_user),
):
result = await db.execute(select(Entry).where(Entry.user_id == current_user.id))
entries = result.scalars().all()
return _calc_stats(entries)