Files
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

46 lines
1.8 KiB
Python

from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select
from app.dependencies import get_db
from app.models.user import User
from app.models.entry import Entry, EntryType
from app.schemas.entry import PublicUserStats
router = APIRouter(prefix="/api/public", tags=["public"])
@router.get("/stats", response_model=list[PublicUserStats])
async def public_stats(db: AsyncSession = Depends(get_db)):
users_result = await db.execute(select(User))
users = users_result.scalars().all()
stats: list[PublicUserStats] = []
for user in users:
entries_result = await db.execute(select(Entry).where(Entry.user_id == user.id))
entries = entries_result.scalars().all()
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_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 = round(weighted_proof_sum / proof_shot_total, 1) if proof_shot_total > 0 else None
bourbons = sorted({e.bourbon_name for e in adds if e.bourbon_name}, key=str.casefold)
stats.append(PublicUserStats(
display_name=user.display_name or user.email.split("@")[0],
total_add_entries=len(adds),
current_total_shots=round(current_total, 2),
estimated_proof=estimated_proof,
bottle_size=user.bottle_size,
bourbons=bourbons,
))
return stats