Add initial project files
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
0
backend/app/routers/__init__.py
Normal file
0
backend/app/routers/__init__.py
Normal file
39
backend/app/routers/auth.py
Normal file
39
backend/app/routers/auth.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.dependencies import get_db
|
||||
from app.models.user import User
|
||||
from app.schemas.user import UserCreate, Token, LoginRequest
|
||||
from app.utils.security import hash_password, verify_password, create_token
|
||||
|
||||
router = APIRouter(prefix="/api/auth", tags=["auth"])
|
||||
|
||||
|
||||
@router.post("/register", response_model=Token, status_code=status.HTTP_201_CREATED)
|
||||
async def register(body: UserCreate, db: AsyncSession = Depends(get_db)):
|
||||
result = await db.execute(select(User).where(User.email == body.email))
|
||||
if result.scalar_one_or_none():
|
||||
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="Email already registered")
|
||||
|
||||
user = User(
|
||||
email=body.email,
|
||||
password_hash=hash_password(body.password),
|
||||
display_name=body.display_name or body.email.split("@")[0],
|
||||
)
|
||||
db.add(user)
|
||||
await db.commit()
|
||||
await db.refresh(user)
|
||||
|
||||
return Token(access_token=create_token(user.id))
|
||||
|
||||
|
||||
@router.post("/login", response_model=Token)
|
||||
async def login(body: LoginRequest, db: AsyncSession = Depends(get_db)):
|
||||
result = await db.execute(select(User).where(User.email == body.email))
|
||||
user = result.scalar_one_or_none()
|
||||
|
||||
if not user or not verify_password(body.password, user.password_hash):
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid credentials")
|
||||
|
||||
return Token(access_token=create_token(user.id))
|
||||
95
backend/app/routers/entries.py
Normal file
95
backend/app/routers/entries.py
Normal file
@@ -0,0 +1,95 @@
|
||||
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),
|
||||
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,
|
||||
)
|
||||
async with db.begin():
|
||||
db.add(entry)
|
||||
|
||||
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")
|
||||
|
||||
async with db.begin():
|
||||
await db.delete(entry)
|
||||
|
||||
|
||||
@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)
|
||||
41
backend/app/routers/public.py
Normal file
41
backend/app/routers/public.py
Normal file
@@ -0,0 +1,41 @@
|
||||
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
|
||||
|
||||
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,
|
||||
))
|
||||
|
||||
return stats
|
||||
47
backend/app/routers/users.py
Normal file
47
backend/app/routers/users.py
Normal file
@@ -0,0 +1,47 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.dependencies import get_db, get_current_user
|
||||
from app.models.user import User
|
||||
from app.schemas.user import UserResponse, UserUpdate, PasswordChange
|
||||
from app.utils.security import verify_password, hash_password
|
||||
|
||||
router = APIRouter(prefix="/api/users", tags=["users"])
|
||||
|
||||
|
||||
@router.get("/me", response_model=UserResponse)
|
||||
async def get_me(current_user: User = Depends(get_current_user)):
|
||||
return current_user
|
||||
|
||||
|
||||
@router.put("/me", response_model=UserResponse)
|
||||
async def update_me(
|
||||
body: UserUpdate,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
if body.display_name is not None:
|
||||
current_user.display_name = body.display_name
|
||||
if body.timezone is not None:
|
||||
current_user.timezone = body.timezone
|
||||
|
||||
async with db.begin():
|
||||
db.add(current_user)
|
||||
|
||||
await db.refresh(current_user)
|
||||
return current_user
|
||||
|
||||
|
||||
@router.put("/me/password", status_code=status.HTTP_204_NO_CONTENT)
|
||||
async def change_password(
|
||||
body: PasswordChange,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
if not verify_password(body.current_password, current_user.password_hash):
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Current password is incorrect")
|
||||
|
||||
current_user.password_hash = hash_password(body.new_password)
|
||||
|
||||
async with db.begin():
|
||||
db.add(current_user)
|
||||
Reference in New Issue
Block a user