Files
bourbonacci/backend/app/routers/entries.py
derekc ca83351e9d Add bottle size, bourbon list modal, and stat improvements
- Add bottle_size field to User model and UserResponse/UserUpdate schemas
- Settings modal includes bottle size input (shots capacity)
- Community bottles and My Bottle page show fill bar based on bottle size
- Community bottle cards are clickable — opens searchable bourbon list modal
- Add total_shots_added stat to replace duplicate net volume on dashboard
- Reorder dashboard stats: Bourbons Added, Total Poured In, Shots Remaining, Est. Proof
- Theme-matched custom scrollbar (amber on dark)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-24 21:53:50 -07:00

96 lines
3.3 KiB
Python

from fastapi import APIRouter, Depends, HTTPException, 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(
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())
)
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_ENTITY, 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)